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/TernaryParentheses autocorrect changes code intent #4460
Comments
Interesting, this does seem like a bug because the method call isn't using parenthesis. This appears to be a different issue than #4118. The preferred syntax for this would be |
I agree with @rrosenblum. I am inclined to say that this shouldn't be auto-corrected by this cop, because deciding whether So the fix would be to not auto-correct cases like these, IMHO. |
Hi, we ran into the same issue. When auto-correcting with the One class of cases was the one above (method call without parentheses), the other one was
is rewritten into
|
This was fixed by #3505. |
I am still seeing this error in rubocop v0.62:
It is corrected to this:
I have configured the Style/TernaryParentheses cop with EnforcedStyle: require_parentheses_when_complex If I run @buehmann s example above with default configuration I get the same results as he does.
|
Yes. There's still a bug there. 😞 |
…call? We say that a method call is not safe for removing outer parentheses if the method takes an argument but doesn't enclose the argument in parentheses. E.g., `(func x) ? 1 : 0`, but the previous solution didn't get all cases right.
The
Style/TernaryParentheses
cop removes parentheses around ternary conditions when auto-correcting.This week we had a case where applying this cop's autocorrect resulted in a change in code behaviour. I understand from other PRs/issues that autocorrects are intended to not change code's behaviour, only it's style.
Given code like
(odd? x) ? "A" : "B"
, this cop transforms it intoodd? x ? "A" : "B"
which will return a different result.Expected behavior
Given:
(odd? x) ? "A" : "B"
I would expect the cop to do one of:
odd?(x) ? "A" : "B"
Actual behavior
The code is auto-corrected to
odd? x ? "A" : "B"
(interpreted asodd?(x ? "A" : "B")
)Steps to reproduce the problem
I've reproduced in a test which you can run: petehamilton@a9ec2ee
RuboCop version
Note: I originally thought this might be a similar issue to #4118 but on re-reading I think it's actually different so I've opened this issue to discuss
The text was updated successfully, but these errors were encountered: