Compare Identical
ID |
go.compare_identical |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Go |
Tags |
reliability, suspicious-comparison |
Description
Reports comparisons whose two operands 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
A self-comparison is at best dead code, at worst a hidden logic bug. x == x is always true
and x != x is always false, so the branch they guard either always runs or never runs. The
intent was almost certainly to compare two different operands.
Floating-point operands are the one exception: because NaN != NaN, the form f == f is a
(non-idiomatic) NaN test, so float comparisons are not reported here — the dedicated
go.do_not_compare_nan rule guides the correct math.IsNaN fix.
func check(x int, y int) bool {
if x == x { // FLAW — always true
return true
}
if x > x { // FLAW — always false
return true
}
return x == y // OK — different operands
}
Remediation
Replace the duplicated operand with the value it should be compared against. If the comparison is genuinely redundant, delete it.