Skip to content

clippy::single_element_loop fails to detect issue when if is added to loop #16510

Description

@parkercoates

Summary

Context: https://www.reddit.com/r/rust/comments/1qvmm3l/guess_how_long_it_took_to_spot_this_little/

I posted a rant about a dumb mistake. Got lots of replies telling me Clippy definitely would have caught the issue. Clippy did not catch the issue. ☹️

Lint Name

clippy::single_element_loop

Reproducer

I tried this code:

fn f(print: bool) {
    for _ in [..5] {
        if print {
            println!("Hello from f");
        }
    }
}

I expected to see this happen:

warning: this loops only once with `_` being `..5`
 --> src/main.rs:2:14
  |
2 |     for _ in [..5] {
  |              ^^^^^ help: did you mean to iterate over the range instead?: `..5`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.93.0/index.html#single_element_loop
  = note: `#[warn(clippy::single_element_loop)]` on by default

Instead, this happened:

Version

rustc 1.93.0 (254b59607 2026-01-19)
binary: rustc
commit-hash: 254b59607d4417e9dffbc307138ae5c86280fe4c
commit-date: 2026-01-19
host: aarch64-apple-darwin
release: 1.93.0
LLVM version: 21.1.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn't

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions