-
-
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
Make Style/RedundantParentheses
allow parentheses in assignment
#12892
base: master
Are you sure you want to change the base?
Make Style/RedundantParentheses
allow parentheses in assignment
#12892
Conversation
I think this should be some configuration option, not a default behavior. |
I think that by default user's intent can be respected with |
Style/RedundantParentheses
allow parentheses in assignment.Style/RedundantParentheses
allow parentheses in assignment
4d8d17c
to
a054bef
Compare
My point is that if a cop is supposed to register redundant parentheses anything that's redundant, but someone might want to keep should be considered some special case. In general it's hard to know if some redundant parentheses where placed on purpose or accidentally. |
Also - it seems me it'd be good to extend the example's in the cop's documentation to cover more cases of redundant parentheses (e.g. like the one we're discussing right now). |
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) ```
a054bef
to
7265952
Compare
That reasoning makes a lot of sense. OTOH, I apologize for sharing my personal opinion, but for instance, the operator precedence differs in the following example: $ ruby-parse -e "var = foo || bar"
(lvasgn :var
(or
(send nil :foo)
(send nil :bar)))
$ ruby-parse -e "var = foo or bar"
(or
(lvasgn :var
(send nil :foo))
(send nil :bar)) I understand that allowing parentheses in these cases provides consistent readability. But, the current behavior forces parentheses to be removed when using So, I've added this special case to the documentation. How about starting with this? |
Yeah, I agree the parentheses should be preserved when removing them would change the AST. I'd suggest the tests and the changes to focus on this. |
@@ -5,6 +5,13 @@ module Cop | |||
module Style | |||
# Checks for redundant parentheses. | |||
# | |||
# Parentheses are allowed in the following cases for readability: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the tests we've allowed them everywhere where there is assignment, and I don't think this always improves the readability.
I still think that by default they should allowed only where removing them would change the AST, and that there should be an option to allow them in assignments more broadly.
@rubocop/rubocop-core Anyone else has an opinion on how to proceed here? |
I would expect a Style/RedundantParentheses in its default configuration to flag not just some redundant parentheses but all redundant parentheses. If that results in my code looking “inconsistent” because |
@bquorning I totally agree with your perspective! |
Fixes #12511 (comment).
This PR makes
Style/RedundantParentheses
allow parentheses around conditional expressions in assignment to respect user's intentions for readability:Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.