Skip to content

feat: handle if matches!() for replace_if_let_with_match#22079

Merged
ChayimFriedman2 merged 2 commits intorust-lang:masterfrom
A4-Tacks:if-matches-with-match
Apr 26, 2026
Merged

feat: handle if matches!() for replace_if_let_with_match#22079
ChayimFriedman2 merged 2 commits intorust-lang:masterfrom
A4-Tacks:if-matches-with-match

Conversation

@A4-Tacks
Copy link
Copy Markdown
Member

@A4-Tacks A4-Tacks commented Apr 18, 2026

Close #20013

Example

fn foo(x: Result<i32, ()>) {
    $0if matches!(x, Ok(a @ 1..2)) {
    }
}

Before this PR

fn foo(x: Result<i32, ()>) {
    match matches!(x, Ok(a @ 1..2)) {
        true => {}
        false => (),
    }
}

After this PR

fn foo(x: Result<i32, ()>) {
    match x {
        Ok(a@1..2) => {}
        _ => (),
    }
}

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 18, 2026
@A4-Tacks A4-Tacks force-pushed the if-matches-with-match branch from 46ab9fe to d187af3 Compare April 18, 2026 05:21
Example
---
```rust
fn foo(x: Result<i32, ()>) {
    $0if matches!(x, Ok(a @ 1..2)) {
    }
}
```

**Before this PR**

```rust
fn foo(x: Result<i32, ()>) {
    match matches!(x, Ok(a @ 1..2)) {
        true => {}
        false => (),
    }
}
```

**After this PR**

```rust
fn foo(x: Result<i32, ()>) {
    match x {
        Ok(a@1..2) => {}
        _ => (),
    }
}
```
@A4-Tacks A4-Tacks force-pushed the if-matches-with-match branch from d187af3 to 9e61113 Compare April 18, 2026 06:19
Copy link
Copy Markdown
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

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

let input = tt.syntax().children_with_tokens().skip(1).take_while(|it| *it != r_delim);
// Only supports single top-level comma case
let [comma] = input.clone().filter(|it| it.kind() == T![,]).collect_array()?;
let input_rest = input.clone().skip_while(|it| *it != comma).skip(1);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why skip(1)? Is it to skip the whitespace? Better to skip only whitespace.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

  • input.clone().skip_while(|it| *it != comma) outputs ", ..."
  • input.clone().skip_while(|it| *it != comma).skip(1) outputs " ..."

let input_expr = input.clone().take_while(|it| *it != comma).join("");
let input_pat = input_rest.clone().take_while(|it| Some(it) != if_kwd.as_ref());
let input_guard =
if_kwd.as_ref().map(|if_kwd| { input_rest }.skip_while(|it| it != if_kwd).skip(1).join(""));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Here again no skip(1).

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Apr 26, 2026
Merged via the queue into rust-lang:master with commit 5d9bcee Apr 26, 2026
18 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 26, 2026
@A4-Tacks A4-Tacks deleted the if-matches-with-match branch April 26, 2026 12:05
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.

assist: Replace if matches!(..) with match expr

3 participants