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 000000000..60d920a89 --- /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 01bae4e16..f9845a2fb 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 4b6d11430..1a7cd624c 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)