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 #7849] Add new Lint/AmbiguousOperatorPrecedence cop #10047

Merged
merged 2 commits into from Aug 29, 2021

Conversation

dvandersluis
Copy link
Member

As per @bbatsov's request a year ago (#7849), adds a cop that requires parentheses around operations within an expression with multiple operations of different precedence. It does not consider comparison or unary operations.

Please take a look at the second commit to see how rubocop itself is changed by this cop (it looks pretty good to me but if anything stands out we can customize).


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.

@dvandersluis dvandersluis changed the title [Fix #7849] Add new Lint/AmbiguousOperatorPrecedence cop [Fix #7849] Add new Lint/AmbiguousOperatorPrecedence cop Aug 26, 2021
@koic
Copy link
Member

koic commented Aug 26, 2021

TBH, it looks redundant style to me as many changes mean. I think this cop should be disabled by default.

@koic
Copy link
Member

koic commented Aug 26, 2021

Also, I think this is a style dept, not a lint dept, because users may have different tastes.

# # good (same precedence)
# a + b + c
# a * b / c % d
class AmbiguousOperatorPrecedence < Base
Copy link
Member

Choose a reason for hiding this comment

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

Ambiguous looked weird because operator precedence is clear in the Ruby specification. How about name like Style/OperatorPrecedenceParentheses?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think Ambiguous makes sense because it's aligned with the other Ambiguous cops -- flagging syntax that, while clearly defined for ruby itself, is not necessarily clear for a user reading it, especially one who is not well-versed in Ruby.

However, if it's not desired here, ExplicitOperatorPrecedence could be a better name.

Copy link
Member

Choose a reason for hiding this comment

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

ExplicitOperatorPrecedence could be a better name.

I also first came up with the same name :-) However, I've withheld the name because I'm a little confused as to whether it's Explicit or Implicit. But I think it's a better name than Ambiguous.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, probably we can do a bit better here, although the point is more that the precedence might be ambiguous for the people, not for Ruby. :-) It's clear that as far as Ruby's concerned there's no ambiguity, yet still people make a ton of mistakes with logical expressions, based on wrong assumptions about the priorities there. A more neutral name can be just LogicalOperatorPrecedence with styles like "Explicit" and "Implicit" if we want to appease everyone.

Copy link
Member Author

Choose a reason for hiding this comment

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

I would be strongly against making it configurable in a way that makes these parens an offense, and otherwise "Implicit" style would probably just be choosing to disable the cop 😅

@dvandersluis
Copy link
Member Author

If it's desired to move this to Style, I'm ok with that, I just put it in Lint to parallel the other Ambiguous cops as mentioned above.

I am ok with making this disabled by default as well, but I think it should be at the minimum enabled for rubocop itself, given @bbatsov's request for this in the first place. I'll defer to him though.

@koic
Copy link
Member

koic commented Aug 27, 2021

it should be at the minimum enabled for rubocop itself

I think .rubocop.yml of the repo should also be disabled (following the default). IMHO, it's tough for me that git blame will be overwritten in addition to redundant parentheses.

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 27, 2021

That's a valid point, although I have to say that the cop does exactly what I had in mind (therefore I like it) and I think that it should probably be enabled by default. Of course, I'm the type of person who's not afraid of extra parentheses. :D (and values clarity over brevity)

I'm on the fence as to whether that's a Style or a Lint cop - it can certainly prevent very real mistakes in the code. I would definitely like to have it enabled at least for RuboCop's codebase, but I don't see the harm in leaving it to the users to decide if they should enable it (meaning giving it the usual "pending" status).

@koic
Copy link
Member

koic commented Aug 27, 2021

Yeah, I fully agree that the implementation behavior is pretty to the expected behavior. However, I think it depends on the context whether it is preferable to have parentheses, so it is hard to be forced to do so uniformly. I sometimes hear "Default rules are too strict, but customization is hard...". I feel that this rule will increase it...

@dvandersluis
Copy link
Member Author

IMHO, it's tough for me that git blame will be overwritten in addition to redundant parentheses.

Respectfully, I don't think this should matter, it's easy enough to look at the previous blame for a line, and we do plenty of this kind of correction that overwrites a wider blame historically. I don't think it should prevent us from making changes that affect our own style.

I sometimes hear "Default rules are too strict, but customization is hard...".

Definitely get this. I think this is a beneficial change and would likely enable it on my codebase if it's disabled by default, but I will leave it to you guys. My counterpoint would be that it's very hard to discover cops that exist but aren't enabled...

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 27, 2021

My counterpoint would be that it's very hard to discover cops that exist but aren't enabled...

That's why I like launching everything as pending and leaving it to the users to decide what they like. If we get very bad feedback about something we won't be enabling it by default in the next version.

@bbatsov bbatsov merged commit 9885eef into rubocop:master Aug 29, 2021
@bbatsov
Copy link
Collaborator

bbatsov commented Aug 29, 2021

Let's go ahead with this cop and see how it goes. We can still change the name before the next release if someone comes up with a better name.

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.

None yet

3 participants