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/Style/RedundantParentheses #10981

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
@@ -0,0 +1 @@
* [#10979](https://github.com/rubocop/rubocop/issues/10979): Fix a false positive for `Style/RedundantParentheses` when using parentheses with pin operator except for variables. ([@Tietew][])
4 changes: 4 additions & 0 deletions lib/rubocop/cop/style/redundant_parentheses.rb
Expand Up @@ -29,6 +29,9 @@ class RedundantParentheses < Base
# @!method rescue?(node)
def_node_matcher :rescue?, '{^resbody ^^resbody}'

# @!method allowed_pin_operator?(node)
def_node_matcher :allowed_pin_operator?, '^(pin (begin !{lvar ivar cvar gvar}))'

# @!method arg_in_call_with_block?(node)
def_node_matcher :arg_in_call_with_block?, '^^(block (send _ _ equal?(%0) ...) ...)'

Expand All @@ -44,6 +47,7 @@ def parens_allowed?(node)
empty_parentheses?(node) ||
first_arg_begins_with_hash_literal?(node) ||
rescue?(node) ||
allowed_pin_operator?(node) ||
allowed_expression?(node)
end

Expand Down
45 changes: 45 additions & 0 deletions spec/rubocop/cop/style/redundant_parentheses_spec.rb
Expand Up @@ -626,4 +626,49 @@ def x
]
RUBY
end

context 'pin operator', :ruby31 do
let(:target_ruby_version) { 3.1 }

shared_examples 'redundant parentheses' do |variable, description|
it "registers an offense and corrects #{description}" do
expect_offense(<<~RUBY, variable: variable)
var = 0
foo in { bar: ^(%{variable}) }
^^{variable}^ Don\x27t use parentheses around a variable.
RUBY

expect_correction(<<~RUBY)
var = 0
foo in { bar: ^#{variable} }
RUBY
end
end

it_behaves_like 'redundant parentheses', 'var', 'a local variable'
it_behaves_like 'redundant parentheses', '@var', 'an instance variable'
it_behaves_like 'redundant parentheses', '@@var', 'a class variable'
it_behaves_like 'redundant parentheses', '$var', 'a global variable'

shared_examples 'allowed parentheses' do |expression, description|
it "accepts parentheses on #{description}" do
expect_no_offenses(<<~RUBY)
var = 0
foo in { bar: ^(#{expression}) }
RUBY
end
end

it_behaves_like 'allowed parentheses', 'meth', 'a function call with no arguments'
it_behaves_like 'allowed parentheses', 'meth(true)', 'a function call with arguments'
it_behaves_like 'allowed parentheses', 'var.to_i', 'a method call on a local variable'
it_behaves_like 'allowed parentheses', '@var.to_i', 'a method call on an instance variable'
it_behaves_like 'allowed parentheses', '@@var.to_i', 'a method call on a class variable'
it_behaves_like 'allowed parentheses', '$var.to_i', 'a method call on a global variable'
it_behaves_like 'allowed parentheses', 'var + 1', 'an expression'
it_behaves_like 'allowed parentheses', '[1, 2]', 'an array literal'
it_behaves_like 'allowed parentheses', '{ baz: 2 }', 'a hash literal'
it_behaves_like 'allowed parentheses', '1..2', 'a range literal'
it_behaves_like 'allowed parentheses', '1', 'an int literal'
end
end