Skip to content

Offend parentheses for chained && expressions in Style/RedundantParentheses#14024

Merged
koic merged 1 commit intorubocop:masterfrom
lovro-bikic:redundant-parentheses-for-chained-and
Mar 23, 2025
Merged

Offend parentheses for chained && expressions in Style/RedundantParentheses#14024
koic merged 1 commit intorubocop:masterfrom
lovro-bikic:redundant-parentheses-for-chained-and

Conversation

@lovro-bikic
Copy link
Contributor

@lovro-bikic lovro-bikic commented Mar 23, 2025

Follow-up to #14012 (comment) to see if this change is acceptable.

Offends cases such as (x && y) && z and autocorrects to x && y && z since parentheses have no bearing on expression evaluation.

I know that in some cases parentheses are redundant for the machine but not for the human since they might make the intention clearer; this cop already accepts redundant parentheses in cases such as:

it 'accepts parentheses around logical operator in ternary operator' do
# Parentheses are redundant, but respect user's intentions for readability.
expect_no_offenses('cond ? x : (y || z)')
end

In case we don't want to change existing behavior, I'm also okay with that, but then I'd like to change this PR so it at least adds an expectation that (x && y) && z doesn't offend (currently, this case isn't covered).


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.

^^^^^^^^ Don't use parentheses around a logical expression.

x || (y && z) && w
^^^^^^^^ Don't use parentheses around a logical expression.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you separate the test cases as much as possible?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@lovro-bikic lovro-bikic force-pushed the redundant-parentheses-for-chained-and branch 2 times, most recently from 4575cba to 81956b0 Compare March 23, 2025 09:20
@lovro-bikic lovro-bikic force-pushed the redundant-parentheses-for-chained-and branch from 81956b0 to 88bb560 Compare March 23, 2025 09:22
@koic koic merged commit d8bc66c into rubocop:master Mar 23, 2025
24 checks passed
@lovro-bikic lovro-bikic deleted the redundant-parentheses-for-chained-and branch March 23, 2025 22:20
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.

2 participants