You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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
…tionChain`
Fixesrubocop#11230.
This PR fixes an incorrect autocorrect for `Lint/SafeNavigationChain`
when using safe navigation with `[]` operator followed method chain.
Fixes#11230.
This PR fixes an incorrect autocorrect for `Lint/SafeNavigationChain`
when using safe navigation with `[]` operator followed method chain.
This is similar to the bug fixed by #4749 (see all the linked issues).
Before:
After running
rubocop -a --only Lint/SafeNavigationChain
on it: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:it corrects it to
which is at least correct Ruby, but the
&.[](:
is pretty gross. I manually fixed my own code usingdig
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:Steps to reproduce the problem
Reproducible with the code snippets above.
RuboCop version
The text was updated successfully, but these errors were encountered: