Same condition tested twice in one if / else-if chain
ID |
objectivec.correctness.duplicate_conditions |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Duplicate Logic |
Language |
Objective-C |
Description
Reports an if and a later else if in the same chain whose conditions are written identically, including the case where the two are separated by other branches or by a nested if. Conditions are compared as source text, so two conditions that differ only in spacing are not reported as duplicates. Two separate if statements that happen to share a condition are not reported either — only branches of one chain, where the second is genuinely unreachable.
Rationale
The second branch can never run: reaching it means the first condition was false, and the second is the same test. Its body is therefore dead, and the case it was written to handle falls through to whatever comes after the chain. This is almost always a copy-paste where the condition should have been edited and was not, so the missing case is the one the author cared about most — and because both branches are present and plausible, the chain reads as complete in review.
The following code illustrates the pattern detected by this rule:
@implementation Dispatcher
- (void)handle:(NSInteger)code {
// FLAGGED: Same condition tested twice in one if / else-if chain
if (code == 200) {
[self deliver];
} else if (code == 200) {
[self retry];
}
}
- (void)deliver {
}
- (void)retry {
}
- (void)log {
}
- (BOOL)isReady {
return YES;
}
@end