Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Lint/BinaryOperatorWithIdenticalOperands
cop
- Loading branch information
Showing
11 changed files
with
139 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# This cop checks for places where binary operator has identical operands. | ||
# | ||
# It covers arithmetic operators: `+`, `-`, `*`, `/`, `%`, `**`; | ||
# comparison operators: `==`, `===`, `=~`, `>`, `>=`, `<`, `<=`; | ||
# bitwise operators: `|`, `^`, `&`, `<<`, `>>`; | ||
# boolean operators: `&&`, `||` | ||
# and "spaceship" operator - `<=>`. | ||
# | ||
# This cop is marked as unsafe as it does not consider side effects when calling methods | ||
# and thus can generate false positives: | ||
# if wr.take_char == '\0' && wr.take_char == '\0' | ||
# | ||
# @example | ||
# # bad | ||
# x.top >= x.top | ||
# | ||
# if a.x != 0 && a.x != 0 | ||
# do_something | ||
# end | ||
# | ||
# def childs? | ||
# left_child || left_child | ||
# end | ||
# | ||
class BinaryOperatorWithIdenticalOperands < Base | ||
MSG = 'Binary operator `%<op>s` has identical operands.' | ||
|
||
def on_send(node) | ||
return unless node.binary_operation? | ||
|
||
lhs, operation, rhs = *node | ||
return if node.arithmetic_operation? && lhs.basic_literal? && rhs.basic_literal? | ||
|
||
add_offense(node, message: format(MSG, op: operation)) if lhs == rhs | ||
end | ||
|
||
def on_and(node) | ||
add_offense(node, message: format(MSG, op: node.operator)) if node.lhs == node.rhs | ||
end | ||
alias on_or on_and | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
spec/rubocop/cop/lint/binary_operator_with_identical_operands_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Lint::BinaryOperatorWithIdenticalOperands do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense when binary operator has identical nodes' do | ||
offenses = inspect_source(<<~RUBY) | ||
x == x | ||
y = x && x | ||
y = a.x + a.x | ||
a.x(arg) > a.x(arg) | ||
a.(x) > a.(x) | ||
RUBY | ||
|
||
expect(offenses.size).to eq(5) | ||
end | ||
|
||
it 'does not register an offense when using binary operator with different operands' do | ||
expect_no_offenses(<<~RUBY) | ||
x == y | ||
y = x && z | ||
y = a.x + b.x | ||
a.x(arg) > b.x(arg) | ||
a.(x) > b.(x) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using arithmetic operator with numerics' do | ||
expect_no_offenses(<<~RUBY) | ||
x = 2 + 2 | ||
RUBY | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.