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 #10979] Fix a false positive for Style/RedundantParentheses #10980

Closed
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
@@ -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