Email me

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-range

The [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.

Suspiciousgroup
warndefault level
nofixed by cargo clippy --fix
1.98.0added in Rust

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

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?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ