Loop body is empty
ID |
scala.correctness.empty_loop_body |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Suspicious Construct |
Language |
Scala |
Description
Reports a while, do/while or for loop whose body is an empty block, so the loop runs purely for the side effects of its condition or its generators.
Rationale
An empty body is almost always a defect rather than a technique. A while with an empty body and a condition that does not change spins forever and burns a core; a for with an empty body iterates a collection and does nothing with it, which usually means the work was deleted or never written. In the rare case where the loop is deliberately a busy-wait, the intent is invisible to the reader.
The following code illustrates the pattern detected by this rule:
class EmptyLoopBody {
def spin(ready: Boolean): Unit = {
// FLAGGED: Loop body is empty
while (!ready) {}
}
}