Unused method parameter
Why is this an issue?
A parameter that is never referenced in a method or constructor body is a dead input. It misleads anyone reading the signature into thinking the value matters, forces every caller to compute and pass something that is thrown away, and is frequently a sign of a half-completed refactor.
Removing the parameter keeps the signature honest about what the code actually depends on, and simplifies every call site.
Examples
Example of incorrect code:
public class OrderService {
// 'unused' is never referenced in the body
public void process(Order record, Boolean unused) {
update record;
}
}
Example of correct code:
public class OrderService {
public void process(Order record) {
update record;
}
}
How can I fix violations?
Remove the parameter from the method or constructor signature, then update every caller to stop passing the now-removed argument.
If the value is genuinely needed but simply is not wired up yet, use it.
Configuration options
You can configure whether parameters whose name starts with an underscore (for example _unused) are allowed. This is enabled by default, so underscore-prefixed parameters are treated as deliberately unused and are not reported.
When should I disable this rule?
The rule already ignores cases where the parameter cannot be removed:
Methods that override a base-class method.
Methods that implement an interface.
In both of those cases the signature is fixed, so an unused parameter is unavoidable and is not reported.
You might still want to dismiss individual issues where a parameter is retained deliberately. For example to keep a signature stable for external callers you cannot change, or one dictated by a framework the analyzer cannot see. In those cases, renaming the parameter to start with an underscore (for example _event) signals that it is intentionally unused and stops it being reported.
A parameter can also be used as a dynamic SOQL or SOSL bind variable (:parameterName). The rule recognizes these binds when the query text is a string in the same file. If the query text is built up somewhere else the analyzer cannot see (for example a constant or helper in another class), the parameter can look unused even though it is bound at runtime. Dismiss that instance if you know the bind resolves to the parameter.
Resources
