Skip to content

Commit

Permalink
[Fix rubocop#10979] Fix a false positive for `Style/Style/RedundantPa…
Browse files Browse the repository at this point in the history
…rentheses`

Fixes rubocop#10979.

This PR fixes a false positive for `Style/Style/RedundantParentheses`
when using parentheses in method call with pin operator.
  • Loading branch information
koic committed Aug 30, 2022
1 parent a63fd6d commit 416d600
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
@@ -0,0 +1 @@
* [#10979](https://github.com/rubocop/rubocop/issues/10979): Fix a false positive for `Style/Style/RedundantParentheses` when using parentheses in method call with pin operator. ([@koic][])
7 changes: 6 additions & 1 deletion lib/rubocop/cop/style/redundant_parentheses.rb
Expand Up @@ -58,7 +58,8 @@ def allowed_expression?(node)
allowed_ancestor?(node) ||
allowed_method_call?(node) ||
allowed_multiple_expression?(node) ||
allowed_ternary?(node)
allowed_ternary?(node) ||
allowed_pin_operator?(node)
end

def allowed_ancestor?(node)
Expand Down Expand Up @@ -86,6 +87,10 @@ def allowed_ternary?(node)
node.parent.ternary? && ternary_parentheses_required?
end

def allowed_pin_operator?(node)
node&.parent&.pin_type? && node.children.first.call_type?
end

def ternary_parentheses_required?
config = @config.for_cop('Style/TernaryParentheses')
allowed_styles = %w[require_parentheses require_parentheses_when_complex]
Expand Down
29 changes: 29 additions & 0 deletions spec/rubocop/cop/style/redundant_parentheses_spec.rb
Expand Up @@ -626,4 +626,33 @@ def x
]
RUBY
end

context 'pin operator', :ruby31 do
it 'does not register an offense when using parentheses in method call with pin operator' do
expect_no_offenses(<<~RUBY)
foo in { bar: ^(baz.to_i) }
RUBY
end

it 'does not register an offense when using parentheses in safe navigation method call with pin operator' do
expect_no_offenses(<<~RUBY)
foo in { bar: ^(baz&.to_i) }
RUBY
end

it 'registers an offense when using parentheses in lvar with pin operator' do
expect_offense(<<~RUBY)
baz = 42
foo in { bar: ^(baz) }
^^^^^ Don't use parentheses around a variable.
RUBY

expect_correction(<<~RUBY)
baz = 42
foo in { bar: ^baz }
RUBY
end
end
end

0 comments on commit 416d600

Please sign in to comment.