diff --git a/changelog/fix_error_for_redundant_equality_comparison_block.md b/changelog/fix_error_for_redundant_equality_comparison_block.md new file mode 100644 index 0000000000..0ec6898e64 --- /dev/null +++ b/changelog/fix_error_for_redundant_equality_comparison_block.md @@ -0,0 +1 @@ +* [#452](https://github.com/rubocop/rubocop-performance/pull/452): Fix an error for `Performance/RedundantEqualityComparisonBlock` when the block is empty. ([@earlopain][]) diff --git a/lib/rubocop/cop/performance/redundant_equality_comparison_block.rb b/lib/rubocop/cop/performance/redundant_equality_comparison_block.rb index b01887a887..1cdbcdd991 100644 --- a/lib/rubocop/cop/performance/redundant_equality_comparison_block.rb +++ b/lib/rubocop/cop/performance/redundant_equality_comparison_block.rb @@ -64,7 +64,7 @@ def on_block(node) return unless one_block_argument?(node.arguments) block_argument = node.first_argument - block_body = node.body + return unless (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)) 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 01346760f6..0bb544e739 100644 --- a/spec/rubocop/cop/performance/redundant_equality_comparison_block_spec.rb +++ b/spec/rubocop/cop/performance/redundant_equality_comparison_block_spec.rb @@ -181,4 +181,16 @@ items.any? { |item| do_something[0..item] == item } RUBY end + + it 'does not register an offense when the block is empty' do + expect_no_offenses(<<~RUBY) + items.any? { |item| } + RUBY + end + + it 'does not register an offense when the block takes no argument' do + expect_no_offenses(<<~RUBY) + items.any? { } + RUBY + end end