Skip to content

Incorrect fix (loop labels lost) when turning while loop to for loop #13123

Closed
@VorpalBlade

Description

@VorpalBlade

Summary

It seems that when clippy tries to fix this:

warning: this loop could be written as a `for` loop
  --> srclib.rs:3:5
   |
37 |     'label: while let Some(line) = lines.next() {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for line in lines`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#while_let_on_iterator
   = note: `#[warn(clippy::while_let_on_iterator)]` on by default

it forgets about the label. Leading to the fixed code to not compile.

Reproducer

I tried this code:

fn dummy(input: &str) {
    let mut lines = input.lines();
    'label: while let Some(line) = lines.next() {
        // For this example the label isn't needed, my real program does
        // (nested loops) but I wanted to create a minimal example!
        // So please don't comment on that, it would be counter-productive.
        if line == "foo" {
            break 'label;
        }
        println!("{}", line);
    }
}

I expected to see this happen:

When using --fix it should create code like:

fn dummy(input: &str) {
    let mut lines = input.lines();
    'label: for line in lines {
        // For this example the label isn't needed, my real program does
        // (nested loops) but I wanted to create a minimal example!
        // So please don't comment on that, it would be counter-productive.
        if line == "foo" {
            break 'label;
        }
        println!("{}", line);
    }
}

Instead, this happened:

warning: failed to automatically apply fixes suggested by rustc to crate `dummy`

after fixes were automatically applied the compiler reported errors within these files:

  * src/lib.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0426]: use of undeclared label `'label`
  --> src/lib.rs:8:19
  |
8 |             break 'label;
  |                   ^^^^^^ undeclared label `'label`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0426`.

Version

rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions