diff --git a/CHANGELOG.md b/CHANGELOG.md index 23f129ef601..3e05d140f7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb b/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb index d28a3ff5c6a..c1e20bdd024 100644 --- a/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +++ b/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb @@ -29,12 +29,13 @@ module Lint # class BinaryOperatorWithIdenticalOperands < Base MSG = 'Binary operator `%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 diff --git a/spec/rubocop/cop/lint/binary_operator_with_identical_operands_spec.rb b/spec/rubocop/cop/lint/binary_operator_with_identical_operands_spec.rb index 97f9f01b3af..3dc24e2be41 100644 --- a/spec/rubocop/cop/lint/binary_operator_with_identical_operands_spec.rb +++ b/spec/rubocop/cop/lint/binary_operator_with_identical_operands_spec.rb @@ -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