Skip to content

Commit

Permalink
[Fix rubocop#225] Fix a false positive for `Style/RedundantEqualityCo…
Browse files Browse the repository at this point in the history
…mparisonBlock`

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 `===`.
  • Loading branch information
koic committed Mar 17, 2021
1 parent 2d6b745 commit 6aff38e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,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][])
* [#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][])

## 1.10.1 (2021-03-02)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,13 @@ def use_equality_comparison_block?(block_body)
end

def same_block_argument_and_is_a_argument?(block_body, block_argument)
return false unless IS_A_METHODS.include?(block_body.method_name)

block_argument.source == block_body.first_argument.source
if block_body.method?(:===)
block_argument.source != block_body.children[2].source
elsif IS_A_METHODS.include?(block_body.method_name)
block_argument.source == block_body.first_argument.source
else
false
end
end

def new_argument(block_argument, block_body)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@
items.#{method_name}(Klass)
RUBY
end

it "does not register an offense when using `#{method_name}` with `===` comparison block and" \
'block argument is not used as a receiver for `===`' do
expect_no_offenses(<<~RUBY, method_name: method_name)
items.#{method_name} { |item| item === pattern }
RUBY
end
end

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

0 comments on commit 6aff38e

Please sign in to comment.