Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positives for Style/ArgumentsForwarding #12578

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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