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 Lint/SafeNavigationConsistency on .nil? check #5840

Closed
michniewicz opened this issue Apr 30, 2018 · 2 comments · Fixed by #5846
Closed

False positive Lint/SafeNavigationConsistency on .nil? check #5840

michniewicz opened this issue Apr 30, 2018 · 2 comments · Fixed by #5846

Comments

@michniewicz
Copy link
Contributor

Expected behavior

Lint/SafeNavigationConsistency should at least not autocorrect .nil? calls.

Actual behavior

Lint/SafeNavigationConsistency replaces .nil? call with safe navigation like &.nil?

Steps to reproduce the problem

say you have a sample code like this:

class Foo
  def bar
    return true if a.nil? || a&.whatever?
  end
end

you get a warning:

app/test.rb:5:20: W: Lint/SafeNavigationConsistency: Ensure that safe navigation is used consistently inside of && and ||. return true if a.nil? || a&.whatever?

if you run rubocop with autocorrect, the code is replaced with:

return true if a&.nil? || a&.whatever?

in that case the code is broken, as if a == nil, the code returns nil instead of true.

RuboCop version

$ rubocop -V
0.55.0 (using Parser 2.5.1.0, running on ruby 2.5.1 x86_64-linux)
@hashicco
Copy link

hashicco commented Apr 30, 2018 via email

@rrosenblum
Copy link
Contributor

@michniewicz thanks for the bug report. This is a bug. Like the other safe navigation cops, I probably need to ignore methods that nil responds to entirely.

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 a pull request may close this issue.

3 participants