Skip to content

Commit

Permalink
[Fixes rubocop#8478] Relax Lint/BinaryOperatorWithIdenticalOperands f…
Browse files Browse the repository at this point in the history
…or mathematical operations

Simplified one of the `basic_literal?`, since we'd only raise an offense if they are
equal, in which case `basic_literal?` has the same result on both `lhs` and `rhs`...
  • Loading branch information
marcandre committed Aug 6, 2020
1 parent 5fc07bb commit 3b872a0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#8478](https://github.com/rubocop-hq/rubocop/issues/8478): Relax `Lint/BinaryOperatorWithIdenticalOperands` for mathematical operations. ([@marcandre][])

## 0.89.0 (2020-08-05)

### New features
Expand Down
Expand Up @@ -29,12 +29,13 @@ module Lint
#
class BinaryOperatorWithIdenticalOperands < Base
MSG = 'Binary operator `%<op>s` has identical operands.'
MATH_OPERATORS = %i[+ - * / % ** << >> | ^].to_set.freeze

def on_send(node)
return unless node.binary_operation?

lhs, operation, rhs = *node
return if node.arithmetic_operation? && lhs.basic_literal? && rhs.basic_literal?
return if MATH_OPERATORS.include?(node.method_name) && rhs.basic_literal?

add_offense(node, message: format(MSG, op: operation)) if lhs == rhs
end
Expand Down
Expand Up @@ -28,6 +28,7 @@
it 'does not register an offense when using arithmetic operator with numerics' do
expect_no_offenses(<<~RUBY)
x = 2 + 2
x = 1 << 1
RUBY
end
end

0 comments on commit 3b872a0

Please sign in to comment.