KEMBAR78
Fix Style/SafeNavigation cop to preserve existing safe navigation in check by martinemde · Pull Request #14424 · rubocop/rubocop · GitHub
Skip to content

Conversation

@martinemde
Copy link
Contributor

@martinemde martinemde commented Aug 5, 2025

Fixes issue where safe navigation is lost when it exists in the check. For example, "check if the variable is not nil and the method call foo on that object doesn't return nil, if so, call bar on the result, else nil":

variable&.foo ? variable.foo.bar : nil # bad

This fixes the autocorrect from the incorrect behavior that omits the original safe navigation, to the correct behavior that preserves the existing safe navigation.

variable.foo&.bar # incorrect
variable&.foo&.bar # fixed

Fixes:

- foo&.bar && foo.bar.baz
+ foo&.bar&.baz

- if foo&.bar; foo.bar.baz; end
+ foo&.bar&.baz

- foo&.bar ? foo.bar.baz : nil
+ foo&.bar&.baz

This relates to a comment I placed in #14420. Fixing the issue with this PR rather than opening a new issue.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@martinemde martinemde force-pushed the martinemde/fix-incorrect-autocorrect-style-safe-navigation branch from 1438096 to 4ba7e30 Compare August 5, 2025 15:06
@koic
Copy link
Member

koic commented Aug 5, 2025

Can you add a changelog entry?

@martinemde martinemde force-pushed the martinemde/fix-incorrect-autocorrect-style-safe-navigation branch from 37844b4 to 3cd2db7 Compare August 5, 2025 19:48
Fixes issue where 'variable&.foo ? variable.foo.bar : nil'
was incorrectly corrected to 'variable.foo&.bar' instead of
the correct 'variable&.foo&.bar'

- Fixes: foo&.bar && foo.bar.baz → foo&.bar&.baz
- Fixes: if foo&.bar; foo.bar.baz; end → foo&.bar&.baz
- Fixes: foo&.bar ? foo.bar.baz : nil → foo&.bar&.baz

Co-authored-by: Koichi ITO <koic.ito@gmail.com>
@martinemde martinemde force-pushed the martinemde/fix-incorrect-autocorrect-style-safe-navigation branch from 3cd2db7 to 6ecbee7 Compare August 5, 2025 19:54
@martinemde
Copy link
Contributor Author

martinemde commented Aug 5, 2025

This is ready (after windows CI finishes)

@koic koic merged commit 5535cd3 into rubocop:master Aug 6, 2025
23 checks passed
@koic
Copy link
Member

koic commented Aug 6, 2025

Thanks!

@martinemde martinemde deleted the martinemde/fix-incorrect-autocorrect-style-safe-navigation branch August 6, 2025 03:53
koic added a commit that referenced this pull request Aug 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants