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 #11361] Make Style/MethodDefParentheses aware of anonymous rest and keyword rest args #11362

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 @@
* [#11361](https://github.com/rubocop/rubocop/issues/11361): Make `Style/MethodDefParentheses` aware of Ruby 3.2's anonymous rest and keyword rest arguments. ([@koic][])
15 changes: 11 additions & 4 deletions lib/rubocop/cop/style/method_def_parentheses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ module Style
#
# 1. Endless methods
# 2. Argument lists containing a `forward-arg` (`...`)
# 3. Argument lists containing an anonymous block forwarding (`&`)
# 3. Argument lists containing an anonymous rest arguments forwarding (`*`)
# 4. Argument lists containing an anonymous keyword rest arguments forwarding (`**`)
# 5. Argument lists containing an anonymous block forwarding (`&`)
#
# Removing the parens would be a syntax error here.
#
Expand Down Expand Up @@ -130,9 +132,11 @@ def forced_parentheses?(node)
# Regardless of style, parentheses are necessary for:
# 1. Endless methods
# 2. Argument lists containing a `forward-arg` (`...`)
# 3. Argument lists containing an anonymous block forwarding (`&`)
# 3. Argument lists containing an anonymous rest arguments forwarding (`*`)
# 4. Argument lists containing an anonymous keyword rest arguments forwarding (`**`)
# 5. Argument lists containing an anonymous block forwarding (`&`)
# Removing the parens would be a syntax error here.
node.endless? || node.arguments.any?(&:forward_arg_type?) || anonymous_block_arg?(node)
node.endless? || anonymous_arguments?(node)
end

def require_parentheses?(args)
Expand Down Expand Up @@ -162,7 +166,10 @@ def unwanted_parentheses(args)
end
end

def anonymous_block_arg?(node)
def anonymous_arguments?(node)
return true if node.arguments.any? do |arg|
arg.forward_arg_type? || arg.restarg_type? || arg.kwrestarg_type?
end
return false unless (last_argument = node.arguments.last)

last_argument.blockarg_type? && last_argument.name.nil?
Expand Down
16 changes: 16 additions & 0 deletions spec/rubocop/cop/style/method_def_parentheses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ def foo(&)
end
RUBY
end

it 'requires parens for anonymous rest arguments forwarding', :ruby32 do
expect_no_offenses(<<~RUBY)
def foo(*)
bar(*)
end
RUBY
end

it 'requires parens for anonymous keyword rest arguments forwarding', :ruby32 do
expect_no_offenses(<<~RUBY)
def foo(**)
bar(**)
end
RUBY
end
end

shared_examples 'endless methods' do
Expand Down