Skip to content

Commit

Permalink
Merge pull request #8939 from koic/accept_method_calls_for_style_mult…
Browse files Browse the repository at this point in the history
…iple_comparison

Accept method calls for `Style/MultipleComparison`
  • Loading branch information
koic committed Oct 27, 2020
2 parents 51cda39 + dbc2714 commit d459e2d
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,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 @@ -1658,7 +1658,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 @@ -2312,7 +2312,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

0 comments on commit d459e2d

Please sign in to comment.