Rust

Rust

Made by DeepSource

Found while with constant condition RS-W1077

Anti-pattern
Minor

A while loop with a constant condition will either never run or enter an infinite loop. If intentional, consider using a loop statement instead.

For example, if the while loop has a condition that is always true, the lint will trigger a warning indicating that the loop is an infinite loop.

On the other hand, if the while loop has a condition that is always false, the lint will trigger a warning indicating that the loop will never run.

Bad practice

fn main() {
    let x = 0;
    while x < 5 {
        println!("x = {}", x);
        x += 1;
    }

    let y = 0;
    while true {
    //    ^^^^ the lint will trigger a warning here
    //         indicating that this condition is not valid
    //         and the while loop will be an infinite loop
        println!("y = {}", y);
        y += 1;
    }
}

Recommended

fn main() {
    let x = 0;
    while x < 5 {
        println!("x = {}", x);
        x += 1;
    }

    let y = 0;
    loop {
    //    either make it explicit with loop
    //    or refactor it if it's a bug
        println!("y = {}", y);
        y += 1;
    }

}