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 negative for Style/MultipleComparison when not part of conditional #5467

Closed
asherkach opened this issue Jan 15, 2018 · 0 comments · Fixed by #6047
Closed

False negative for Style/MultipleComparison when not part of conditional #5467

asherkach opened this issue Jan 15, 2018 · 0 comments · Fixed by #6047

Comments

@asherkach
Copy link
Contributor

asherkach commented Jan 15, 2018

The Style/MultipleComparison cop fails to trigger if the multiple comparison is not part of a conditional.


Expected behavior

A Style/MultipleComparison violation is triggered for

def foo(x)
  x == 1 || x == 2 || x == 3
end

as this could be written more succinctly as

def foo(x)
  [1, 2, 3].include? x
end

Actual behavior

No Style/MultipleComparison violation is reported.

Steps to reproduce the problem

$ cat bug.rb
def foo(x)
x == 1 || x == 2 || x == 3
end
$ bundle exec rubocop --only Style/MultipleComparison bug.rb
Inspecting 1 file
.

1 file inspected, no offenses detected

RuboCop version

$ rubocop -V
0.52.1 (using Parser 2.4.0.2, running on ruby 2.2.3 x86_64-darwin16)
koic added a commit to koic/rubocop that referenced this issue Jun 25, 2018
Fixes rubocop#5467.

This PR fixes a false negative for `Style/MultipleComparison` when
multiple comparison is not part of a conditional.

The following is a false negative example of issue.

```ruby
def foo(x)
  x == 1 || x == 2 || x == 3
end
```

In addition, this PR will change the offense range as follows.

```diff
  a = "a"
  if a == "a" || a == "b"
- ^^^^^^^^^^^^^^^^^^^^^^^ Avoid comparing a variable with multiple
  items in a conditional, use `Array#include?` instead.
+    ^^^^^^^^^^^^^^^^^^^^ Avoid comparing a variable with multiple
  items in a conditional, use `Array#include?` instead.
    print a
  end
```

Since `if` statements are not subject to replace with `Array#include?`,
this change will result in a strict offense range.
bbatsov pushed a commit that referenced this issue Jun 25, 2018
Fixes #5467.

This PR fixes a false negative for `Style/MultipleComparison` when
multiple comparison is not part of a conditional.

The following is a false negative example of issue.

```ruby
def foo(x)
  x == 1 || x == 2 || x == 3
end
```

In addition, this PR will change the offense range as follows.

```diff
  a = "a"
  if a == "a" || a == "b"
- ^^^^^^^^^^^^^^^^^^^^^^^ Avoid comparing a variable with multiple
  items in a conditional, use `Array#include?` instead.
+    ^^^^^^^^^^^^^^^^^^^^ Avoid comparing a variable with multiple
  items in a conditional, use `Array#include?` instead.
    print a
  end
```

Since `if` statements are not subject to replace with `Array#include?`,
this change will result in a strict offense range.
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.

1 participant