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

Lint/RequireParentheses and Style/TernaryParentheses contradicting each other #11320

Closed
tjschuck opened this issue Dec 22, 2022 · 0 comments · Fixed by #11321
Closed

Lint/RequireParentheses and Style/TernaryParentheses contradicting each other #11320

tjschuck opened this issue Dec 22, 2022 · 0 comments · Fixed by #11321
Labels

Comments

@tjschuck
Copy link
Contributor

Sample code:

self.foo = bar && baz ? 'qux' : 'quux'

Running Rubocop against that, you get:

example.rb:1:1: W: Lint/RequireParentheses: Use parentheses in the method call to avoid confusion about precedence.
self.foo = bar && baz ? 'qux' : 'quux'
^^^^^^^^^^^^^^^^^^^^^

However, correcting it to:

self.foo = (bar && baz) ? 'qux' : 'quux'

Rubocop now reports:

example.rb:1:12: C: [Correctable] Style/TernaryParentheses: Omit parentheses for ternary conditions.
self.foo = (bar && baz) ? 'qux' : 'quux'
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Correcting back-and-forth just keeps you in this loop forever.

Interesting wildcard: the first offense happens when the sample code is self.foo =, but not when it's foo =, which makes me think the first offense is a bug.


Expected behavior

Based on the docs for Lint/RequireParentheses, it seems like it's only supposed to apply when predicate methods are used, which isn't happening in the sample code, so it probably shouldn't apply here.

Actual behavior

See above.

Steps to reproduce the problem

See above.

RuboCop version

1.41.1 (using Parser 3.1.3.0, rubocop-ast 1.24.0, running on ruby 3.0.4) [x86_64-darwin21]
koic added a commit to koic/rubocop that referenced this issue Dec 23, 2022
Fixes rubocop#11320.

This PR fixes a false positive for `Lint/RequireParentheses`
when assigning ternary operator.
@koic koic added the bug label Dec 23, 2022
bbatsov pushed a commit that referenced this issue Dec 23, 2022
Fixes #11320.

This PR fixes a false positive for `Lint/RequireParentheses`
when assigning ternary operator.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants