Skip to content

Commit

Permalink
Fix false positives for Style/ArgumentsForwarding
Browse files Browse the repository at this point in the history
Fixes #12571 (comment).

This PR fixes false positives for `Style/ArgumentsForwarding`
when rest arguments forwarding to a method in block.
  • Loading branch information
koic authored and bbatsov committed Jan 11, 2024
1 parent 86822c3 commit 730c4a9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12578](https://github.com/rubocop/rubocop/pull/12578): Fix false positives for `Style/ArgumentsForwarding` when rest arguments forwarding to a method in block. ([@koic][])
10 changes: 8 additions & 2 deletions lib/rubocop/cop/style/arguments_forwarding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ def add_post_ruby_32_offenses(def_node, send_classifications, forwardable_args)
rest_arg, kwrest_arg, _block_arg = *forwardable_args

send_classifications.each do |send_node, _c, forward_rest, forward_kwrest|
if forward_rest
if outside_block?(forward_rest)
register_forward_args_offense(def_node.arguments, rest_arg)
register_forward_args_offense(send_node, forward_rest)
end

if forward_kwrest
if outside_block?(forward_kwrest)
register_forward_kwargs_offense(!forward_rest, def_node.arguments, kwrest_arg)
register_forward_kwargs_offense(!forward_rest, send_node, forward_kwrest)
end
Expand Down Expand Up @@ -250,6 +250,12 @@ def redundant_named_arg(arg, config_name, keyword)
redundant_arg_names.include?(arg.source) ? arg : nil
end

def outside_block?(node)
return false unless node

node.each_ancestor(:block, :numblock).none?
end

def register_forward_args_offense(def_arguments_or_send, rest_arg_or_splat)
add_offense(rest_arg_or_splat, message: ARGS_MSG) do |corrector|
add_parens_if_missing(def_arguments_or_send, corrector)
Expand Down
46 changes: 39 additions & 7 deletions spec/rubocop/cop/style/arguments_forwarding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1548,21 +1548,53 @@ def foo(*, **)
RUBY
end

it 'registers an offense when forwarding to a method in block' do
expect_offense(<<~RUBY)
it 'does not register an offense when rest arguments forwarding to a method in block' do
expect_no_offenses(<<~RUBY)
def foo(*args, &block)
^^^^^ Use anonymous positional arguments forwarding (`*`).
do_something do
bar(*args, &block)
^^^^^ Use anonymous positional arguments forwarding (`*`).
end
end
RUBY
end

expect_correction(<<~RUBY)
def foo(*, &block)
it 'does not register an offense when rest arguments forwarding to a method in numbered block' do
expect_no_offenses(<<~RUBY)
def foo(*args, &block)
do_something do
bar(*args, &block)
baz(_1)
end
end
RUBY
end

it 'does not register an offense when keyword rest arguments forwarding to a method in block' do
expect_no_offenses(<<~RUBY)
def foo(**kwargs, &block)
do_something do
bar(**kwargs, &block)
end
end
RUBY
end

it 'does not register an offense when keyword rest arguments forwarding to a method in numbered block' do
expect_no_offenses(<<~RUBY)
def foo(**kwargs, &block)
do_something do
bar(*, &block)
bar(**kwargs, &block)
baz(_1)
end
end
RUBY
end

it 'does not register an offense when rest arguments and keyword rest arguments forwarding to a method in block' do
expect_no_offenses(<<~RUBY)
def foo(*args, **kwargs)
block_method do
bar(*args, **kwargs)
end
end
RUBY
Expand Down

0 comments on commit 730c4a9

Please sign in to comment.