Unused Method Parameter
ID |
csharp.unused_method_parameter |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
CSharp |
Tags |
code-style, unused-code |
Description
Reports method parameters that are never referenced in the method body. A parameter the method does not use is misleading: callers must still pass a value the method silently ignores.
Rationale
An unused parameter is a maintenance trap. It implies the value matters when it does not, and
it lingers after a refactor that removed the only use. Methods whose signature is fixed by an
external contract are excluded — override, virtual, abstract and partial methods, and
the canonical event-handler shape (object sender, EventArgs e) — because there an unused
parameter is required, not a defect. out/ref parameters and parameter arrays are also
exempt: their value flows out through the binding or is contract-driven.
public class Service
{
public int Add(int a, int b)
{
return a + b; // OK — both used
}
public int First(int a, int b) // FLAW on b — never read
{
return a;
}
public virtual void Hook(int ctx) { } // OK — virtual contract
public void OnClick(object sender, EventArgs e) // OK — event-handler shape
{
Console.WriteLine("clicked");
}
}
Remediation
Remove the parameter and update every call site, or use it if it was meant to participate in the computation. When the signature is mandated by an interface or delegate that this rule did not detect, keep the parameter.