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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#7849](https://github.com/rubocop/rubocop/issues/7849): Add new `Lint/AmbiguousOperatorPrecedence` cop. ([@dvandersluis][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# This cop looks for expressions containing multiple binary operators | ||
# where precedence is ambiguous due to lack of parentheses. For example, | ||
# in `1 + 2 * 3`, the multiplication will happen before the addition, but | ||
# lexically it appears that the addition will happen first. | ||
# | ||
# The cop does not consider unary operators (ie. `!a` or `-b`) or comparison | ||
# operators (ie. `a =~ b`) because those are not ambiguous. | ||
# | ||
# NOTE: Ranges are handled by `Lint/AmbiguousRange`. | ||
# | ||
# @example | ||
# # bad | ||
# a + b * c | ||
# a || b && c | ||
# a ** b + c | ||
# | ||
# # good (different precedence) | ||
# a + (b * c) | ||
# a || (b && c) | ||
# (a ** b) + c | ||
# | ||
# # good (same precedence) | ||
# a + b + c | ||
# a * b / c % d | ||
class AmbiguousOperatorPrecedence < Base | ||
extend AutoCorrector | ||
|
||
# See https://ruby-doc.org/core-3.0.2/doc/syntax/precedence_rdoc.html | ||
PRECEDENCE = [ | ||
%i[**], | ||
%i[* / %], | ||
%i[+ -], | ||
%i[<< >>], | ||
%i[&], | ||
%i[| ^], | ||
%i[&&], | ||
%i[||] | ||
].freeze | ||
RESTRICT_ON_SEND = PRECEDENCE.flatten.freeze | ||
MSG = 'Wrap expressions with varying precedence with parentheses to avoid ambiguity.' | ||
|
||
def on_new_investigation | ||
# Cache the precedence of each node being investigated | ||
# so that we only need to calculate it once | ||
@node_precedences = {} | ||
super | ||
end | ||
|
||
def on_and(node) | ||
return unless (parent = node.parent) | ||
|
||
return if parent.begin_type? # if the `and` is in a `begin`, it's parenthesized already | ||
return unless parent.or_type? | ||
|
||
add_offense(node) do |corrector| | ||
autocorrect(corrector, node) | ||
end | ||
end | ||
|
||
def on_send(node) | ||
return if node.parenthesized? | ||
|
||
return unless (parent = node.parent) | ||
return unless operator?(parent) | ||
return unless greater_precedence?(node, parent) | ||
|
||
add_offense(node) do |corrector| | ||
autocorrect(corrector, node) | ||
end | ||
end | ||
|
||
private | ||
|
||
def precedence(node) | ||
@node_precedences.fetch(node) do | ||
PRECEDENCE.index { |operators| operators.include?(operator_name(node)) } | ||
end | ||
end | ||
|
||
def operator?(node) | ||
(node.send_type? && RESTRICT_ON_SEND.include?(node.method_name)) || node.operator_keyword? | ||
end | ||
|
||
def greater_precedence?(node1, node2) | ||
precedence(node2) > precedence(node1) | ||
end | ||
|
||
def operator_name(node) | ||
if node.send_type? | ||
node.method_name | ||
else | ||
node.operator.to_sym | ||
end | ||
end | ||
|
||
def autocorrect(corrector, node) | ||
corrector.wrap(node, '(', ')') | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Ambiguous
looked weird because operator precedence is clear in the Ruby specification. How about name likeStyle/OperatorPrecedenceParentheses
?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.
I think
Ambiguous
makes sense because it's aligned with the otherAmbiguous
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.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.
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
orImplicit
. But I think it's a better name thanAmbiguous
.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.
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.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.
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 😅