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) {}
}

Remediation

Remove the loop if nothing needs repeating, or move the statement that belongs in the body back inside it. When the empty body is deliberate — a spin on state another isolate updates, or a loop whose whole effect is in its condition — put a comment in the block saying so.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved