Identical Operands
ID |
swift.identical_operands |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Swift |
Tags |
reliability, suspicious |
Description
Reports an equality (==, !=, ===, !==) or relational (<, ⇐,
>, >=) binary expression whose left and right operands have identical
source text. x == x is always true (except NaN); x < x is always
false — almost always a typo.
if x == x { ... } // FLAW
return s != s // FLAW
if a < a { ... } // FLAW
if x == y { ... } // OK
if x.isNaN { ... } // OK — explicit NaN check
Rationale
The only legitimate use of x != x is detecting NaN — and Swift has
isNaN for that. Every other identical-operand comparison is dead code,
usually copy-paste from a more complex expression that lost its second
variable.
Remediation
Replace with the intended comparison:
// If you wanted to compare with a different variable:
if x == y { ... }
// If you wanted a NaN check:
if x.isNaN { ... }