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, the
canonical event-handler shape (object sender, EventArgs e), single-object-parameter BCL
callback shapes such as TimerCallback’s `(object state), and the ASP.NET Core hosting
convention methods (ConfigureServices, ConfigureContainer, Configure on a Startup
class) — 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. A read as the governing subject of a switch expression or through a
null-conditional invocation chain (handler?.Invoke(…)) also counts as a usage.
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");
}
public void OnTick(object state) { } // OK — single-object callback shape
private string DefaultMessageFor(int statusCode) // OK — switch expression subject
{
return statusCode switch
{
400 => "Bad request",
_ => null
};
}
public void Notify(EventHandler handler) // OK — null-conditional invocation
{
handler?.Invoke(this, EventArgs.Empty);
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services) { } // OK — startup convention
}
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.