Nil check around a release message
ID |
objectivec.maintainability.unnecessary_nilcheck_dealloc |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Redundant Code |
Language |
Objective-C |
Description
This nil check is dead weight: a message sent to nil in Objective-C is a no-op, so [obj release] is already safe when obj is nil. Drop the surrounding if and send the release unconditionally. This rule is about the redundant guard only - the manual reference counting itself is reported by manual-memory-management, which also flags the inner release, so a guarded release is reported by both rules.
Rationale
This nil check is dead weight: a message sent to nil in Objective-C is a no-op, so [obj release] is already safe when obj is nil. Drop the surrounding if and send the release unconditionally. This rule is about the redundant guard only - the manual reference counting itself is reported by manual-memory-management, which also flags the inner release, so a guarded release is reported by both rules.
The following code illustrates the pattern detected by this rule:
@implementation Article
- (void)dealloc {
// FLAGGED: Nil check around a release message
if (_title) {
[_title release];
}
// FLAGGED: Nil check around a release message
if (_body) {
[_body release];
_body = nil;
}
// FLAGGED: Nil check around a release message
if (_tags) [_tags release];
// FLAGGED: Nil check around a release message
if (_url != nil) {
[_url release];
}
// FLAGGED: Nil check around a release message
if (nil != _author) [_author release];
// An unguarded release is the fix, and the control for the whole rule.
[_summary release];
[super dealloc];
}
@end