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

Style/NegatedIfElseCondition: False positive on if !! #9050

Closed
pdobb opened this issue Nov 15, 2020 · 0 comments · Fixed by #9051
Closed

Style/NegatedIfElseCondition: False positive on if !! #9050

pdobb opened this issue Nov 15, 2020 · 0 comments · Fixed by #9051
Labels

Comments

@pdobb
Copy link
Contributor

pdobb commented Nov 15, 2020

Using !! isn't negation, it's conversion to Boolean. So this should be valid code:

if !!my_method_or_variable
  "true condition"
else
  "false condition"
end

# Fails with:
Style/NegatedIfElseCondition: Invert the negated condition and swap the if-else branches.
  if !!my_method ...
  ^^^^^^^^^^^^^^

Expected behavior

Don't fail when !! is used in an if/else block. i.e. this should be valid:

if !!my_method_or_variable
  "true condition"
else
  "false condition"
end

Actual behavior

The Style/NegatedIfElseCondition cop fails with:

Style/NegatedIfElseCondition: Invert the negated condition and swap the if-else branches

Steps to reproduce the problem

Add an if/else such as the following to a ruby file and run the Style/NegatedIfElseCondition cop:

if !!my_method
  "a"
else
  "b"
end

RuboCop version

$ rubocop -V
1.3.0 (using Parser 2.7.2.0, rubocop-ast 1.1.1, running on ruby 2.6.6 x86_64-darwin19)
  - rubocop-performance 1.8.1
  - rubocop-rails 2.8.1
  - rubocop-rake 0.5.1
@koic koic added the bug label Nov 15, 2020
koic added a commit to koic/rubocop that referenced this issue Nov 15, 2020
…tion`

Fixes rubocop#9050.

This PR fixes a false positive for `Style/NegatedIfElseCondition`
when `if` with `!!` condition.
koic added a commit that referenced this issue Nov 15, 2020
…else_condition

[Fix #9050] Fix a false positive for `Style/NegatedIfElseCondition`
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