while loop never updates the variables in its own condition

ID

dart.correctness.potential_infinite_loop

Severity

low

Remediation Complexity

medium

Remediation Risk

medium

Remediation Effort

medium

Resource

Infinite Loop

Language

Dart

Description

Reports a while loop comparing two simple variables where the body never assigns to either of them and never leaves the loop with break, return or throw.

Rationale

Nothing the body does can make the condition false, so the loop runs until the isolate is killed. In Dart that is worse than a hung computation: the loop occupies the single event loop, so timers, platform channels and UI frames stop being served and the application freezes rather than slowing down. The usual cause is an increment that was removed by a refactor or moved into a branch that the loop never reaches.

The following code illustrates the pattern detected by this rule:

class Cursor {
  void scan(int limit) {
    var index = 0;
    // FLAGGED: while loop never updates the variables in its own condition
    while (index < limit) {
      emit(index);
    }
  }

  void emit(int v) {}
  int next(int v) => v + 1;
  bool skip(int v) => false;
  bool done(int v) => false;
  Future<void> pump() async {}
}

Remediation

Advance the loop variable in the body, or leave the loop when the work is done. A counting loop is usually clearer as for (var i = 0; i < limit; i++), where the update sits next to the condition and cannot be lost. When the loop should wait for state that changes elsewhere, await that change instead of spinning on it.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved