Skip to content

Commit 6aff38e

Browse files
committed
[Fix rubocop#225] Fix a false positive for Style/RedundantEqualityComparisonBlock
Fixes rubocop#225 This PR fixes a false positive for `Style/RedundantEqualityComparisonBlock` when using `any?` with `===` comparison block and block argument is not used as a receiver for `===`.
1 parent 2d6b745 commit 6aff38e

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* [#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][])
88
* [#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][])
99
* [#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][])
10+
* [#225](https://github.com/rubocop/rubocop-performance/issues/225): Fix a false positive for `Style/RedundantEqualityComparisonBlock` when using `any?` with `===` comparison block and block argument is not used as a receiver for `===`. ([@koic][])
1011

1112
## 1.10.1 (2021-03-02)
1213

lib/rubocop/cop/performance/redundant_equality_comparison_block.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,13 @@ def use_equality_comparison_block?(block_body)
6363
end
6464

6565
def same_block_argument_and_is_a_argument?(block_body, block_argument)
66-
return false unless IS_A_METHODS.include?(block_body.method_name)
67-
68-
block_argument.source == block_body.first_argument.source
66+
if block_body.method?(:===)
67+
block_argument.source != block_body.children[2].source
68+
elsif IS_A_METHODS.include?(block_body.method_name)
69+
block_argument.source == block_body.first_argument.source
70+
else
71+
false
72+
end
6973
end
7074

7175
def new_argument(block_argument, block_body)

spec/rubocop/cop/performance/redundant_equality_comparison_block_spec.rb

+7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@
4646
items.#{method_name}(Klass)
4747
RUBY
4848
end
49+
50+
it "does not register an offense when using `#{method_name}` with `===` comparison block and" \
51+
'block argument is not used as a receiver for `===`' do
52+
expect_no_offenses(<<~RUBY, method_name: method_name)
53+
items.#{method_name} { |item| item === pattern }
54+
RUBY
55+
end
4956
end
5057

5158
it 'registers and corrects an offense when using method chanin and `all?` with `===` comparison block' do

0 commit comments

Comments
 (0)