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 in Style/NegativedIf/Style/NegatedUnless #11558

Closed
mlwyatt opened this issue Feb 8, 2023 · 1 comment
Closed

False positive in Style/NegativedIf/Style/NegatedUnless #11558

mlwyatt opened this issue Feb 8, 2023 · 1 comment

Comments

@mlwyatt
Copy link

mlwyatt commented Feb 8, 2023

I'm not sure if this is a bug with Style/NegatedIf/Style/NegatedUnless or this is just how they were designed to work and a new cop/config flag would need added. I like that if !/unless ! gets replaced with the respective unless/if, but I would also like if x != y/unless x != y to also get replaced with the respective unless x == y/if x == y.


Expected behavior

# Bad
if x != y
  # code
end
unless x != y
  # code
end

# Good
unless x == y
  # code
end
if x == y
  # code
end

Actual behavior

Rubocop didn't complain about any of the following

if x != y
  # code
end

unless x != y
  # code
end

unless x == y
  # code
end

if x == y
  # code
end

Steps to reproduce the problem

Create a file with the above conditions and see no rubocop errors from any of them.
Minimal Config:

AllCops:
  NewCops: disable
  TargetRubyVersion: 3.0.1
  ActiveSupportExtensionsEnabled: true

Style/NegatedIf:
  Enabled: true
  EnforcedStyle: both
Style/NegatedUnless:
  Enabled: true
  EnforcedStyle: both

RuboCop version

1.43.0 (using Parser 3.2.0.0, rubocop-ast 1.24.1, running on ruby 3.0.1) [aarch64-linux]
  - rubocop-rails 2.17.4

I tested again with the 1.45.1 release

1.45.1 (using Parser 3.2.0.0, rubocop-ast 1.24.1, running on ruby 3.0.1) [aarch64-linux]
  - rubocop-rails 2.17.4
@koic
Copy link
Member

koic commented Feb 9, 2023

This would be false negatives, not false positives. The Style/Negated* cops is for detecting unary operator like !foo, not binary operator like foo != bar. Also if foo != bar is allowed because it might be intended. On the other hand, double negation such as unless foo != bar can be detected by Style/InvertibleUnlessCondition cop. The cop is disabled by default, so enable it explicitly if needed.
#11432

@koic koic closed this as completed Feb 9, 2023
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