Skip to content

Commit

Permalink
[Fix rubocop#12875] Fix false positive for `Style/ArgumentsForwarding…
Browse files Browse the repository at this point in the history
…` when argument is used inside a block.
  • Loading branch information
dvandersluis committed Oct 9, 2024
1 parent e57bc29 commit e4780da
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12875](https://github.com/rubocop/rubocop/issues/12875): Fix false positive for `Style/ArgumentsForwarding` when argument is used inside a block. ([@dvandersluis][])
7 changes: 7 additions & 0 deletions lib/rubocop/cop/style/arguments_forwarding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ def add_forward_all_offenses(node, send_classifications, forwardable_args)
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def add_post_ruby_32_offenses(def_node, send_classifications, forwardable_args)
return unless use_anonymous_forwarding?
return if send_inside_block?(send_classifications)

rest_arg, kwrest_arg, block_arg = *forwardable_args

Expand Down Expand Up @@ -356,6 +357,12 @@ def use_anonymous_forwarding?
cop_config.fetch('UseAnonymousForwarding', false)
end

def send_inside_block?(send_classifications)
send_classifications.any? do |send_node, *|
send_node.each_ancestor(:block, :numblock).any?
end
end

def add_parens_if_missing(node, corrector)
return if parentheses?(node)
return if node.send_type? && node.method?(:[])
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/style/arguments_forwarding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2508,6 +2508,21 @@ def foo(*args, &block)
RUBY
end

context 'UseAnonymousForwarding: true' do
let(:cop_config) { { 'UseAnonymousForwarding' => true } }

it 'does not register an offense when a forwarded argument is used within a block' do
expect_no_offenses(<<~RUBY)
def foo(*args)
bar(*args)
baz do |x|
x.foo(*args)
end
end
RUBY
end
end

context 'UseAnonymousForwarding: false' do
let(:cop_config) { { 'UseAnonymousForwarding' => false } }

Expand Down

0 comments on commit e4780da

Please sign in to comment.