From 0d982851b1f2467e5e9b72fb96dd3e13bbe585ef Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 22 Mar 2024 21:37:00 +0900 Subject: [PATCH] [Fix #448] Fix a false positive for `Performance/RedundantBlockCall` Fixes #448. This PR fixes a false positive for `Performance/RedundantBlockCall` when using `block.call` with block argument. --- ...itive_for_performance_redundant_block_call.md | 1 + .../cop/performance/redundant_block_call.rb | 2 ++ .../cop/performance/redundant_block_call_spec.rb | 16 ++++++++++++++++ 3 files changed, 19 insertions(+) create mode 100644 changelog/fix_false_positive_for_performance_redundant_block_call.md diff --git a/changelog/fix_false_positive_for_performance_redundant_block_call.md b/changelog/fix_false_positive_for_performance_redundant_block_call.md new file mode 100644 index 0000000000..60d920a89e --- /dev/null +++ b/changelog/fix_false_positive_for_performance_redundant_block_call.md @@ -0,0 +1 @@ +* [#448](https://github.com/rubocop/rubocop-performance/issues/448): Fix a false positive for `Performance/RedundantBlockCall` when using `block.call` with block argument. ([@koic][]) diff --git a/lib/rubocop/cop/performance/redundant_block_call.rb b/lib/rubocop/cop/performance/redundant_block_call.rb index 01bae4e163..f9845a2fb9 100644 --- a/lib/rubocop/cop/performance/redundant_block_call.rb +++ b/lib/rubocop/cop/performance/redundant_block_call.rb @@ -49,6 +49,8 @@ def on_def(node) next unless body calls_to_report(argname, body).each do |blockcall| + next if blockcall.block_literal? + add_offense(blockcall, message: format(MSG, argname: argname)) do |corrector| autocorrect(corrector, blockcall) end diff --git a/spec/rubocop/cop/performance/redundant_block_call_spec.rb b/spec/rubocop/cop/performance/redundant_block_call_spec.rb index 4b6d11430f..1a7cd624c4 100644 --- a/spec/rubocop/cop/performance/redundant_block_call_spec.rb +++ b/spec/rubocop/cop/performance/redundant_block_call_spec.rb @@ -117,6 +117,22 @@ def method(&block) RUBY end + it 'accepts when using `block.call` with block argument' do + expect_no_offenses(<<~RUBY) + def method(&block) + block.call { do_something } + end + RUBY + end + + it 'accepts when using `block.call` with numbered block argument' do + expect_no_offenses(<<~RUBY) + def method(&block) + block.call { _1.do_something } + end + RUBY + end + it 'accepts another block being passed along with other args' do expect_no_offenses(<<~RUBY) def method(&block)