switch case falls through into the next label
ID |
objectivec.correctness.missing_break_in_switch |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Control Flow |
Language |
Objective-C |
Description
Reports a case arm that carries statements and reaches the next case:/default: label without break, return, goto, continue, exit, abort or @throw, and without a comment saying it falls through. An empty label stacked above another, and an arm holding a braced block, are not reported.
Rationale
Control drops straight into the arm below, so two arms run for one value. Because the arms are laid out one under another with nothing between them, the code reads as if only the matched arm executes — and the arm below is usually the one that was written for a different value entirely. It is the classic outcome of appending a new case above an existing one and forgetting the jump, which is why a deliberate fall-through is conventionally commented.
The following code illustrates the pattern detected by this rule:
@implementation Handler
- (void)fallsThrough:(XYState)state {
switch (state) {
// FLAGGED: switch case falls through into the next label
case XYStateIdle:
[self idle];
[self log];
case XYStateRunning:
[self run];
break;
default:
break;
}
}
- (BOOL)ready { return YES; }
- (void)idle { }
- (void)run { }
- (void)done { }
- (void)log { }
- (void)unknown { }
@end