for loop has an empty body
ID |
objectivec.correctness.empty_for_block |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Suspicious Code |
Language |
Objective-C |
Description
Reports a for loop — C-style or fast enumeration — whose body holds nothing at all, whether the braces are written on one line or across several. A body carrying a statement, or a comment explaining that the loop deliberately does its work in the header, is not reported.
Rationale
A for loop with an empty body iterates and produces nothing, so either the body was deleted and the loop shell survived, or the work now lives entirely in the increment or the condition. The first case is a silent behaviour change: the collection is still walked, the cost is still paid, and the effect the loop was written for is simply gone. The second is legal but hides the loop’s real work in the one place a reader does not look for it.
The following code illustrates the pattern detected by this rule:
@implementation Indexer
- (void)walkOnOneLine:(NSArray *)items {
// FLAGGED: for loop has an empty body
for (NSUInteger i = 0; i < items.count; i++) { }
}
- (void)index:(id)item {
NSLog(@"%@", item);
}
@end
Remediation
Restore the body, or delete the loop if walking the collection no longer achieves anything. Where the work genuinely belongs in the header — counting elements out of an enumerator, for instance — move it into the body, or say so in a comment inside the braces so the emptiness reads as a decision rather than an omission.