Skip to content

Commit

Permalink
[Fix rubocop#217] Fix a false positive for `Performance/RedundantEqua…
Browse files Browse the repository at this point in the history
…lityComparisonBlock`

Fixes rubocop#217.

This PR fixes a false positive for `Performance/RedundantEqualityComparisonBlock`
when using block argument is used for an argument of `is_a`.
  • Loading branch information
koic committed Mar 1, 2021
1 parent ac6f67b commit f5fbc22
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 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 @@

* [#214](https://github.com/rubocop/rubocop-performance/issues/214): Fix a false positive for `Performance/RedundantEqualityComparisonBlock` when using multiple block arguments. ([@koic][])
* [#216](https://github.com/rubocop/rubocop-performance/issues/216): Fix a false positive for `Performance/RedundantSplitRegexpArgument` when using split method with ignore case regexp option. ([@koic][])
* [#217](https://github.com/rubocop/rubocop-performance/issues/217): Fix a false positive for `Performance/RedundantEqualityComparisonBlock` when using block argument is used for an argument of `is_a`. ([@koic][])

## 1.10.0 (2021-03-01)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def on_block(node)
block_argument = node.arguments.first
block_body = node.body
return unless use_equality_comparison_block?(block_body)
return if same_block_argument_and_is_a_argument?(block_body, block_argument)
return unless (new_argument = new_argument(block_argument, block_body))

range = offense_range(node)
Expand All @@ -55,6 +56,12 @@ def use_equality_comparison_block?(block_body)
block_body.send_type? && COMPARISON_METHODS.include?(block_body.method_name)
end

def same_block_argument_and_is_a_argument?(block_body, block_argument)
return false unless %i[is_a? kind_of?].include?(block_body.method_name)

block_argument.source == block_body.first_argument.source
end

def new_argument(block_argument, block_body)
if block_argument.source == block_body.receiver.source
block_body.first_argument.source
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@
RUBY
end

it 'does not register an offense when using block argument is used for an argument of `is_a`' do
expect_no_offenses(<<~RUBY)
klasses.all? { |klass| item.is_a?(klass) }
RUBY
end

it 'does not register an offense when using block argument is used for an argument of `kind_of?`' do
expect_no_offenses(<<~RUBY)
klasses.all? { |klass| item.kind_of?(klass) }
RUBY
end

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

0 comments on commit f5fbc22

Please sign in to comment.