Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint/SafeNavigationChain creating non-functional code when [] are involved #11230

Closed
tjschuck opened this issue Dec 3, 2022 · 0 comments · Fixed by #11231
Closed

Lint/SafeNavigationChain creating non-functional code when [] are involved #11230

tjschuck opened this issue Dec 3, 2022 · 0 comments · Fixed by #11231
Labels

Comments

@tjschuck
Copy link
Contributor

tjschuck commented Dec 3, 2022

This is similar to the bug fixed by #4749 (see all the linked issues).

Before:

subscription.customer&.custom_fields[:receipt_recipients].to_s

After running rubocop -a --only Lint/SafeNavigationChain on it:

subscription.customer&.custom_fields&.[:receipt_recipients].to_s

That &.[ is invalid Ruby.

In fact, Rubocop also correctly identifies it as invalid and throws Lint/Syntax: unexpected token tLBRACK2 on it after it made the correction in the first place!


Expected behavior

Certainly not generate invalid code!

Interestingly, if you use the same code without the trailing to_s on the chain:

subscription.customer&.custom_fields[:receipt_recipients]

it corrects it to

subscription.customer&.custom_fields&.[](:receipt_recipients)

which is at least correct Ruby, but the &.[](: is pretty gross. I manually fixed my own code using dig instead (custom_fields&.dig(:receipt_recipients)), which reads better to me.

Actual behavior

Here's the full output with --debug that generates the invalid code:

$ cat safe-navigation-with-hash-access.rb
subscription.customer&.custom_fields[:receipt_recipients].to_s

$ rubocop -a --debug --only Lint/SafeNavigationChain safe-navigation-with-hash-access.rb
For /Users/tjschuck/Code/rubocop: configuration from /Users/tjschuck/Code/rubocop/.rubocop.yml
configuration from /Users/tjschuck/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-performance-1.15.1/config/default.yml
configuration from /Users/tjschuck/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-performance-1.15.1/config/default.yml
Default configuration from /Users/tjschuck/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-1.39.0/config/default.yml
configuration from /Users/tjschuck/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-rspec-2.15.0/config/default.yml
configuration from /Users/tjschuck/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-rspec-2.15.0/config/default.yml
configuration from /Users/tjschuck/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-rake-0.6.0/config/default.yml
configuration from /Users/tjschuck/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/rubocop-rake-0.6.0/config/default.yml
Inheriting configuration from /Users/tjschuck/Code/rubocop/.rubocop_todo.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /Users/tjschuck/Code/rubocop/safe-navigation-with-hash-access.rb
Loading cache from /Users/tjschuck/.cache/rubocop_cache/11226f41faec7637153da17318ab9466aeea3fbf/5bd7dd7c49a5710853cb463c9447753a064e7a92/9d1153664702149d21c8f5721ac254facef98fa1
W

Offenses:

safe-navigation-with-hash-access.rb:1:37: W: [Corrected] Lint/SafeNavigationChain: Do not chain ordinary method call after safe navigation operator.
subscription.customer&.custom_fields[:receipt_recipients].to_s
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
Finished in 0.5164380000060191 seconds

Steps to reproduce the problem

Reproducible with the code snippets above.

RuboCop version

$ rubocop -V
1.39.0 (using Parser 3.1.3.0, rubocop-ast 1.24.0, running on ruby 3.0.4) [x86_64-darwin21]
  - rubocop-performance 1.15.1
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.15.0
@koic koic added the bug label Dec 3, 2022
koic added a commit to koic/rubocop that referenced this issue Dec 3, 2022
…tionChain`

Fixes rubocop#11230.

This PR fixes an incorrect autocorrect for `Lint/SafeNavigationChain`
when using safe navigation with `[]` operator followed method chain.
bbatsov pushed a commit that referenced this issue Dec 5, 2022
Fixes #11230.

This PR fixes an incorrect autocorrect for `Lint/SafeNavigationChain`
when using safe navigation with `[]` operator followed method chain.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants