-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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 should allow parenthesis for compound logical statements #12511
Comments
Those are super rare in practice, though, so providing special support for them seems like an overkill to me. Might be best to just implement a custom cop targeting those. |
@bbatsov if you check the documentation for Style/RedundantParentheses it shows: # bad
(x) if ((y.z).nil?)
# good
x if y.z.nil? so I assume the purpose of this cop is to also eliminate parenthesis around singular statements, even though they are super rare. Despite being rare, I'd like to ensure they don't get into my code, while still allowing parenthesis around compound logical statements. |
In all the cases exemplified, Ruby style does not use parentheses around the outermost part of condition. And This cop respects parentheses that alter the precedence of the condition and targets only the outermost parentheses for removal. |
The rubocop examples show only invole singular logical statements (ex: The examples listed in the Ruby Style guide also only use singular logical statements, not compound logical statements. There is no authoritative style example for whether parenthesis in compound logical statements is allowed or prohibited This should be clarified, as currently every example only refers to singular logical statements. |
I would like to second this. I've had a couple cases that prompted me to disable this cop entirely because it wanted my code to be less readable. For instance: @var ||= (method1 || method2) Ruby does not require the parentheses. However, having the parentheses makes it very clear what is happening in the code. If the parentheses are gone, I expect developers looking at the code later to have to pause and consider if the code actually works the way it is intended. |
Fixes rubocop#12511 (comment). This PR makes `Style/RedundantParentheses` allow parentheses around conditional expressions in assignment to respect user's intentions for readability: ```ruby var = (foo || bar) ```
Fixes rubocop#12511 (comment). This PR makes `Style/RedundantParentheses` allow parentheses around conditional expressions in assignment to respect user's intentions for readability: ```ruby var = (foo || bar) ```
Fixes rubocop#12511 (comment). This PR makes `Style/RedundantParentheses` allow parentheses around conditional expressions in assignment to respect user's intentions for readability: ```ruby var ||= (foo || bar) ```
Recently
rubocop
began flagging code with the Style/RedundantParentheses rule. However, it was flagging complex compound logical statements where the parenthesis help with showing the beginning and end of said statement, which helps with code readability.I do still believe
Style/RedundantParentheses
can be useful for catching parenthesis around singular logical statements (ex:if (variable.nil?)
).I believe
Style/RedundantParentheses
should make an exception for compound logical statements, or should at least be configurable to make an exception for compound logical statements. Disabling the rule entirely would allow needless parenthesis around singular logical statements to slip by.The text was updated successfully, but these errors were encountered: