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

RedundantSafeNavigation does not consider constants may be nil. #12271

Closed
ioquatix opened this issue Oct 13, 2023 · 1 comment · Fixed by #12272
Closed

RedundantSafeNavigation does not consider constants may be nil. #12271

ioquatix opened this issue Oct 13, 2023 · 1 comment · Fixed by #12272
Labels

Comments

@ioquatix
Copy link

ioquatix commented Oct 13, 2023

Expected behavior

FOO = nil

self::FOO&.dig(...)
      ^^^^^
[Correctable] Lint/RedundantSafeNavigation: Redundant safe navigation detected.

Actual behavior

I don't believe we should flag constants as not needing safe navigation.

Steps to reproduce the problem

This is extremely important! Providing us with a reliable way to reproduce
a problem will expedite its solution.

RuboCop version

1.57.0 (using Parser 3.2.2.4, rubocop-ast 1.29.0, running on ruby 3.2.2) [x86_64-linux]
  - rubocop-rspec 2.24.1

Potential Fix

Consider deleting this line?

unless node.receiver.const_type?

@koic koic added the bug label Oct 13, 2023
koic added a commit to koic/rubocop that referenced this issue Oct 13, 2023
…ation`

Fixes rubocop#12271.

This PR fixes a false positive for `Lint/RedundantSafeNavigation`
when using snake case constant receiver.

Camel case is used for naming classes and modules, while snake case is used for all other constants.
This naming conforms with `Naming/ConstantName` cop.

Since constants that turn into classes or modules are normally not `nil`, they will continue to be detected.
However, this PR will update to allow safe navigation for constants in snake case.

This change resolves both issue rubocop/rubocop-rails#1104 and rubocop#12271.
bbatsov pushed a commit that referenced this issue Oct 13, 2023
Fixes #12271.

This PR fixes a false positive for `Lint/RedundantSafeNavigation`
when using snake case constant receiver.

Camel case is used for naming classes and modules, while snake case is used for all other constants.
This naming conforms with `Naming/ConstantName` cop.

Since constants that turn into classes or modules are normally not `nil`, they will continue to be detected.
However, this PR will update to allow safe navigation for constants in snake case.

This change resolves both issue rubocop/rubocop-rails#1104 and #12271.
@ioquatix
Copy link
Author

Thanks everyone for your quick turn around!

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