Empty Loop Body
ID |
java.empty_loop_body |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
CWE:1071, reliability |
Description
Reports for, while, and do-while loops whose body is empty (either a bare semicolon or an empty block).
Rationale
An empty loop body is almost always a bug. A stray semicolon after the loop header turns the intended body into a separate statement that runs exactly once, outside the loop. Even when an empty loop is intentional (e.g., a busy-wait spin loop), the intent should be made explicit with a comment inside the braces.
// Bad -- stray semicolon makes the loop body empty
for (int i = 0; i < 10; i++);
process(i); // This runs once, not 10 times!
// Bad -- empty block
while (queue.poll() != null) {
}