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

Lint/BinaryOperatorWithIdenticalOperands warning for simple math #8482

Closed
ShockwaveNN opened this issue Aug 7, 2020 · 2 comments · Fixed by #9221
Closed

Lint/BinaryOperatorWithIdenticalOperands warning for simple math #8482

ShockwaveNN opened this issue Aug 7, 2020 · 2 comments · Fixed by #9221
Labels

Comments

@ShockwaveNN
Copy link
Contributor

Steps to reproduce the problem

  1. Create test.rb file with
x = 1
y = x * x * 2
puts y
  1. Runing rubocop on it result:
Offenses:

1.rb:2:5: W: Lint/BinaryOperatorWithIdenticalOperands: Binary operator * has identical operands.
y = x * x * 2

I'm thinking this case is out of scope of Lint/BinaryOperatorWithIdenticalOperands, just a simple math calculation

This code can be fixed by

y = x ^ 2 * 2

But I don't think original code is bad

RuboCop version

Include the output of rubocop -V or bundle exec rubocop -V if using Bundler. Here's an example:

$ [bundle exec] rubocop -V
0.89.0 (using Parser 2.7.1.4, rubocop-ast 0.3.0, running on ruby 2.7.1 x86_64-linux)

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 7, 2020

@marcandre had a partial fix for this, but I was thinking that we'd encounter problems like the one you've described as well. I'm starting to think there was a reason we had constrained this check to comparison ops in the past. 😆

@bbatsov bbatsov added the bug label Aug 7, 2020
@fatkodima
Copy link
Contributor

Catching math ops is nevertheless useful as it can catch bugs like foo(arg1) * foo(arg1), where it should be foo(arg1) * foo(arg2) or foo(arg1) * bar(arg1) or even if it is a heavy function call, should be extracted into a variable and then used as var * var.

I think we should change the cop to ignore cases where lhs and rhs of mathematical operations are simple lvar, ivar, etc.
So y = @x * @x or mentioned var * var or y = x * x * 2 will be ignored.

JustinAiken added a commit to usertesting/ut_rubocop that referenced this issue Aug 26, 2020
- It was just added in 0.89.0
- It finds errors in simple math operations, has many errors reported,
such as:
  - rubocop/rubocop#8482
  - rubocop/rubocop#8478

It looks like the next version of rubocop may loosen it a bit, but let's
just disable it for now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants