Skip to content

never_loop removes label that the replaced loop was attached to (outer label) #15350

@matthiaskrgr

Description

@matthiaskrgr

followup to #15007 I guess ?

Using the following flags

--force-warn clippy::never_loop

this code:

//@ run-pass

pub fn main() {
    'foo: loop {
        loop {
            break 'foo;
        }
    }

    'bar: for _ in 0..100 {
        loop {
            break 'bar;
        }
    }

    'foobar: while 1 + 1 == 2 {
        loop {
            break 'foobar;
        }
    }
}

caused the following diagnostics:

    Checking _labeled-break v0.1.0 (/tmp/icemaker_global_tempdir.0gE5D6KlClUx/icemaker_clippyfix_tempdir.hvDqeCsLHElq/_labeled-break)
warning: this loop never actually loops
 --> src/main.rs:4:5
  |
4 | /     'foo: loop {
5 | |         loop {
6 | |             break 'foo;
7 | |         }
8 | |     }
  | |_____^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#never_loop
  = note: requested on the command line with `--force-warn clippy::never-loop`

warning: this loop never actually loops
 --> src/main.rs:5:9
  |
5 | /         loop {
6 | |             break 'foo;
7 | |         }
  | |_________^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#never_loop

warning: this loop never actually loops
  --> src/main.rs:10:5
   |
10 | /     'bar: for _ in 0..100 {
11 | |         loop {
12 | |             break 'bar;
13 | |         }
14 | |     }
   | |_____^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#never_loop
help: if you need the first element of the iterator, try writing
   |
10 -     'bar: for _ in 0..100 {
10 +     if let Some(_) = (0..100).next() {
   |

warning: this loop never actually loops
  --> src/main.rs:11:9
   |
11 | /         loop {
12 | |             break 'bar;
13 | |         }
   | |_________^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#never_loop

warning: this loop never actually loops
  --> src/main.rs:16:5
   |
16 | /     'foobar: while 1 + 1 == 2 {
17 | |         loop {
18 | |             break 'foobar;
19 | |         }
20 | |     }
   | |_____^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#never_loop

warning: this loop never actually loops
  --> src/main.rs:17:9
   |
17 | /         loop {
18 | |             break 'foobar;
19 | |         }
   | |_________^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#never_loop

warning: `_labeled-break` (bin "_labeled-break") generated 6 warnings
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.42s

However after applying these diagnostics, the resulting code:

//@ run-pass

pub fn main() {
    'foo: loop {
        loop {
            break 'foo;
        }
    }

    if let Some(_) = (0..100).next() {
        loop {
            break 'bar;
        }
    }

    'foobar: while 1 + 1 == 2 {
        loop {
            break 'foobar;
        }
    }
}

no longer compiled:

    Checking _labeled-break v0.1.0 (/tmp/icemaker_global_tempdir.0gE5D6KlClUx/icemaker_clippyfix_tempdir.hvDqeCsLHElq/_labeled-break)
error[E0426]: use of undeclared label `'bar`
  --> src/main.rs:12:19
   |
12 |             break 'bar;
   |                   ^^^^ undeclared label `'bar`

For more information about this error, try `rustc --explain E0426`.
error: could not compile `_labeled-break` (bin "_labeled-break") due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_labeled-break` (bin "_labeled-break" test) due to 1 previous error

Version:

rustc 1.90.0-nightly (f32b23204 2025-07-26)
binary: rustc
commit-hash: f32b23204a0efe2fe8383ed4be1a30b56c1bbf94
commit-date: 2025-07-26
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions