Skip to content

Commit

Permalink
[Fix rubocop#12380] Make Style/RedundantParentheses aware of lambda…
Browse files Browse the repository at this point in the history
… or proc

Fixes rubocop#12380.

This PR makes `Style/RedundantParentheses` aware of lambda or proc.
  • Loading branch information
koic committed Nov 10, 2023
1 parent 80fce12 commit 2bb0bc7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12380](https://github.com/rubocop/rubocop/issues/12380): Make `Style/RedundantParentheses` aware of lambda or proc. ([@koic][])
5 changes: 3 additions & 2 deletions lib/rubocop/cop/style/redundant_parentheses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,13 @@ def check(begin_node)
check_send(begin_node, node) if node.call_type?
end

# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
def find_offense_message(begin_node, node)
return 'a keyword' if keyword_with_redundant_parentheses?(node)
return 'a literal' if disallowed_literal?(begin_node, node)
return 'a variable' if node.variable?
return 'a constant' if node.const_type?
return 'an expression' if node.lambda_or_proc?
return 'an interpolated expression' if interpolation?(begin_node)

return if begin_node.chained?
Expand All @@ -159,7 +160,7 @@ def find_offense_message(begin_node, node)
'a comparison expression'
end
end
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity

# @!method interpolation?(node)
def_node_matcher :interpolation?, '[^begin ^^dstr]'
Expand Down
4 changes: 4 additions & 0 deletions spec/rubocop/cop/style/redundant_parentheses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@

it_behaves_like 'redundant', '(X)', 'X', 'a constant'

it_behaves_like 'redundant', '(-> { x })', '-> { x }', 'an expression'
it_behaves_like 'redundant', '(lambda { x })', 'lambda { x }', 'an expression'
it_behaves_like 'redundant', '(proc { x })', 'proc { x }', 'an expression'

it_behaves_like 'redundant', '(x)', 'x', 'a method call'
it_behaves_like 'redundant', '(x(1, 2))', 'x(1, 2)', 'a method call'
it_behaves_like 'redundant', '("x".to_sym)', '"x".to_sym', 'a method call'
Expand Down

0 comments on commit 2bb0bc7

Please sign in to comment.