Skip to content

Commit f5fbc22

Browse files
committed
[Fix rubocop#217] Fix a false positive for Performance/RedundantEqualityComparisonBlock
Fixes rubocop#217. This PR fixes a false positive for `Performance/RedundantEqualityComparisonBlock` when using block argument is used for an argument of `is_a`.
1 parent ac6f67b commit f5fbc22

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
* [#214](https://github.com/rubocop/rubocop-performance/issues/214): Fix a false positive for `Performance/RedundantEqualityComparisonBlock` when using multiple block arguments. ([@koic][])
88
* [#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][])
9+
* [#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][])
910

1011
## 1.10.0 (2021-03-01)
1112

lib/rubocop/cop/performance/redundant_equality_comparison_block.rb

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def on_block(node)
3939
block_argument = node.arguments.first
4040
block_body = node.body
4141
return unless use_equality_comparison_block?(block_body)
42+
return if same_block_argument_and_is_a_argument?(block_body, block_argument)
4243
return unless (new_argument = new_argument(block_argument, block_body))
4344

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

59+
def same_block_argument_and_is_a_argument?(block_body, block_argument)
60+
return false unless %i[is_a? kind_of?].include?(block_body.method_name)
61+
62+
block_argument.source == block_body.first_argument.source
63+
end
64+
5865
def new_argument(block_argument, block_body)
5966
if block_argument.source == block_body.receiver.source
6067
block_body.first_argument.source

spec/rubocop/cop/performance/redundant_equality_comparison_block_spec.rb

+12
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@
7171
RUBY
7272
end
7373

74+
it 'does not register an offense when using block argument is used for an argument of `is_a`' do
75+
expect_no_offenses(<<~RUBY)
76+
klasses.all? { |klass| item.is_a?(klass) }
77+
RUBY
78+
end
79+
80+
it 'does not register an offense when using block argument is used for an argument of `kind_of?`' do
81+
expect_no_offenses(<<~RUBY)
82+
klasses.all? { |klass| item.kind_of?(klass) }
83+
RUBY
84+
end
85+
7486
it 'does not register and corrects an offense when using block argument is not used as it is' do
7587
expect_no_offenses(<<~RUBY)
7688
items.all? { |item| item.do_something == other }

0 commit comments

Comments
 (0)