@finally block is empty
ID |
objectivec.correctness.empty_finally_block |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Error Handling |
Language |
Objective-C |
Description
Reports an @finally clause whose body holds nothing at all, whether the braces are written on one line or across several. A clause carrying a statement or an explanatory comment is not reported.
Rationale
The only reason to write @finally is to run cleanup on every exit path, so an empty one is a promise of cleanup that never happens. Either the release was removed and the clause was left behind — in which case the socket, file handle or lock the @try acquired now leaks on the exception path — or the clause was added in anticipation and never filled in. The surrounding code still reads as if the resource is handled.
The following code illustrates the pattern detected by this rule:
@implementation Session
- (void)openOnOneLine {
@try {
[self connect];
} @catch (NSException *e) {
NSLog(@"%@", e.reason);
// FLAGGED: @finally block is empty
} @finally { }
}
- (void)connect {
NSLog(@"connecting");
}
- (void)disconnect {
NSLog(@"disconnecting");
}
@end