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

Make Style/RedundantParentheses allow parentheses in assignment #12892

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

koic
Copy link
Member

@koic koic commented May 8, 2024

Fixes #12511 (comment).

This PR makes Style/RedundantParentheses allow parentheses around conditional expressions in assignment to respect user's intentions for readability:

var = (foo || bar)

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@bbatsov
Copy link
Collaborator

bbatsov commented May 9, 2024

I think this should be some configuration option, not a default behavior.

@koic
Copy link
Member Author

koic commented May 9, 2024

I think that by default user's intent can be respected with var = (a || b). If user don't like it, intentionally writing var = foo || bar will not register an offense.
How about starting with that approach, and if issues become noticeable, consider adding a configuration option?

@koic koic changed the title Make Style/RedundantParentheses allow parentheses in assignment. Make Style/RedundantParentheses allow parentheses in assignment May 9, 2024
@koic koic force-pushed the make_style_redundant_parentheses_allow_parentheses_in_assignment branch from 4d8d17c to a054bef Compare May 9, 2024 08:25
@bbatsov
Copy link
Collaborator

bbatsov commented May 9, 2024

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.

@bbatsov
Copy link
Collaborator

bbatsov commented May 9, 2024

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)
```
@koic koic force-pushed the make_style_redundant_parentheses_allow_parentheses_in_assignment branch from a054bef to 7265952 Compare May 9, 2024 09:52
@koic
Copy link
Member Author

koic commented May 9, 2024

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 ||. (which is, of course, expected behavior).
For a use case, see rubygems/rubygems@7cc647c#diff-6e1c7ff50326d1315e87ff73c66c05ba3897487bcbac0ed1290ad3a3bd889efc, though I doubt that this improved readability, from var ||= (foo || bar) to var ||= foo || bar.

So, I've added this special case to the documentation. How about starting with this?

@bbatsov
Copy link
Collaborator

bbatsov commented May 17, 2024

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:
Copy link
Collaborator

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.

@bbatsov
Copy link
Collaborator

bbatsov commented May 22, 2024

@rubocop/rubocop-core Anyone else has an opinion on how to proceed here?

@bquorning
Copy link
Contributor

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 a = (b or c) has parens while a = b || c has not, it is fine. It actually underlines the difference between the two operators. (And in my personal opinion I think many people could use the reminder that || and or are not the same thing.)

@bbatsov
Copy link
Collaborator

bbatsov commented May 22, 2024

@bquorning I totally agree with your perspective!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Style/RedundantParentheses should allow parenthesis for compound logical statements
3 participants