rust-lang / rust-lang/rust-clippy

`clippy::never_loop` suggests

Open
#12,931 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

L-suggestion
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

In some cases, clippy::never_loop suggests a replacement that would still trigger clippy::iter_skip_next.

Minimizing clippy feedback loops seems important to avoid user frustration. Therefore, it seems valuable to modify never_loop suggestions based on a check against iter_skip_next (and maybe other lints?), to ensure the modified snippet won't get rejected again.

Reproducer

I tried this code in the Rust playground and ran Clippy:

fn main() {
    for thing in [1, 2, 3].iter().skip(1) {
        panic!("oh noes, too many things");
    }
}

I expected to see this happen:

    Checking playground v0.0.1 (/playground)
warning: unused variable: `thing`
<snip>

error: this loop never actually loops
 --> src/main.rs:2:5
  |
2 | /     for thing in [1, 2, 3].iter().skip(1) {
3 | |         panic!("oh noes, too many things");
4 | |     }
  | |_____^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#never_loop
  = note: `#[deny(clippy::never_loop)]` on by default
help: if you need the first element of the iterator, try writing
  |
2 |     if let Some(thing) = [1, 2, 3].iter().nth(1) {
  |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

warning: `playground` (bin "playground") generated 1 warning
error: could not compile `playground` (bin "playground") due to 1 previous error; 1 warning emitted

Instead, this happened:

    Checking playground v0.0.1 (/playground)
warning: unused variable: `thing`
<snip>

error: this loop never actually loops
 --> src/main.rs:2:5
  |
2 | /     for thing in [1, 2, 3].iter().skip(1) {
3 | |         panic!("oh noes, too many things");
4 | |     }
  | |_____^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#never_loop
  = note: `#[deny(clippy::never_loop)]` on by default
help: if you need the first element of the iterator, try writing
  |
2 |     if let Some(thing) = [1, 2, 3].iter().skip(1).next() {
  |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

warning: `playground` (bin "playground") generated 1 warning
error: could not compile `playground` (bin "playground") due to 1 previous error; 1 warning emitted
Version
rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: aarch64-apple-darwin
release: 1.78.0
LLVM version: 18.1.2
Additional Labels

@rustbot label +I-suggestion-causes-error +L-suggestion

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported example with clippy::never_loop and inspect how its suggestion interacts with clippy::iter_skip_next. The work is done when the suggested replacement no longer produces the reported follow-up lint, with regression coverage for the reproducer.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.