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

Add AllowMethodComparison option for Lint/MultipleComparison #8953

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 @@ -10,6 +10,7 @@
* [#7549](https://github.com/rubocop-hq/rubocop/issues/7549): Add new `Style/ArgumentsForwarding` cop. ([@koic][])
* [#8859](https://github.com/rubocop-hq/rubocop/issues/8859): Add new `Lint/UnmodifiedReduceAccumulator` cop. ([@dvandersluis][])
* [#8951](https://github.com/rubocop-hq/rubocop/pull/8951): Support auto-correction for `Style/MultipleComparison`. ([@koic][])
* [#8953](https://github.com/rubocop-hq/rubocop/pull/8953): Add `AllowMethodComparison` option for `Lint/MultipleComparison`. ([@koic][])

### Bug fixes

Expand Down
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -1659,6 +1659,7 @@ Lint/MultipleComparison:
Enabled: true
VersionAdded: '0.47'
VersionChanged: '1.1'
AllowMethodComparison: true

Lint/NestedMethodDefinition:
Description: 'Do not use nested method definitions.'
Expand Down
10 changes: 10 additions & 0 deletions docs/modules/ROOT/pages/cops_lint.adoc
Expand Up @@ -2338,6 +2338,16 @@ x < y && y < z
10 <= x && x <= 20
----

=== Configurable attributes

|===
| Name | Default value | Configurable values

| AllowMethodComparison
| `true`
| Boolean
|===

== Lint/NestedMethodDefinition

|===
Expand Down
22 changes: 21 additions & 1 deletion docs/modules/ROOT/pages/cops_style.adoc
Expand Up @@ -6017,7 +6017,8 @@ 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.
It accepts comparisons of multiple method calls to avoid unnecessary method calls
by default. It can be configured by `AllowMethodComparison` option.

=== Examples

Expand All @@ -6033,6 +6034,25 @@ foo if ['a', 'b', 'c'].include?(a)
foo if a == b.lightweight || a == b.heavyweight
----

==== AllowMethodComparison: true (default)

[source,ruby]
----
# good
foo if a == b.lightweight || a == b.heavyweight
----

==== AllowMethodComparison: false

[source,ruby]
----
# bad
foo if a == b.lightweight || a == b.heavyweight

# good
foo if [b.lightweight, b.heavyweight].include?(a)
----

== Style/MutableConstant

|===
Expand Down
24 changes: 21 additions & 3 deletions lib/rubocop/cop/style/multiple_comparison.rb
Expand Up @@ -5,7 +5,8 @@ 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.
# It accepts comparisons of multiple method calls to avoid unnecessary method calls
# by default. It can be configured by `AllowMethodComparison` option.
#
# @example
# # bad
Expand All @@ -16,6 +17,17 @@ module Style
# a = 'a'
# foo if ['a', 'b', 'c'].include?(a)
# foo if a == b.lightweight || a == b.heavyweight
#
# @example AllowMethodComparison: true (default)
# # good
# foo if a == b.lightweight || a == b.heavyweight
#
# @example AllowMethodComparison: false
# # bad
# foo if a == b.lightweight || a == b.heavyweight
#
# # good
# foo if [b.lightweight, b.heavyweight].include?(a)
class MultipleComparison < Base
extend AutoCorrector

Expand Down Expand Up @@ -44,10 +56,10 @@ def on_or(node)

def_node_matcher :simple_double_comparison?, '(send $lvar :== $lvar)'
def_node_matcher :simple_comparison_lhs?, <<~PATTERN
(send $lvar :== $!send)
(send $lvar :== $_)
PATTERN
def_node_matcher :simple_comparison_rhs?, <<~PATTERN
(send $!send :== $lvar)
(send $_ :== $lvar)
PATTERN

def nested_variable_comparison?(node)
Expand All @@ -71,6 +83,8 @@ def variables_in_simple_node(node)
return [variable_name(var1), variable_name(var2)]
end
if (var, obj = simple_comparison_lhs?(node)) || (obj, var = simple_comparison_rhs?(node))
return [] if allow_method_comparison? && obj.send_type?

@compared_elements << obj.source
return [variable_name(var)]
end
Expand Down Expand Up @@ -103,6 +117,10 @@ def root_of_or_node(or_node)
or_node
end
end

def allow_method_comparison?
cop_config.fetch('AllowMethodComparison', true)
end
end
end
end
Expand Down
47 changes: 35 additions & 12 deletions spec/rubocop/cop/style/multiple_comparison_spec.rb
@@ -1,10 +1,8 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Style::MultipleComparison do
RSpec.describe RuboCop::Cop::Style::MultipleComparison, :config do
subject(:cop) { described_class.new(config) }

let(:config) { RuboCop::Config.new }

it 'does not register an offense for comparing an lvar' do
expect_no_offenses(<<~RUBY)
a = "a"
Expand Down Expand Up @@ -100,15 +98,6 @@ 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 Expand Up @@ -174,4 +163,38 @@ def foo(x)
end
RUBY
end

context 'when `AllowMethodComparison: true`' do
let(:cop_config) { { 'AllowMethodComparison' => true } }

it 'does not register an offense 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
end

context 'when `AllowMethodComparison: false`' do
let(:cop_config) { { 'AllowMethodComparison' => false } }

it 'registers an offense and corrects when using multiple method calls' do
expect_offense(<<~RUBY)
col = loc.column
if col == before.column || col == after.column
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid comparing a variable with multiple items in a conditional, use `Array#include?` instead.
do_something
end
RUBY

expect_correction(<<~RUBY)
col = loc.column
if [before.column, after.column].include?(col)
do_something
end
RUBY
end
end
end