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

New Lint/AmbiguousOperatorPrecedence: problems with shift operator #10079

Closed
andrew-aladev opened this issue Sep 13, 2021 · 2 comments
Closed

Comments

@andrew-aladev
Copy link
Contributor

Please create file sample.rb:

a = 1 + 2 * (3 << 4)
rubocop --only="Lint/AmbiguousOperatorPrecedence" sample.rb

Expected behavior

Everything is fine.

Actual behavior

W: [Correctable] Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.
a = 1 + 2 * (3 << 4)
        ^^^^^^^^^^^^

RuboCop version

$ [bundle exec] rubocop -V
1.21.0 (using Parser 3.0.2.0, rubocop-ast 1.11.0, running on ruby 3.0.2 x86_64-linux)

See also #10047.

@dvandersluis
Copy link
Member

dvandersluis commented Sep 13, 2021

I'm not sure what the bug is here? The offense is not because of the << operator, but because of the + operator. Since * has higher precedence than +, the parentheses disambiguate the right side of the + operation.

The idea is that the parentheses will make it obvious that this expression can be expressed as:

b = 2 * (3 << 4)
a = 1 + b

In other words, if you didn't know the order of precedence in ruby, it's impossible to know if 1 + 2 * (3 << 4) is (1 + 2) * (3 << 4) or 1 + (2 * (3 << 4)), which is what this cop is addressing.

@andrew-aladev
Copy link
Contributor Author

Ok, thank you. I am going to disable this cop.

So this is not a bug.

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

No branches or pull requests

2 participants