Skip to content

Commit 3088b19

Browse files
committed
[Fix #359] Fix a false positive for Performance/RedundantEqualityComparisonBlock
Fixes #359. This PR fixes a false positive for `Performance/RedundantEqualityComparisonBlock` when the block variable is used on both sides of `==`.
1 parent 4e7233d commit 3088b19

3 files changed

Lines changed: 8 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#359](https://github.com/rubocop/rubocop-performance/issues/359): Fix a false positive for `Performance/RedundantEqualityComparisonBlock` when the block variable is used on both sides of `==`. ([@koic][])

lib/rubocop/cop/performance/redundant_equality_comparison_block.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def same_block_argument_and_is_a_argument?(block_body, block_argument)
9797
elsif IS_A_METHODS.include?(block_body.method_name)
9898
block_argument.source == block_body.first_argument.source
9999
else
100-
false
100+
block_body.receiver.source == block_body.first_argument.receiver&.source
101101
end
102102
end
103103

spec/rubocop/cop/performance/redundant_equality_comparison_block_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@
102102
RUBY
103103
end
104104

105+
it 'does not register an offense when the block variable is used on both sides of `==`' do
106+
expect_no_offenses(<<~RUBY)
107+
items.all? { |item| item == item.do_something }
108+
RUBY
109+
end
110+
105111
it 'does not register an offense when using not target methods with `===` comparison block' do
106112
expect_no_offenses(<<~RUBY)
107113
items.do_something { |item| item == other }

0 commit comments

Comments
 (0)