diff --git a/changelog/fix_false_negative_for_performance_redundant_block_call.md b/changelog/fix_false_negative_for_performance_redundant_block_call.md new file mode 100644 index 0000000000..df0b697583 --- /dev/null +++ b/changelog/fix_false_negative_for_performance_redundant_block_call.md @@ -0,0 +1 @@ +* [#261](https://github.com/rubocop/rubocop-performance/issues/261): Fix a false negative for `Performance/RedundantBlockCall` when using `block.call` in a class method'. ([@koic][]) diff --git a/lib/rubocop/cop/performance/redundant_block_call.rb b/lib/rubocop/cop/performance/redundant_block_call.rb index 27ad168809..bf5a23dcc8 100644 --- a/lib/rubocop/cop/performance/redundant_block_call.rb +++ b/lib/rubocop/cop/performance/redundant_block_call.rb @@ -55,6 +55,7 @@ def on_def(node) end end end + alias on_defs on_def private diff --git a/spec/rubocop/cop/performance/redundant_block_call_spec.rb b/spec/rubocop/cop/performance/redundant_block_call_spec.rb index 0a3e88ec56..105a2593f5 100644 --- a/spec/rubocop/cop/performance/redundant_block_call_spec.rb +++ b/spec/rubocop/cop/performance/redundant_block_call_spec.rb @@ -31,6 +31,21 @@ def method(&block) RUBY end + it 'registers and corrects an offense when using `block.call` in a class method' do + expect_offense(<<~RUBY) + def self.method(&block) + block.call + ^^^^^^^^^^ Use `yield` instead of `block.call`. + end + RUBY + + expect_correction(<<~RUBY) + def self.method(&block) + yield + end + RUBY + end + it 'registers and autocorrects an offense when `block.call` with arguments' do expect_offense(<<~RUBY) def method(&block)