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.
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?
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)):