Skip to content

never_loop should also remove break labels that were inside the loop #15007

Open
@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::never_loop

this code:

fn main() {
    'a: for _ in 0..1 { break 'a; }
    'a: for _ in 0..1 { break 'a; }
}

caused the following diagnostics:

    Checking _snippet_149 v0.1.0 (/tmp/icemaker_global_tempdir.VQhomPU3g4RT/icemaker_clippyfix_tempdir.kAAqrUJknTKl/_snippet_149)
warning: this loop never actually loops
 --> src/main.rs:2:5
  |
2 |     'a: for _ in 0..1 { break 'a; }
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = 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`
help: if you need the first element of the iterator, try writing
  |
2 -     'a: for _ in 0..1 { break 'a; }
2 +     if let Some(_) = (0..1).next() { break 'a; }
  |

warning: this loop never actually loops
 --> src/main.rs:3:5
  |
3 |     'a: for _ in 0..1 { break 'a; }
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = 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
  |
3 -     'a: for _ in 0..1 { break 'a; }
3 +     if let Some(_) = (0..1).next() { break 'a; }
  |

warning: `_snippet_149` (bin "_snippet_149") generated 2 warnings
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.11s

However after applying these diagnostics, the resulting code:

fn main() {
    if let Some(_) = (0..1).next() { break 'a; }
    if let Some(_) = (0..1).next() { break 'a; }
}

no longer compiled:

    Checking _snippet_149 v0.1.0 (/tmp/icemaker_global_tempdir.VQhomPU3g4RT/icemaker_clippyfix_tempdir.kAAqrUJknTKl/_snippet_149)
error[E0426]: use of undeclared label `'a`
 --> src/main.rs:2:44
  |
2 |     if let Some(_) = (0..1).next() { break 'a; }
  |                                            ^^ undeclared label `'a`

error[E0426]: use of undeclared label `'a`
 --> src/main.rs:3:44
  |
3 |     if let Some(_) = (0..1).next() { break 'a; }
  |                                            ^^ undeclared label `'a`

error[E0268]: `break` outside of a loop or labeled block
 --> src/main.rs:2:38
  |
2 |     if let Some(_) = (0..1).next() { break 'a; }
  |                                      ^^^^^^^^ cannot `break` outside of a loop or labeled block

error[E0268]: `break` outside of a loop or labeled block
 --> src/main.rs:3:38
  |
3 |     if let Some(_) = (0..1).next() { break 'a; }
  |                                      ^^^^^^^^ cannot `break` outside of a loop or labeled block

Some errors have detailed explanations: E0268, E0426.
For more information about an error, try `rustc --explain E0268`.
error: could not compile `_snippet_149` (bin "_snippet_149" test) due to 4 previous errors
warning: build failed, waiting for other jobs to finish...
error: could not compile `_snippet_149` (bin "_snippet_149") due to 4 previous errors

Version:

rustc 1.89.0-nightly (44f415c1d 2025-06-06)
binary: rustc
commit-hash: 44f415c1d617ebc7b931a243b7b321ef8a6ca47c
commit-date: 2025-06-06
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

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