Clippy lints / Suspicious
Clippy lint · Suspicious · checked with Clippy 0.1.99
clippy::for_unbounded_range: for loop on unbounded range
What it meansChecks for unbounded for loops over char or integers. It is in the suspicious group and warns by default.
What you see
warning: for loop on unbounded range --> main.rs:3:1 | 3 | / for i in 250u8.. { 4 | | println!("{i}"); 5 | | } | |_^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#for_unbounded_range = note: `#[warn(clippy::for_unbounded_range)]` on by default help: for loops over unbounded ranges will wrap around and may panic, consider adding an inclusive high bound | 3 | for i in 250u8..=u8::MAX { | ++++++++
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?
Using a unbounded range over char and integers will unexpectedly not handle overflows so it will lead to panics or infinite loops. Instead there should be a max value set, usually the MAX constant for a given type such as '\0'..char::MAX or 250..u8::MAX.
Code that is most likely wrong or useless. Warns by default.
How to fix it
Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:
Flagged
for i in 250u8.. {
println!("{i}");
}
Write this instead
for i in 250u8..=u8::MAX {
println!("{i}");
}
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::for_unbounded_range)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
for_unbounded_range = "allow"One run
cargo clippy -- -A clippy::for-unbounded-rangeThe [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 for unbounded for loops over char or integers.
Why is this bad?
Using a unbounded range over char and integers will unexpectedly not handle overflows so it will lead to panics or infinite loops.
Instead there should be a max value set, usually the MAX constant for a given type such as '\0'..char::MAX
or 250..u8::MAX.
Example
for i in 250u8.. {
println!("{i}");
}
Use instead:
for i in 250u8..=u8::MAX {
println!("{i}");
}
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- empty_line_after_outer_attrempty line after outer attribute
- empty_loopempty `loop {}` wastes CPU cycles
- float_equality_without_absfloat equality check without `.abs()`
- forget_non_dropcall to `std::mem::forget` with a value that does not implement `Drop`. Forgetting such a type is the same as dropping it
- four_forward_slashesthis item has comments with 4 forward slashes (`////`). These look like doc comments, but they aren't
- from_raw_with_void_ptrChecks if we’re passing a c_void raw pointer to {Box,Rc,Arc,Weak}::from_raw(_)
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::empty_line_after_outer_attrempty line after outer attributeRead it next →Did this answer your question?