Skip to content

Commit

Permalink
[Fix #11361] Make Style/MethodDefParentheses aware of anonymous res…
Browse files Browse the repository at this point in the history
…t and keyword rest args

Fixes #11361

This PR fixes a false positive for `Style/MethodDefParentheses` when using `EnforcedStyle: require_no_parentheses`
and Ruby 3.2's anonymous rest and keyword rest arguments.
  • Loading branch information
koic authored and bbatsov committed Dec 31, 2022
1 parent 952c2df commit abbddba
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
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

0 comments on commit abbddba

Please sign in to comment.