Empty Function Body
ID |
csharp.empty_function_body |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
CSharp |
Tags |
dead-code, reliability |
Description
Reports a method whose body is an empty { } block (or a block containing only
comments). An empty method silently does nothing while looking like a real
implementation; callers cannot tell that the operation was a no-op.
override methods (the member cannot be omitted) and Razor Pages convention handlers (OnGet on a PageModel, required for routing) are exempt, as are units carrying a generated-code marker (// <auto-generated> header or [GeneratedCode]).
Rationale
A method with an empty body is almost always a sign of an incomplete change: a
stub left after a refactoring, a callback that was never wired up, or a method
that should have been deleted. Comments do not count as statements — a body
that contains only // TODO is still empty and still misleading.
abstract, partial and extern methods are excluded because the language
either forbids a body or expects the implementation to be supplied elsewhere.
public class Service
{
public void Start() // OK — body has statements
{
Initialize();
}
public void Stop() // FLAW — body is empty
{
}
public void Reset()
{ // FLAW — only a comment, not a statement
// TODO: implement
}
public abstract void Tick(); // OK — abstract method has no body
}
Remediation
Decide whether the method should do something, be removed, or be declared
abstract / virtual to make the empty body explicit. If a deliberate no-op
is required (for example to satisfy an interface contract), document the
intent with an executable statement such as a logger call or an explicit
return; plus an explanatory comment.