Skip to content

Commit

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

Fixes rubocop#255.

This PR fixes a false positive for `Style/RedundantEqualityComparisonBlock`
when using block argument is used for an argument of operand.
  • Loading branch information
koic committed Aug 10, 2021
1 parent aa402de commit 829e215
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#255](https://github.com/rubocop/rubocop-performance/issues/255): Fix a false positive for `Style/RedundantEqualityComparisonBlock` when using block argument is used for an argument of operand. ([@koic][])

## 1.11.4 (2021-07-07)

### Bug fixes
Expand Down
19 changes: 17 additions & 2 deletions lib/rubocop/cop/performance/redundant_equality_comparison_block.rb
Expand Up @@ -74,12 +74,27 @@ def same_block_argument_and_is_a_argument?(block_body, block_argument)

def new_argument(block_argument, block_body)
if block_argument.source == block_body.receiver.source
block_body.first_argument.source
rhs = block_body.first_argument
return if use_block_argument_in_method_argument_of_operand?(block_argument, rhs)

rhs.source
elsif block_argument.source == block_body.first_argument.source
block_body.receiver.source
lhs = block_body.receiver
return if use_block_argument_in_method_argument_of_operand?(block_argument, lhs)

lhs.source
end
end

def use_block_argument_in_method_argument_of_operand?(block_argument, operand)
return false unless operand.send_type?

arguments = operand.arguments
arguments.inject(arguments.map(&:source)) do |operand_sources, argument|
operand_sources + argument.each_descendant(:lvar).map(&:source)
end.any?(block_argument.source)
end

def offense_range(node)
node.send_node.loc.selector.join(node.source_range.end)
end
Expand Down
Expand Up @@ -106,4 +106,28 @@
items.do_something { |item| item == other }
RUBY
end

it 'does not register an offense when using block argument is used for an argument of RHS operand' do
expect_no_offenses(<<~RUBY)
items.any? { |item| item == do_something(item) }
RUBY
end

it 'does not register an offense when using block argument is used for a argument of LHS operand' do
expect_no_offenses(<<~RUBY)
items.any? { |item| do_something(item) == item }
RUBY
end

it 'does not register an offense when using block argument is used for a nested argument of RHS operand' do
expect_no_offenses(<<~RUBY)
items.any? { |item| item == do_something[0..item] }
RUBY
end

it 'does not register an offense when using block argument is used for a nested argument of LHS operand' do
expect_no_offenses(<<~RUBY)
items.any? { |item| do_something[0..item] == item }
RUBY
end
end

0 comments on commit 829e215

Please sign in to comment.