while condition whose variables are never updated in the body

ID

rust.correctness.while_immutable_condition

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

low

Resource

Control Flow

Language

Rust

Description

Reports a while loop whose condition compares two simple variables and whose body never assigns to either of them, never borrows one mutably, and never leaves the loop with break or return. Such a loop spins forever.

Rationale

The condition is re-evaluated on every iteration, but nothing in the body can change its outcome, so the loop either never runs or never ends. In a release build the thread hangs while consuming a core; in a service it holds whatever locks and connections it acquired before entering the loop, so the failure spreads well beyond the spinning thread and is hard to attribute back to this code.

The following code illustrates the pattern detected by this rule:

fn current_usage() -> usize { 0 }
fn wait_for_capacity(limit: usize) {
    let used = current_usage();
    // FLAGGED: while condition whose variables are never updated in the body
    while used < limit {
        std::thread::sleep(std::time::Duration::from_millis(10));
    }
}

Remediation

Update the loop variable in the body (i += 1), recompute the value the condition reads from, or exit explicitly with break when the terminating event occurs. When the loop waits on another thread, use a channel, Condvar or an atomic with acquire/release ordering rather than re-reading a plain local.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved