CodeQL documentation

Empty branch of conditional

ID: cpp/empty-block
Kind: problem
Severity: recommendation
Precision: very-high
Tags:
   - reliability
   - readability
Query suites:
   - cpp-security-and-quality.qls

Click to see the query in the CodeQL repository

This rule finds empty blocks that occur as a branch of a conditional or as a loop body. This may indicate badly maintained code or a defect due to an unhandled case. It is common to find commented-out code in the empty body. Commented-out code is discouraged and is a source of defects and maintainability issues.

Recommendation

If the conditional or loop is useless, remove it.

If only the else-branch of an if statement is empty, omit it. If the then-branch is empty, invert the sense of the condition.

Example

void f(int i) {
	if (i == 10); //empty then block
		... //won't be part of the if statement

	if (i == 12) {
		...
	} else { //empty else block, most likely a mistake
	}
}