Control structure body is not wrapped in curly braces
ID |
objectivec.maintainability.control_statement_braces |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Readability |
Language |
Objective-C |
Description
Reports an if, else, for or while whose body is a bare statement rather than a block, whether the statement sits on the header line or on the line below it. A do / while, an else if that opens a block, and a header whose condition spans several lines are not reported.
Rationale
Without braces only the first statement belongs to the control structure, and nothing in the layout says so. Adding a second line under an unbraced if silently moves it outside the condition — it stays indented, it looks guarded, and it runs unconditionally. The same shape lets a dangling else bind to the wrong if, and it makes every diff that touches the body larger than it needs to be, because the braces have to be added before the line can be.
The following code illustrates the pattern detected by this rule:
@implementation Guard
- (void)checkNextLine:(NSInteger)value flag:(BOOL)flag items:(NSArray *)items {
// FLAGGED: Control structure body is not wrapped in curly braces
if (flag)
[self report:value];
// FLAGGED: Control structure body is not wrapped in curly braces
while (value > 0)
value--;
// FLAGGED: Control structure body is not wrapped in curly braces
for (id item in items)
[self report:1];
if (flag) {
[self report:value];
// FLAGGED: Control structure body is not wrapped in curly braces
} else
[self report:0];
}
- (BOOL)positive:(NSInteger)v {
return v > 0;
}
- (NSUInteger)countOf:(NSInteger)v {
return (NSUInteger)v;
}
- (void)report:(NSInteger)v {
NSLog(@"%ld", (long)v);
}
@end