forked from rubocop/rubocop
/
binary_operator_with_identical_operands.rb
68 lines (63 loc) · 2.05 KB
/
binary_operator_with_identical_operands.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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 - `<=>`.
#
# Simple arithmetic operations are allowed by this cop: `+`, `*`, `**`, `<<` and `>>`.
# Although these can be rewritten in a different way, it should not be necessary to
# do so. This does not include operations such as `-` or `/` where the result will
# always be the same (`x - x` will always be 0; `x / x` will always be 1), and
# thus are legitimate offenses.
#
# @safety
# This cop is unsafe as it does not consider side effects when calling methods
# and thus can generate false positives, for example:
#
# [source,ruby]
# ----
# if wr.take_char == '\0' && wr.take_char == '\0'
# # ...
# end
# ----
#
# @example
# # bad
# x / x
# x.top >= x.top
#
# if a.x != 0 && a.x != 0
# do_something
# end
#
# def childs?
# left_child || left_child
# end
#
# # good
# x + x
# 1 << 1
#
class BinaryOperatorWithIdenticalOperands < Base
MSG = 'Binary operator `%<op>s` has identical operands.'
ALLOWED_MATH_OPERATORS = %i[+ * ** << >>].to_set.freeze
def on_send(node)
return unless node.binary_operation?
lhs, operation, rhs = *node
return if ALLOWED_MATH_OPERATORS.include?(node.method_name)
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