Skip to content

fix: handle control flows for convert_iter_for_each_to_for#22752

Open
A4-Tacks wants to merge 1 commit into
rust-lang:masterfrom
A4-Tacks:iter-for-control-flows
Open

fix: handle control flows for convert_iter_for_each_to_for#22752
A4-Tacks wants to merge 1 commit into
rust-lang:masterfrom
A4-Tacks:iter-for-control-flows

Conversation

@A4-Tacks

@A4-Tacks A4-Tacks commented Jul 9, 2026

Copy link
Copy Markdown
Member

Fixes #11111

Example

fn main() {
    let x = vec![1, 2, 3];
    for $0v in x {
        if v == 2 {
            continue;
        }
        if v == 3 {
            break;
        }
        v *= 2;
    }
}

Before this PR

fn main() {
    let x = vec![1, 2, 3];
    x.into_iter().for_each(|v| {
        if v == 2 {
            continue;
        }
        if v == 3 {
            break;
        }
        v *= 2;
    });
}

After this PR

fn main() {
    let x = vec![1, 2, 3];
    x.into_iter().try_for_each(|v| {
        if v == 2 {
            return ControlFlow::Continue(());
        }
        if v == 3 {
            return ControlFlow::Break(());
        }
        v *= 2;
        ControlFlow::Continue(())
    });
}

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 9, 2026
Example
---
```rust
fn main() {
    let x = vec![1, 2, 3];
    for $0v in x {
        if v == 2 {
            continue;
        }
        if v == 3 {
            break;
        }
        v *= 2;
    }
}
```

**Before this PR**

```rust
fn main() {
    let x = vec![1, 2, 3];
    x.into_iter().for_each(|v| {
        if v == 2 {
            continue;
        }
        if v == 3 {
            break;
        }
        v *= 2;
    });
}
```

**After this PR**

```rust
fn main() {
    let x = vec![1, 2, 3];
    x.into_iter().try_for_each(|v| {
        if v == 2 {
            return ControlFlow::Continue(());
        }
        if v == 3 {
            return ControlFlow::Break(());
        }
        v *= 2;
        ControlFlow::Continue(())
    });
}
```
@A4-Tacks A4-Tacks force-pushed the iter-for-control-flows branch from 5806438 to 481f6d3 Compare July 9, 2026 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

convert_for_loop_with_for_each does not take control flow in account

2 participants