Skip to content

Commit

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

This PR fixes false positives for `Style/ArgumentsForwarding`
when rest arguments forwarding to a method in block.
  • Loading branch information
koic committed Dec 28, 2023
1 parent 229f205 commit daaf944
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12577](https://github.com/rubocop/rubocop/pull/12577): Fix false positives for `Style/ArgumentsForwarding` when rest arguments forwarding to a method in block. ([@koic][])
6 changes: 4 additions & 2 deletions lib/rubocop/cop/style/arguments_forwarding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,23 +180,25 @@ def add_forward_all_offenses(node, send_classifications, forwardable_args)
register_forward_all_offense(node, node.arguments, rest_arg)
end

# rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
def add_post_ruby_32_offenses(def_node, send_classifications, forwardable_args)
return unless use_anonymous_forwarding?

rest_arg, kwrest_arg, _block_arg = *forwardable_args

send_classifications.each do |send_node, _c, forward_rest, forward_kwrest|
if forward_rest
if forward_rest&.each_ancestor(:block, :numblock)&.none?
register_forward_args_offense(def_node.arguments, rest_arg)
register_forward_args_offense(send_node, forward_rest)
end

if forward_kwrest
if forward_kwrest&.each_ancestor(:block, :numblock)&.none?
register_forward_kwargs_offense(!forward_rest, def_node.arguments, kwrest_arg)
register_forward_kwargs_offense(!forward_rest, send_node, forward_kwrest)
end
end
end
# rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity

def non_splat_or_block_pass_lvar_references(body)
body.each_descendant(:lvar, :lvasgn).filter_map do |lvar|
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 daaf944

Please sign in to comment.