switch has case labels but no default label
ID |
objectivec.correctness.missing_default_in_switch |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Control Flow |
Language |
Objective-C |
Description
Reports a switch that carries at least one case: label and no default: label anywhere before the enclosing method ends. A default written first, last or after a nested switch is recognised, and a commented-out or quoted switch is not reported.
Rationale
Without a default arm every value the case labels do not name falls straight through the statement, so the method continues as if nothing needed doing — no branch ran, nothing was logged, and the omission is invisible at the call site. That is exactly what happens when a constant is added to the enumeration later: the new value silently takes the do-nothing path in every switch that was written before it existed.
The following code illustrates the pattern detected by this rule:
@implementation Router
- (void)twoCasesNoDefault:(XYState)state {
// FLAGGED: switch has case labels but no default label
switch (state) {
case XYStateIdle:
[self idle];
break;
case XYStateRunning:
[self run];
break;
}
}
- (BOOL)ready { return YES; }
- (XYState)mode { return XYStateIdle; }
- (void)idle { }
- (void)run { }
- (void)done { }
- (void)unknown { }
@end