Rust-clippy: infinite loops

Created on 3 May 2015  路  6Comments  路  Source: rust-lang/rust-clippy

There are a few patterns we can search for (while true { ... } without break or return, method calling itself outside of if or closure, or within if true.

This is one of the cases where constant folding would enable us to implement more powerful checks.

E-medium L-lint T-AST

Most helpful comment

In another language I came across a lint today that warns when variables in the loop condition are never mentioned in the loop body. One might be able to extend that to include mutability, as shown in the following examples (although I'm not sure whether this would work well with internal mutability, except case (2)):

fn main() {
    // (1) Clippy could warn: Variables mentioned in the loop condition are immutable
    let y = 0;
    while y < 10 {
        println!("Hello world");
    }

    let mut x = 0;
    x = 1; // ... just to silence "does not need to be mutable" warning

    // (2) Slightly harder: variables in the loop condition are never mentioned in loop body
    while x < 10 {
        println!("Hello world");
    }

    // (3) Still harder: variables used in the loop condition do appear in the loop body,
    // but only as non-mutable function argument
    while x < 10 {
        print_byval(x);
        print_byref(&x);
    }
}

fn print_byval(value: i32) {
    println!("Hello {}", value);
}

fn print_byref(value: &i32) {
    println!("Hello {}", value);
}

All 6 comments

Related: #95

Note that it is pretty common for server-like applications to just run a handler loop infinitely, waiting for termination by a signal. Especially as long as Rust doesn't have a standard signal handling solution that we can detect, this would result in false positives.

No. Rust has a loop { ... } construct for exactly this purpose, which is more readable and less likely to trip others.

Well, while true is already linted by rustc.

In another language I came across a lint today that warns when variables in the loop condition are never mentioned in the loop body. One might be able to extend that to include mutability, as shown in the following examples (although I'm not sure whether this would work well with internal mutability, except case (2)):

fn main() {
    // (1) Clippy could warn: Variables mentioned in the loop condition are immutable
    let y = 0;
    while y < 10 {
        println!("Hello world");
    }

    let mut x = 0;
    x = 1; // ... just to silence "does not need to be mutable" warning

    // (2) Slightly harder: variables in the loop condition are never mentioned in loop body
    while x < 10 {
        println!("Hello world");
    }

    // (3) Still harder: variables used in the loop condition do appear in the loop body,
    // but only as non-mutable function argument
    while x < 10 {
        print_byval(x);
        print_byref(&x);
    }
}

fn print_byval(value: i32) {
    println!("Hello {}", value);
}

fn print_byref(value: &i32) {
    println!("Hello {}", value);
}

@flip1995 this one can be closed, I assume?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

durka picture durka  路  17Comments

Shnatsel picture Shnatsel  路  25Comments

malbarbo picture malbarbo  路  26Comments

BenjaminGill-Metaswitch picture BenjaminGill-Metaswitch  路  22Comments

Manishearth picture Manishearth  路  20Comments