Skip to content

Commit

Permalink
Fix false negatives for Style/ArgumentsForwarding
Browse files Browse the repository at this point in the history
It is similar to the process in rubocop#12921.

This PR fixes false negatives for `Style/ArgumentsForwarding`
in method forwaring with safe navigation.
  • Loading branch information
koic committed May 23, 2024
1 parent a909dda commit 2aacab4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12923](https://github.com/rubocop/rubocop/pull/12923): Fix false negatives for `Style/ArgumentsForwarding` when forward target is safe navigation method. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/arguments_forwarding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def on_def(node)

restarg, kwrestarg, blockarg = extract_forwardable_args(node.arguments)
forwardable_args = redundant_forwardable_named_args(restarg, kwrestarg, blockarg)
send_nodes = node.each_descendant(:send, :super).to_a
send_nodes = node.each_descendant(:send, :csend, :super).to_a

send_classifications = classify_send_nodes(
node, send_nodes, non_splat_or_block_pass_lvar_references(node.body), forwardable_args
Expand Down
34 changes: 34 additions & 0 deletions spec/rubocop/cop/style/arguments_forwarding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,22 @@ def method_missing(m, ...)
RUBY
end

it 'registers an offense if an additional positional parameter is present in method forwarding with safe navigation', :ruby30 do
expect_offense(<<~RUBY)
def method_missing(m, *args, **kwargs, &block)
^^^^^^^^^^^^^^^^^^^^^^^ Use shorthand syntax `...` for arguments forwarding.
obj.foo(m, *args, **kwargs, &block)
^^^^^^^^^^^^^^^^^^^^^^^ Use shorthand syntax `...` for arguments forwarding.
end
RUBY

expect_correction(<<~RUBY)
def method_missing(m, ...)
obj.foo(m, ...)
end
RUBY
end

it 'registers an offense if an additional positional parameter is present in `super`', :ruby30 do
expect_offense(<<~RUBY)
def method_missing(m, *args, **kwargs, &block)
Expand Down Expand Up @@ -1481,6 +1497,24 @@ def foo(m, *, &)
RUBY
end

it 'registers an offense when args are forwarded with a positional parameter last in method forwarding with safe navigation' do
expect_offense(<<~RUBY)
def foo(m, *args, &block)
^^^^^ Use anonymous positional arguments forwarding (`*`).
^^^^^^ Use anonymous block arguments forwarding (`&`).
obj&.bar(*args, m, &block)
^^^^^ Use anonymous positional arguments forwarding (`*`).
^^^^^^ Use anonymous block arguments forwarding (`&`).
end
RUBY

expect_correction(<<~RUBY)
def foo(m, *, &)
obj&.bar(*, m, &)
end
RUBY
end

it 'registers an offense when args are forwarded with a positional parameter last in `super`' do
expect_offense(<<~RUBY)
def foo(m, *args, &block)
Expand Down

0 comments on commit 2aacab4

Please sign in to comment.