diff --git a/changelog/fix_false_negatives_for_style_arguments_forwarding_whtn_using_safe_navigation.md b/changelog/fix_false_negatives_for_style_arguments_forwarding_whtn_using_safe_navigation.md new file mode 100644 index 000000000000..4f9038bfb46a --- /dev/null +++ b/changelog/fix_false_negatives_for_style_arguments_forwarding_whtn_using_safe_navigation.md @@ -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][]) diff --git a/lib/rubocop/cop/style/arguments_forwarding.rb b/lib/rubocop/cop/style/arguments_forwarding.rb index 1b35264fbe73..8fdfc0bc2249 100644 --- a/lib/rubocop/cop/style/arguments_forwarding.rb +++ b/lib/rubocop/cop/style/arguments_forwarding.rb @@ -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 diff --git a/spec/rubocop/cop/style/arguments_forwarding_spec.rb b/spec/rubocop/cop/style/arguments_forwarding_spec.rb index 2163e1897f4a..9bf127c73e71 100644 --- a/spec/rubocop/cop/style/arguments_forwarding_spec.rb +++ b/spec/rubocop/cop/style/arguments_forwarding_spec.rb @@ -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) @@ -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)