Method parameter never used in the body
ID |
objectivec.maintainability.unused_method_parameter |
Severity |
info |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Dead Code |
Language |
Objective-C |
Description
Reports a method that never mentions one of its own parameters in its body. The body is bounded at the closing brace in the first column, so a name used in a later method does not count as a use, and the method header must itself start in the first column. Two Cocoa conventions are deliberately exempt: a parameter named after its own type, which is how a delegate callback names the object sending the message, and a parameter named sender, which is the target-action convention. A method carrying either is not reported, even for its other parameters.
Rationale
Every parameter is a promise that the value affects what the method does. One the body never reads breaks that promise silently: callers keep computing and passing a value that goes nowhere, and the next reader has to check the whole body to establish that. It is also how a half-finished change looks - the body was rewritten to use something else and the parameter was left behind - or a symptom of a real bug, where the parameter that should have been used was shadowed by a local or by an instance variable with a similar name.
The following code illustrates the pattern detected by this rule:
@interface Report : NSObject
@end
@implementation Report
// FLAGGED: Method parameter never used in the body
- (NSString *)render:(NSString *)title footer:(NSString *)footer {
return title;
}
- (void)later {
NSString *caption = @"present";
NSLog(@"%@", caption);
}
- (void)reload {
NSLog(@"reload");
}
@end
Remediation
Remove the parameter and the keyword that introduces it, and update the callers - in Objective-C the keyword is part of the selector, so the change is visible at every call site, which is what makes it safe. Where the signature is fixed by a protocol or by a target-action pair, keep the parameter; naming it after its type, as the delegate conventions do, is what tells this rule it is deliberate.