Skip to content

Commit

Permalink
Fix a false positive for Style/RedundantEqualityComparisonBlock
Browse files Browse the repository at this point in the history
This PR fixes a false positive for `Style/RedundantEqualityComparisonBlock`
when using one argument with comma separator in block argument.
  • Loading branch information
koic committed Mar 14, 2021
1 parent 320b762 commit e0ec69b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

* [#162](https://github.com/rubocop/rubocop-performance/issues/162): Fix a false positive for `Performance/RedundantBlockCall` when an optional block that is overridden by block variable. ([@koic][])
* [#36](https://github.com/rubocop/rubocop-performance/issues/36): Fix a false positive for `Performance/ReverseEach` when `each` is called on `reverse` and using the result value. ([@koic][])
* [#224](https://github.com/rubocop/rubocop-performance/pull/224): Fix a false positive for `Style/RedundantEqualityComparisonBlock` when using one argument with comma separator in block argument. ([@koic][])

## 1.10.1 (2021-03-02)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class RedundantEqualityComparisonBlock < Base
IS_A_METHODS = %i[is_a? kind_of?].freeze

def on_block(node)
return unless TARGET_METHODS.include?(node.method_name) && node.arguments.one?
return unless TARGET_METHODS.include?(node.method_name)
return unless one_block_argument?(node.arguments)

block_argument = node.arguments.first
block_body = node.body
Expand All @@ -53,6 +54,10 @@ def on_block(node)

private

def one_block_argument?(block_arguments)
block_arguments.one? && !block_arguments.source.include?(',')
end

def use_equality_comparison_block?(block_body)
block_body.send_type? && COMPARISON_METHODS.include?(block_body.method_name)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@
RUBY
end

it 'does not register and corrects an offense when using `all?` without `===` comparison block' do
it 'does not register an offense when using `all?` without `===` comparison block' do
expect_no_offenses(<<~RUBY)
items.all?(other)
RUBY
end

it 'does not register and corrects an offense when using multiple block arguments' do
it 'does not register an offense when using multiple block arguments' do
expect_no_offenses(<<~RUBY)
items.all? { |key, _value| key == other }
RUBY
Expand All @@ -83,13 +83,19 @@
RUBY
end

it 'does not register and corrects an offense when using block argument is not used as it is' do
it 'does not register an offense when using block argument is not used as it is' do
expect_no_offenses(<<~RUBY)
items.all? { |item| item.do_something == other }
RUBY
end

it 'does not register and corrects an offense when using not target methods with `===` comparison block' do
it 'does not register an offense when using one argument with comma separator in block argument' do
expect_no_offenses(<<~RUBY)
items.all? { |item,| item == other }
RUBY
end

it 'does not register an offense when using not target methods with `===` comparison block' do
expect_no_offenses(<<~RUBY)
items.do_something { |item| item == other }
RUBY
Expand Down

0 comments on commit e0ec69b

Please sign in to comment.