Both sides of a binary operator are the same expression
ID |
scala.correctness.identical_operands |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Suspicious Code |
Language |
Scala |
Description
Reports a comparison, logical or arithmetic operator whose left and right operands are the same identifier or field access, such as a == a or count - count.
Rationale
Such an expression has a fixed result that does not depend on the value being tested - a comparison is always true or always false, a conjunction is a no-op, a subtraction is always zero - so the surrounding logic never does what it appears to. It is almost always a typo or a copy-paste slip where one operand should name a different variable or field, and the resulting branch is either dead or unconditionally taken.
The following code illustrates the pattern detected by this rule:
class IdenticalOperands {
case class Range(lo: Int, hi: Int)
def compare(a: Int): Boolean = {
// FLAGGED: Both sides of a binary operator are the same expression
a == a
}
}