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

[Fix #12380] Make Style/RedundantParentheses aware of lambda or proc #12381

Conversation

koic
Copy link
Member

@koic koic commented Nov 10, 2023

Fixes #12380.

This PR makes Style/RedundantParentheses aware of lambda or proc.


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.

… or proc

Fixes rubocop#12380.

This PR makes `Style/RedundantParentheses` aware of lambda or proc.
@koic koic merged commit 8b79749 into rubocop:master Nov 13, 2023
28 checks passed
@koic koic deleted the make_style_redundant_parentheses_aware_of_lambda_or_proc branch November 13, 2023 04:33
@anton-co
Copy link

anton-co commented Dec 4, 2023

@koic This doesn't seem to work correctly. In one project, all scopes that look like this:

scope :my_scope,
      (lambda do
        where(column: :value)
      end)

Formatted into this:

scope :my_scope,
      -> { lambda } do
        where(column: :value)
      end

And this:

scope :my_scope, (lambda do
  where(column: :value)
end)

Into this:

scope :my_scope, -> { lambda } do
  where(column: :value)
end

I mean the situation when there are several lines in the body of a lambda. It's not possible in one line.

koic added a commit to koic/rubocop that referenced this pull request Dec 4, 2023
Follow up rubocop#12381 (comment).

This PR fixes a false positive for `Style/RedundantParentheses`
when using parenthesized `lambda` or `proc` with `do`...`end` block.

`do`...`end` and `{`...`}` have different precedence, so with `do`...`end`,
parentheses are needed to correct the order of association. However, with `->`,
there is no need to worry about precedence, so the parentheses can be removed.
@koic
Copy link
Member Author

koic commented Dec 4, 2023

@anton-co Right! I've opened #12505 to resolve the issue. Thank you for the feedback!

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.

Improve Style/RedundantParentheses cop for lambdas
2 participants