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
Style/SafeNavigation false negatives #4518
Comments
Thanks for the bug report. I just tested both of the examples, and I was able to reproduce the issue. This is a bug. I will start working on a fix for this. This looks it should be easy to fix. P.S. Thank you for the clear examples, links to source code, and links to configuration. |
Awesome. Thank you for the cop and the quick response. |
This might take a little longer to fix than I originally thought. Looking at the code again, we don't appear to be checking for something like |
Status Update: I underestimated the difficulty of this fix. As far as I can tell, this can't be fixed with a small update to the node matcher. This is because as methods are chained, they become nested [1] pry(#<RuboCop::Cop::Style::SafeNavigation>)> node.source
=> "foo && foo.bar"
[2] pry(#<RuboCop::Cop::Style::SafeNavigation>)> node
=> s(:and,
s(:send, nil, :foo),
s(:send,
s(:send, nil, :foo), :bar))
[1] pry(#<RuboCop::Cop::Style::SafeNavigation>)> node.source
=> "foo && foo.bar == 3"
[2] pry(#<RuboCop::Cop::Style::SafeNavigation>)> node
=> s(:and,
s(:send, nil, :foo),
s(:send,
s(:send,
s(:send, nil, :foo), :bar), :==,
s(:int, 3)))
[1] pry(#<RuboCop::Cop::Style::SafeNavigation>)> node.source
=> "foo && foo.bar.baz"
[2] pry(#<RuboCop::Cop::Style::SafeNavigation>)> node
=> s(:and,
s(:send, nil, :foo),
s(:send,
s(:send,
s(:send, nil, :foo), :bar), :baz)) |
I made some really good progress on this yesterday. I was able to get the cop to register an offense for your example. I need to add a few more test cases and refactor some of the code. |
Eagerly awaiting this fix. Thanks for your work and the status updates! |
Style/SafeNavigation seems like it should trigger with code in the following pattern:
This should be suggested to be changed to:
And, this code:
should be changed to:
Is there something about this syntax causing the cop to miss these examples?
Please note from the links above that the project is open source and so easily reviewable. Here is the .rubocop.yml.
Thanks in advance.
Cc @rrosenblum @david-a-wheeler
The text was updated successfully, but these errors were encountered: