Same Conditional
ID |
csharp.same_conditional |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
CSharp |
Tags |
reliability, suspicious-comparison |
Description
Reports an else if whose condition is textually identical to a condition that already
appeared earlier in the same if / else if chain. Because the first matching condition
already routes control flow into its own branch, the duplicate is unreachable.
Rationale
A repeated condition in an if / else if chain almost always means the author copied a
prior branch and forgot to change the condition. The body of the duplicate is dead code
and the intended behaviour is silently missing.
if (state == "open") { Open(); }
else if (state == "closed") { Close(); }
else if (state == "open") { Reopen(); } // FLAW — unreachable duplicate of the first branch
if (a > 0) { Pos(); }
else if (a < 0) { Neg(); }
else { Zero(); } // OK — distinct conditions
Remediation
Decide which behaviour the duplicate branch was supposed to capture and fix its condition, or remove the branch if it is genuinely redundant.