Skip to content

Conversation

sgasho
Copy link
Contributor

@sgasho sgasho commented Aug 26, 2025

closes: #11373
closes: #19443

example:

match val {
    0 | 1 => {
        entry.remove();
    }
    v => *v -= 1,
}

before this PR
else clause never be executed (all val matches if clause)

if let v = val {
    *v -= 1
} else {
    entry.remove();
}

this PR
"Replace match with if let" not shown up.

スクリーンショット 2025-08-27 0 14 30

An alternative proposal and my thoughts of it

Switching the order of "if" and "else" in the above example and replacing "v" with "val" could work like below.

if let 0 | 1 = val {
    entry.remove()
} else {
    *val -= 1
};

However, this idea requires more code, and it seems like over-engineering to me.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 26, 2025
@sgasho sgasho changed the title Fix "Replace match with if let" not to trigger when an invalid transformation occurs Fix "Replace match with if let" not to trigger when invalid transformations occur Aug 26, 2025
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 23, 2025
Merged via the queue into rust-lang:master with commit 905641f Sep 23, 2025
15 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 23, 2025
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.

Replace match with if let produced bogus results Replace match with if let misfires
3 participants