Clippy lints / Correctness
Clippy lint · Correctness · checked with Clippy 0.1.99
clippy::while_immutable_condition: variables in the condition are not mutated in the loop body
What it meansChecks whether variables used within while loop condition can be (and are) mutated in the body. It is in the correctness group and denied by default (the build fails).
What you see
error: variables in the condition are not mutated in the loop body --> main.rs:4:7 | 4 | while i > 10 { | ^^^^^^ | = note: this may lead to an infinite or to a never running loop = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#while_immutable_condition = note: `#[deny(clippy::while_immutable_condition)]` on by default
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
If the condition is unchanged, entering the body of the loop will lead to an infinite loop.
Code that is almost certainly wrong. Denied by default: the build fails until you fix it or allow it.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
let i = 0;
while i > 10 {
println!("let me loop forever!");
}
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::while_immutable_condition)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
while_immutable_condition = "allow"One run
cargo clippy -- -A clippy::while-immutable-conditionThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Checks whether variables used within while loop condition can be (and are) mutated in the body.
Why is this bad?
If the condition is unchanged, entering the body of the loop will lead to an infinite loop.
Known problems
If the while-loop is in a closure, the check for mutation of the
condition variables in the body can cause false negatives. For example when only Upvar a is
in the condition and only Upvar b gets mutated in the body, the lint will not trigger.
Example
let i = 0;
while i > 10 {
println!("let me loop forever!");
}
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- unused_io_amountwritten amount is not handled
- useless_attributeChecks for extern crate and use items annotated with lint attributes. This lint permits lint attributes for lints emitted on the items themself. For use items these lints are: ambiguous_glob_reexports dead_code deprecated hidden_glob_reexports unreachable_pub unused unused_braces unused_import_braces clippy::disallowed_types clippy::enum_glob_use clippy::macro_use_imports clippy::module_name_repetitions clippy::redundant_pub_crate clippy::single_component_path_imports clippy::unsafe_removed_from_name clippy::wildcard_imports For extern crate items these lints are: unused_imports on items with #[macro_use]
- vec_resize_to_zeroFinds occurrences of Vec::resize(0, an_int)
- wrong_transmuteChecks for transmutes that can’t ever be correct on any architecture
- zst_offsetoffset calculation on zero-sized value
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::unused_io_amountwritten amount is not handledRead it next →Did this answer your question?