Skip to content

Commit

Permalink
[Fix rubocop#448] Fix a false positive for `Performance/RedundantBloc…
Browse files Browse the repository at this point in the history
…kCall`

Fixes rubocop#448.

This PR fixes a false positive for `Performance/RedundantBlockCall`
when using `block.call` with block argument.
  • Loading branch information
koic committed Mar 25, 2024
1 parent d4d1875 commit 0d98285
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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][])
2 changes: 2 additions & 0 deletions lib/rubocop/cop/performance/redundant_block_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions spec/rubocop/cop/performance/redundant_block_call_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0d98285

Please sign in to comment.