Repeated condition in an if / else-if chain
ID |
c.correctness.duplicate_conditions |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Duplicate Logic |
Language |
C / C++ |
Description
The same condition is tested twice in this if / else if chain. The second branch is unreachable because the first branch already handles that case, so its body is dead code. This is usually a copy-paste mistake where one condition should have been changed. Fix the duplicated condition to test the intended, distinct case.
Rationale
The same condition is tested twice in this if / else if chain. The second branch is unreachable because the first branch already handles that case, so its body is dead code. This is usually a copy-paste mistake where one condition should have been changed. Fix the duplicated condition to test the intended, distinct case.
The following code illustrates the pattern detected by this rule:
int classify(int x) {
// FLAGGED: Repeated condition in an if / else-if chain
if (x > 0) {
return 1;
} else if (x > 0) {
return 2;
}
return 0;
}