← back to index

S108 — Nested blocks of code should not be left empty

Language: C#  |  Type: CODE_SMELL  |  Severity: Major

Tags: suspicious

Why is this an issue?

An empty code block is confusing. It will require some effort from maintainers to determine if it is intentional or indicates the implementation is incomplete.

void Foo(int x)
{
  if (x == 42)
  // Noncompliant: the following nested block is empty
  {
  }
  else
  {
    DoSomething();
  }
}
void Foo(int x)
{
  if (x != 42)
  {
    DoSomething();
  }
}

Removing or filling the empty code blocks takes away ambiguity and generally results in a more straightforward and less surprising code.

Exceptions

The rule ignores code blocks that contain comments.

void Foo(int x)
{
  if (x == 42)
  {
    // This is intentionally left empty.
  }
}