Skip to content

Conversation

A4-Tacks
Copy link
Contributor

@A4-Tacks A4-Tacks commented Sep 3, 2025

Fixes #20744

Close #20743

  • Add if-let-chain support
  • Add early expression None in function Option return
  • Fix not applicable for if-expr in let-stmt
  • Add applicable in closure

Examples

fn main() {
    if$0 let Ok(x) = Err(92)
        && x < 30
        && let Some(y) = Some(8)
    {
        foo(x, y);
    }
}

->

fn main() {
    let Ok(x) = Err(92) else { return };
    if x >= 30 {
        return;
    }
    let Some(y) = Some(8) else { return };
    foo(x, y);
}

fn main() {
    let _x = loop {
        if$0 let Ok(x) = Err(92) {
            foo(x);
        }
    };
}

Before this PR:

Assist not applicable

After this PR:

fn main() {
    let _x = loop {
        let Ok(x) = Err(92) else { continue };
        foo(x);
    };
}

fn main() {
    let _f = || {
        bar();
        if$0 true {
            foo();

            // comment
            bar();
        }
    }
}

->

fn main() {
    let _f = || {
        bar();
        if false {
            return;
        }
        foo();

        // comment
        bar();
    }
}

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 3, 2025
- And add early expression `None` in function `Option` return

Example
---
```rust
fn main() {
    if$0 let Ok(x) = Err(92)
        && x < 30
        && let Some(y) = Some(8)
    {
        foo(x, y);
    }
}
```
->
```rust
fn main() {
    let Ok(x) = Err(92) else { return };
    if x >= 30 {
        return;
    }
    let Some(y) = Some(8) else { return };
    foo(x, y);
}
```
Example
---
```rust
fn main() {
    let _x = loop {
        if$0 let Ok(x) = Err(92) {
            foo(x);
        }
    };
}
```

**Before**:

Assist not applicable

**After**:

```rust
fn main() {
    let _x = loop {
        let Ok(x) = Err(92) else { continue };
        foo(x);
    };
}
```
Example
---
```rust
fn main() {
    let _f = || {
        bar();
        if$0 true {
            foo();

            // comment
            bar();
        }
    }
}
```
->
```rust
fn main() {
    let _f = || {
        bar();
        if false {
            return;
        }
        foo();

        // comment
        bar();
    }
}
```
@A4-Tacks A4-Tacks force-pushed the let-chain-sup-conv-to-guarded-ret branch from ff02059 to 3964af7 Compare September 25, 2025 14:13
Copy link
Member

@ShoyuVanilla ShoyuVanilla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ShoyuVanilla ShoyuVanilla added this pull request to the merge queue Sep 26, 2025
Merged via the queue into rust-lang:master with commit 997f3ec Sep 26, 2025
15 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 26, 2025
@A4-Tacks A4-Tacks deleted the let-chain-sup-conv-to-guarded-ret branch September 26, 2025 06:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Not applicable on if-expr in let-stmt for convert_to_guarded_return Applicable on closure-expr for convert_to_guarded_return
3 participants