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

Style/RedundantParentheses false positive on a pin operator with expression #10979

Closed
Tietew opened this issue Aug 30, 2022 · 0 comments · Fixed by #10981
Closed

Style/RedundantParentheses false positive on a pin operator with expression #10979

Tietew opened this issue Aug 30, 2022 · 0 comments · Fixed by #10981
Labels

Comments

@Tietew
Copy link
Contributor

Tietew commented Aug 30, 2022

Ruby 3.1+ allows expression in pattern match (pin operator).
cf. https://bugs.ruby-lang.org/issues/17411 https://bugs.ruby-lang.org/issues/17724

var = '9801'
def meth = '9801'
# pin operator with local/instance/class/global variable
foo in { bar: ^var }
foo in { bar: ^@var}
foo in { bar: ^@@var}
foo in { bar: ^$var}
# allowed but redundant parens (Style/RedundantParentheses detected; true positive)
foo in { bar: ^(var) }
foo in { bar: ^(@var) }
foo in { bar: ^(@@var) }
foo in { bar: ^($var) }
# pin operator with expression: parens are mandatory (THIS ISSUE)
foo in { bar: ^(var.to_i) }
foo in { bar: ^(meth) }
# syntax error
foo in { bar: ^var.to_i }

Expected behavior

No offences shoud be detected:

# pin.rb
var = '9801'
def meth = '9801'
foo in { bar: ^baz }
foo in { bar: ^(baz.to_i) }
foo in { bar: ^(meth) }

Actual behavior

Style/RedundantParentheses detected and autocorrect will remove parens.

pin.rb:5:16: C: [Correctable] Style/RedundantParentheses: Don't use parentheses around a method call.
foo in { bar: ^(var.to_i) }
               ^^^^^^^^^^
pin.rb:6:16: C: [Correctable] Style/RedundantParentheses: Don't use parentheses around a method call.
foo in { bar: ^(meth) }
               ^^^^^^

Steps to reproduce the problem

See above.

RuboCop version

$ [bundle exec] rubocop -V
1.35.1 (using Parser 3.1.2.1, rubocop-ast 1.21.0, running on ruby 3.1.2 x86_64-linux)
@koic koic added the bug label Aug 30, 2022
koic added a commit to koic/rubocop that referenced this issue Aug 30, 2022
…rentheses`

Fixes rubocop#10979.

This PR fixes a false positive for `Style/Style/RedundantParentheses`
when using parentheses in method call with pin operator.
Tietew added a commit to Tietew/rubocop that referenced this issue Aug 30, 2022
…rentheses`

Fixes rubocop#10979.

This PR fixes a false positive for `Style/RedundantParentheses` when using parentheses with pin operator except for variables.
koic added a commit that referenced this issue Aug 30, 2022
…redundant_parentheses

[Fix #10979] Fix a false positive for `Style/Style/RedundantParentheses`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants