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

Accept method calls for Style/MultipleComparison #8939

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,7 @@

* [#8920](https://github.com/rubocop-hq/rubocop/pull/8920): Remove Capybara's `save_screenshot` from `Lint/Debugger`. ([@ybiquitous][])
* [#8919](https://github.com/rubocop-hq/rubocop/issues/8919): Require RuboCop AST to 1.0.1 or higher. ([@koic][])
* [#8939](https://github.com/rubocop-hq/rubocop/pull/8939): Accept comparisons of multiple method calls for `Style/MultipleComparison`. ([@koic][])

## 1.0.0 (2020-10-21)

Expand Down
2 changes: 1 addition & 1 deletion config/default.yml
Expand Up @@ -1653,7 +1653,7 @@ Lint/MultipleComparison:
Description: "Use `&&` operator to compare multiple values."
Enabled: true
VersionAdded: '0.47'
VersionChanged: '0.77'
VersionChanged: '1.1'

Lint/NestedMethodDefinition:
Description: 'Do not use nested method definitions.'
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/cops_lint.adoc
Expand Up @@ -2275,7 +2275,7 @@ named captures.
| Yes
| Yes
| 0.47
| 0.77
| 1.1
|===

In math and Python, we can use `x < y < z` style comparison to compare
Expand Down
2 changes: 2 additions & 0 deletions docs/modules/ROOT/pages/cops_style.adoc
Expand Up @@ -6017,6 +6017,7 @@ end

This cop checks against comparing a variable with multiple items, where
`Array#include?` could be used instead to avoid code repetition.
It accepts comparisons of multiple method calls to avoid unnecessary method calls.

=== Examples

Expand All @@ -6029,6 +6030,7 @@ foo if a == 'a' || a == 'b' || a == 'c'
# good
a = 'a'
foo if ['a', 'b', 'c'].include?(a)
foo if a == b.lightweight || a == b.heavyweight
----

== Style/MutableConstant
Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/layout/extra_spacing.rb
Expand Up @@ -56,8 +56,7 @@ def aligned_locations(locs)
aligned = Set[locs.first.line, locs.last.line]
locs.each_cons(3) do |before, loc, after|
col = loc.column
aligned << loc.line if col == before.column || # rubocop:disable Style/MultipleComparison
col == after.column
aligned << loc.line if col == before.column || col == after.column
end
aligned
end
Expand Down
6 changes: 4 additions & 2 deletions lib/rubocop/cop/style/multiple_comparison.rb
Expand Up @@ -5,6 +5,7 @@ module Cop
module Style
# This cop checks against comparing a variable with multiple items, where
# `Array#include?` could be used instead to avoid code repetition.
# It accepts comparisons of multiple method calls to avoid unnecessary method calls.
#
# @example
# # bad
Expand All @@ -14,6 +15,7 @@ module Style
# # good
# a = 'a'
# foo if ['a', 'b', 'c'].include?(a)
# foo if a == b.lightweight || a == b.heavyweight
class MultipleComparison < Base
MSG = 'Avoid comparing a variable with multiple items ' \
'in a conditional, use `Array#include?` instead.'
Expand All @@ -31,8 +33,8 @@ def on_or(node)

def_node_matcher :simple_double_comparison?, '(send $lvar :== $lvar)'
def_node_matcher :simple_comparison?, <<~PATTERN
{(send $lvar :== _)
(send _ :== $lvar)}
{(send $lvar :== !send)
(send !send :== $lvar)}
PATTERN

def nested_variable_comparison?(node)
Expand Down
9 changes: 9 additions & 0 deletions spec/rubocop/cop/style/multiple_comparison_spec.rb
Expand Up @@ -66,6 +66,15 @@ def foo(x)
RUBY
end

it 'does not register an offense and corrects when using multiple method calls' do
expect_no_offenses(<<~RUBY)
col = loc.column
if col == before.column || col == after.column
do_something
end
RUBY
end

it 'does not register an offense for comparing multiple literal strings' do
expect_no_offenses(<<~RUBY)
if "a" == "a" || "a" == "c"
Expand Down