Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace-if-let-with-match assist generates non-exhaustive match #10583

Closed
fmease opened this issue Oct 19, 2021 · 7 comments
Closed

Replace-if-let-with-match assist generates non-exhaustive match #10583

fmease opened this issue Oct 19, 2021 · 7 comments
Assignees
Labels
A-pattern pattern handling related things

Comments

@fmease
Copy link
Member

fmease commented Oct 19, 2021

Given the following code:

fn main() {
    if let Some('!') = todo!() {} else {}
}

The assist replace_if_let_with_match currently generates the following match expression:

fn main() {
    match todo!() {
        Some('!') => {}
        None => {}
    }
}

… which is non-exhaustive leading rustc to emit the error E0004 since it misses the case Some(_). I expected the assist to generate something akin to this:

fn main() {
    match todo!() {
        Some('!') => {}
        _ => {}
    }
}

… since I think the assist is meant to map if-lets to semantically equivalent matches. If that's not a requirement, feel free to close this issue.


rust-analyzer version: ed4b312 2021-10-11 stable

@flodiebold
Copy link
Member

Duplicate of #8690, can you try the current nightly?

@Veykril
Copy link
Member

Veykril commented Oct 19, 2021

#10491 only fixed the specific issue there actually, so this one should still be a problem on current nightly.

@fmease
Copy link
Member Author

fmease commented Oct 19, 2021

The same thing happens on nightly.

rust-analyzer version: 87d5ef8 2021-10-19 nightly

What's more, the first example in #8690 (the one involving slice patterns, not the Result<Option<_>, _> one) still generates a non-exhaustive match meaning #10491 only partially addressed the aforementioned issue.

@k-nasa
Copy link
Contributor

k-nasa commented Oct 20, 2021

I tried cover this case by test, but it passed. k-nasa#1
Next, I tried this case on my editor, generates non-exhaustive match.

The phenomenon confuse me. 🤔

@Nadrieril Nadrieril added the A-pattern pattern handling related things label Jan 24, 2024
@profetia
Copy link
Contributor

I just did some investigations. It seems when run this case by test, there is something wrong with types in make_else_arm so it just fell back to wildcard directly. While I tried it on IDE, the types problem did not occur and due to some bugs, maybe uncovered corner cases, in #10491, non-exhausitive match happened.

@profetia
Copy link
Contributor

I will try if I can fix this. It seems to be easy.

@rustbot claim

@lnicola
Copy link
Member

lnicola commented Jan 2, 2025

Fixed in #18797

@lnicola lnicola closed this as completed Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-pattern pattern handling related things
Projects
None yet
Development

No branches or pull requests

7 participants