From e0ec69bb8706acbbd4e1600f382b126e0287e3d0 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sun, 14 Mar 2021 14:22:12 +0900 Subject: [PATCH] Fix a false positive for `Style/RedundantEqualityComparisonBlock` This PR fixes a false positive for `Style/RedundantEqualityComparisonBlock` when using one argument with comma separator in block argument. --- CHANGELOG.md | 1 + .../redundant_equality_comparison_block.rb | 7 ++++++- .../redundant_equality_comparison_block_spec.rb | 14 ++++++++++---- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 584898a884..2e3e3d26a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,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][]) ## 1.10.1 (2021-03-02) diff --git a/lib/rubocop/cop/performance/redundant_equality_comparison_block.rb b/lib/rubocop/cop/performance/redundant_equality_comparison_block.rb index 0e91f808f5..de13587c14 100644 --- a/lib/rubocop/cop/performance/redundant_equality_comparison_block.rb +++ b/lib/rubocop/cop/performance/redundant_equality_comparison_block.rb @@ -35,7 +35,8 @@ class RedundantEqualityComparisonBlock < Base IS_A_METHODS = %i[is_a? kind_of?].freeze def on_block(node) - return unless TARGET_METHODS.include?(node.method_name) && node.arguments.one? + return unless TARGET_METHODS.include?(node.method_name) + return unless one_block_argument?(node.arguments) block_argument = node.arguments.first block_body = node.body @@ -53,6 +54,10 @@ def on_block(node) private + def one_block_argument?(block_arguments) + block_arguments.one? && !block_arguments.source.include?(',') + end + def use_equality_comparison_block?(block_body) block_body.send_type? && COMPARISON_METHODS.include?(block_body.method_name) end diff --git a/spec/rubocop/cop/performance/redundant_equality_comparison_block_spec.rb b/spec/rubocop/cop/performance/redundant_equality_comparison_block_spec.rb index f9a5c4d9e7..a23ec9663d 100644 --- a/spec/rubocop/cop/performance/redundant_equality_comparison_block_spec.rb +++ b/spec/rubocop/cop/performance/redundant_equality_comparison_block_spec.rb @@ -59,13 +59,13 @@ RUBY end - it 'does not register and corrects an offense when using `all?` without `===` comparison block' do + it 'does not register an offense when using `all?` without `===` comparison block' do expect_no_offenses(<<~RUBY) items.all?(other) RUBY end - it 'does not register and corrects an offense when using multiple block arguments' do + it 'does not register an offense when using multiple block arguments' do expect_no_offenses(<<~RUBY) items.all? { |key, _value| key == other } RUBY @@ -83,13 +83,19 @@ RUBY end - it 'does not register and corrects an offense when using block argument is not used as it is' do + it 'does not register an offense when using block argument is not used as it is' do expect_no_offenses(<<~RUBY) items.all? { |item| item.do_something == other } RUBY end - it 'does not register and corrects an offense when using not target methods with `===` comparison block' do + it 'does not register an offense when using one argument with comma separator in block argument' do + expect_no_offenses(<<~RUBY) + items.all? { |item,| item == other } + RUBY + end + + it 'does not register an offense when using not target methods with `===` comparison block' do expect_no_offenses(<<~RUBY) items.do_something { |item| item == other } RUBY