Statement indented as if it were part of an unbraced body
ID |
objectivec.correctness.misleading_indentation |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Control Flow |
Language |
Objective-C |
Description
Reports an unbraced if, else if, for or while whose single statement is followed by a second statement at exactly the same indentation, so the second one looks guarded and is not. A body followed by code at the header’s own indentation, a braced body, a statement broken over two lines and a comment at the body indentation are not reported. An unbraced else without a condition is not covered.
Rationale
Only the first statement belongs to the unbraced body; the second runs unconditionally. The indentation says the opposite, and it is the indentation that a reader believes, so the bug survives review and is usually introduced by adding a line to what looked like a block. Where the guard exists to skip work, the skipped work now runs anyway - on exactly the path the guard was written to protect.
The following code illustrates the pattern detected by this rule:
@implementation Uploader
- (BOOL)uploadItem:(NSString *)item {
// FLAGGED: Statement indented as if it were part of an unbraced body
if (!self.connected)
[self reconnect];
[self markDirty];
return YES;
}
- (void)reconnect { }
- (void)markDirty { }
- (void)sendNext { }
- (void)flush { }
- (void)clearPending { }
- (void)accept { }
- (void)ignore { }
- (void)log { }
- (void)send:(NSArray *)items retry:(BOOL)retry { }
@end