Empty branch of conditional, or empty loop bodyΒΆ
ID: cs/empty-block
Kind: problem
Security severity:
Severity: warning
Precision: high
Tags:
- reliability
- readability
Query suites:
- csharp-security-and-quality.qls
Click to see the query in the CodeQL repository
Empty blocks or blocks with only code that has been commented out should be reviewed. It is common to find commented-out code in an empty block. Commented-out code is discouraged because it is a source of defects and maintainability issues.
RecommendationΒΆ
The appropriate solution depends on the exact situation. If the block should be doing something, add an implementation (where possible). If the block is redundant, remove it. Note that some refactoring may then be required: for example, if the βthenβ branch of an if is empty, it should be removed, and the sense of the condition of the if should be inverted to simplify the code.
ExampleΒΆ
In this example the entire βthenβ block of the if statement has been commented out. The block should be removed and the sense of the if condition inverted.
using System;
class EmptyBlock
{
static void Main(string[] args)
{
int i = 23;
if (i == 42)
{
// TODO: Currently still research problems - professors aware.
#if false
AchieveEnlightenment();
CureCancer();
world.SetPeaceful(true);
#endif
}
else
{
Console.WriteLine("Welcome to this meeting of the Realist Society. Join us!");
}
// After removing the empty block:
if (i != 42)
{
Console.WriteLine("Welcome to this meeting of the Realist Society. Join us!");
}
}
}