From d85f59c5ed0fe543ce9052df5c1427e8e6b4f55d Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 23 Oct 2021 03:28:14 +0900 Subject: [PATCH] [Fix #261] Fix a false negative for `Performance/RedundantBlockCall` Fixes #261. This PR fixes a false negative for `Performance/RedundantBlockCall` when using `block.call` in a class method'. --- ...gative_for_performance_redundant_block_call.md | 1 + .../cop/performance/redundant_block_call.rb | 1 + .../cop/performance/redundant_block_call_spec.rb | 15 +++++++++++++++ 3 files changed, 17 insertions(+) create mode 100644 changelog/fix_false_negative_for_performance_redundant_block_call.md 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)