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

False positive for Style/SafeNavigation when safe guarding arithmetic operation or assignment #5089

Closed
tiagotex opened this issue Nov 22, 2017 · 3 comments

Comments

@tiagotex
Copy link
Contributor

Style/SafeNavigation returns false positives and tries to autocorrect when safeguarding a arithmetic operation or assignment on a chained method call.


Expected behavior

Rubocop shouldn't consider this an offense, since we don't want to do the operation when object is null.

Actual behavior

Rubocop removes the safe guard and uses the safe operator.

Steps to reproduce the problem

foo.baz + bar if foo
foo.baz = bar if foo

Rubocop tries to fix to:

foo&.baz + bar
foo&.baz = bar

RuboCop version

$ rubocop -V
0.51.0 (using Parser 2.4.0.0, running on ruby 2.4.2 x86_64-darwin16)
@Drenmi
Copy link
Collaborator

Drenmi commented Nov 22, 2017

Same issue as #5079. The problem seems to be that the cop is not checking that the last method in a method chain is a "dot-less" method (comparisons, arithmetics, hash access, etc.)

@tiagotex
Copy link
Contributor Author

I'm working on a PR to fix it.

tiagotex added a commit to tiagotex/rubocop that referenced this issue Nov 22, 2017
Style/SafeNavigation returns false positives and tries to autocorrect when safeguarding a arithmetic operation or assignment on a chained method call.
@tiagotex
Copy link
Contributor Author

I fixed the problems described in this issue, but not the problem referenced in #5079, I might take a stab at it later.

bbatsov pushed a commit that referenced this issue Nov 22, 2017
Style/SafeNavigation returns false positives and tries to autocorrect when safeguarding a arithmetic operation or assignment on a chained method call.
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

No branches or pull requests

2 participants