Method parameter is never mentioned in the body
ID |
apex.maintainability.unused_method_parameter |
Severity |
info |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Dead Code |
Language |
Apex |
Description
Reports a parameter whose name appears nowhere in its method’s body. Methods declared virtual, override or global, and every method of a class that implements an interface, are skipped, because their signatures are fixed by a contract rather than by the body. A parameter mentioned anywhere in the body counts as used, including where it is only assigned to and including a mention inside a System.runAs block.
Rationale
A parameter the body never touches is a promise the method does not keep. Every call site computes a value, sometimes with a query or a describe call behind it, and passes it in for nothing; the reader at the call site assumes it matters and keeps it up to date. More often the parameter is the remains of an earlier version, and the behaviour it used to control - the currency, the notify flag, the record type - is now hard-coded inside the method while the signature still advertises a choice. Apex has no named arguments, so an ignored parameter in the middle of a list is also a transposition waiting to happen.
The following code illustrates the pattern detected by this rule:
public class QuoteService {
// FLAGGED: Method parameter is never mentioned in the body
public static Decimal total(Decimal amount, String currencyCode) {
return amount * 1.21;
}
// FLAGGED: Method parameter is never mentioned in the body
public static void archive(Id quoteId, Boolean notifyOwner) {
Quote q = new Quote(Id = quoteId, Status__c = 'Archived');
update q;
}
public static Decimal totalFixed(Decimal amount, String currencyCode) {
if (currencyCode == 'EUR') {
return amount * 1.21;
}
return amount;
}
public static List<Quote> byAccount(Id accountId) {
return [SELECT Id FROM Quote WHERE AccountId = :accountId];
}
public static void stamp(Quote q) {
q.Status__c = 'Reviewed';
update q;
}
global static void api(String payload) {
System.debug(LoggingLevel.INFO, 'called');
}
}
Remediation
Remove the parameter and fix the call sites, which is the change the compiler will guide you through. If the value was meant to be used, use it; if the method is deliberately ignoring it to satisfy a shape imposed elsewhere, make that shape explicit - an interface or a virtual base method - so the skip is stated rather than implied.