Compare Value To Itself
ID |
javascript.compare_value_to_itself |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
JavaScript |
Tags |
CWE:570, CWE:571, reliability, suspicious-comparison |
Description
Reports comparisons where both sides are the same expression — x === x, a > a, obj.field ⇐ obj.field, etc. The result is constant for any value of the operand and is almost always a copy-paste mistake or a leftover from a refactor.
Rationale
Self-comparisons are at best dead code, at worst a hidden logic bug:
-
x === xis alwaystruefor any value exceptNaN(where it isfalse);x !== xis the inverse. -
Relational self-comparisons (
a > a,a >= a,a < a,a ⇐ a) are constant for non-NaNvalues. -
The intent was almost certainly to compare two different operands.
The single legitimate use of x !== x — testing for NaN — is better expressed via Number.isNaN(x). The dedicated javascript.use_isnan rule guides that fix; this rule flags the symptomatic pattern uniformly so neither idiom slips through.
// Bad - constant outcomes from self-comparison
if (count === count) { ... }
if (status !== status) { ... } // intent was probably Number.isNaN(status)
if (price > price) { ... }