rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

`never_loop`: Also check all known iterator reductions.

Open Jarcho opened this issue 1 month ago • 4 comments

Description

never_loop currently only checks loop bodies (loop, while and for), but iterator reduction functions that always diverge are just as likely to be bugs.

fn f(x: i32) -> ! {
    panic!();
}

[0, 1].into_iter().for_each(|x| f(x)); // Only calls `f` once

If not looping is actually intended using if let Some(x) = _.next() is much clearer that we do something if any item exists. This suggestion should not be MachineApplicable since looping was likely intended.

fn f(x: i32) -> ! {
    panic!();
}

if let Some(x) = [0, 1].into_iter().next() {
    f(x); 
}

See #16054 for an example of a loop that always diverged without it being immediately obvious. If that were written using for_each instead of a for loop it would still be a bug.

Version

clippy 0.1.93 (6647be9364 2025-11-09)

Jarcho avatar Nov 10 '25 15:11 Jarcho

Hey @Jarcho , this looks like a nice starter task. If you’re not actively on it right now, would you mind if I take a stab at it?

cuongleqq avatar Nov 24 '25 01:11 cuongleqq

Hi, just following up @Jarcho. Would it be okay if I start a PR for #16061 as my first contribution?

cuongleqq avatar Dec 09 '25 14:12 cuongleqq

Hello!

I am interested in working on this issue. I am looking to start contributing to the Rust project and this seems like a great entry point for me to learn the codebase and contribution process.

Could I please be assigned this issue?

Thank you!

Chirag-Janbandhu avatar Dec 11 '25 15:12 Chirag-Janbandhu

Hey @Chirag-Janbandhu, just a quick heads‑up: I’ve already started working on this and opened a PR here: #16222 . If you haven’t started yet, you might want to pick another good first issue so we don’t duplicate the same work. But if you’re curious, feel free to check the PR or suggest extra test cases / ideas there. 🙂

cuongleqq avatar Dec 12 '25 03:12 cuongleqq