From 068b6017061c3ab24f6984fcf8eb76b2a81f96b4 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Thu, 13 Aug 2020 17:52:39 -0400 Subject: [PATCH] [Fixes #8534] Fix `Lint/BinaryOperatorWithIdenticalOperands` for binary operators used as unary operators --- CHANGELOG.md | 1 + .../cop/lint/binary_operator_with_identical_operands.rb | 2 +- .../lint/binary_operator_with_identical_operands_spec.rb | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 852101fae06..f61bd3c8867 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ### Bug fixes * [#8508](https://github.com/rubocop-hq/rubocop/pull/8508): Fix a false positive for `Style/CaseLikeIf` when conditional contains comparison with a class. Mark `Style/CaseLikeIf` as not safe. ([@fatkodima][]) +* [#8534](https://github.com/rubocop-hq/rubocop/issues/8534): Fix `Lint/BinaryOperatorWithIdenticalOperands` for binary operators used as unary operators. ([@marcandre][]) ### Changes 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 c1e20bdd024..b25d2a71337 100644 --- a/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +++ b/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb @@ -35,7 +35,7 @@ def on_send(node) return unless node.binary_operation? lhs, operation, rhs = *node - return if MATH_OPERATORS.include?(node.method_name) && rhs.basic_literal? + return if MATH_OPERATORS.include?(node.method_name) && lhs.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 3dc24e2be41..9842065885f 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 @@ -31,4 +31,10 @@ x = 1 << 1 RUBY end + + it 'does not crash on operator without any argument' do + expect_no_offenses(<<~RUBY) + foo.* + RUBY + end end