Compare Value To Itself
ID |
csharp.compare_value_to_itself |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
CSharp |
Tags |
CWE:570, CWE:571, reliability, suspicious-comparison |
Description
Reports comparisons whose left-hand side and right-hand side are the same expression, for
example x == x or a.b == a.b. The comparison always yields the same constant result
and almost always signals a copy-paste mistake or a typo where one operand should have
been a different variable.
Rationale
A condition like if (count == count) is always true and if (count != count) is always
false (excluding NaN corner cases for floating-point), so the branch logic does not do
what the author intended. The most common cause is duplicating one operand and forgetting
to change it to the second variable.
return x == x; // FLAW — always true
return x != x; // FLAW — always false
return this.balance == this.balance; // FLAW — same member both sides
return x == y; // OK — distinct operands
return o is string; // OK — type test, not a value comparison