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 7acbdb8 commit be47dc7
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

* [#8463](https://github.com/rubocop-hq/rubocop/pull/8463): Fix false positives for `Lint/OutOfRangeRegexpRef` when a regexp is defined and matched in separate steps. ([@eugeneius][])
* [#8466](https://github.com/rubocop-hq/rubocop/issues/8466): Fix a false positive for `Lint/UriRegexp` when using `regexp` method without receiver. ([@koic][])
* [#8478](https://github.com/rubocop-hq/rubocop/issues/8478): Relax `Lint/BinaryOperatorWithIdenticalOperands` for mathematical operations. ([@marcandre][])

## 0.89.0 (2020-08-05)

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 be47dc7

Please sign in to comment.