Duplicate Condition In If Chain
ID |
java.duplicate_condition_in_if_chain |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
control-flow, dead-code, reliability |
Description
Reports an if / else if condition that is structurally identical to an earlier condition in the same chain. The comparison is scoped to a single chain — the same condition appearing in two unrelated if statements elsewhere is ordinary code and is not reported.
A condition that contains a method call or an assignment is never compared: either can legitimately return a different value each time it runs, so two textually identical calls are not necessarily the same defect as two identical pure expressions.
Rationale
An if / else if chain runs top to bottom and stops at the first branch whose condition is true. When a later condition duplicates an earlier one exactly, that branch can never be reached — the earlier branch always wins first. This is almost always a copy-paste mistake where one operand was left unchanged, and the dead branch hides the condition the author actually meant to write.
if (status == PENDING) {
openTicket();
} else if (status == CLOSED) {
archiveTicket();
} else if (status == PENDING) { // unreachable -- PENDING is already handled above
escalateTicket();
}