Skip to content

Commit

Permalink
[Fix rubocop#10090] Fix a false negative for Style/ArgumentsForwarding
Browse files Browse the repository at this point in the history
Fixes rubocop#10090.

This PR fixes a false negative for `Style/ArgumentsForwarding`
when using only kwrest arg.
  • Loading branch information
koic committed Sep 16, 2021
1 parent d1cf26a commit 1536e98
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
@@ -0,0 +1 @@
* [#10090](https://github.com/rubocop/rubocop/issues/10090): Fix a false negative for `Style/ArgumentsForwarding` when using only kwrest arg. ([@koic][])
15 changes: 13 additions & 2 deletions lib/rubocop/cop/style/arguments_forwarding.rb
Expand Up @@ -30,6 +30,10 @@ module Style
# bar(*args)
# end
#
# def foo(**kwargs)
# bar(**kwargs)
# end
#
# @example AllowOnlyRestArgument: false
# # bad
# # The following code can replace the arguments with `...`,
Expand All @@ -38,6 +42,10 @@ module Style
# bar(*args)
# end
#
# def foo(**kwargs)
# bar(**kwargs)
# end
#
class ArgumentsForwarding < Base
include RangeHelp
extend AutoCorrector
Expand All @@ -49,12 +57,15 @@ class ArgumentsForwarding < Base

# @!method use_rest_arguments?(node)
def_node_matcher :use_rest_arguments?, <<~PATTERN
(args (restarg $_) $...)
(args ({restarg kwrestarg} $_) $...)
PATTERN

# @!method only_rest_arguments?(node, name)
def_node_matcher :only_rest_arguments?, <<~PATTERN
(send _ _ (splat (lvar %1)))
{
(send _ _ (splat (lvar %1)))
(send _ _ (hash (kwsplat (lvar %1))))
}
PATTERN

# @!method forwarding_method_arguments?(node, rest_name, block_name, kwargs_name)
Expand Down
24 changes: 24 additions & 0 deletions spec/rubocop/cop/style/arguments_forwarding_spec.rb
Expand Up @@ -204,6 +204,14 @@ def foo(*args)
end
RUBY
end

it 'does not register an offense when using only kwrest arg' do
expect_no_offenses(<<~RUBY)
def foo(**kwargs)
bar(**kwargs)
end
RUBY
end
end

context 'AllowOnlyRestArgument: false' do
Expand All @@ -224,6 +232,22 @@ def foo(...)
end
RUBY
end

it 'registers an offense when using only kwrest arg' do
expect_offense(<<~RUBY)
def foo(**kwargs)
^^^^^^^^ Use arguments forwarding.
bar(**kwargs)
^^^^^^^^ Use arguments forwarding.
end
RUBY

expect_correction(<<~RUBY)
def foo(...)
bar(...)
end
RUBY
end
end
end
end

0 comments on commit 1536e98

Please sign in to comment.