Loop body is empty
ID |
dart.correctness.empty_loop_body |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Suspicious Construct |
Language |
Dart |
Description
Reports while, for, for-in and do-while loops whose body is an empty block with no statement and no explanatory comment.
Rationale
A loop that executes nothing is almost always a mistake in the making: the statement that belonged in the body was moved out and left below the loop, or a refactor emptied the block and the loop was not removed with it. Meanwhile the loop still costs what it always did — the condition is evaluated on every iteration, and a while whose condition nothing inside can change spins until the isolate is killed. A reader also cannot tell an intentional spin from a half-finished edit.
The following code illustrates the pattern detected by this rule:
class Poller {
void drain(int pending) {
// FLAGGED: Loop body is empty
while (pending > 0) {
}
}
void handle(String name) {}
}