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

Replacing if-let with match can lead to match expression with non-exhaustive patterns #8690

Closed
bpandreotti opened this issue Apr 29, 2021 · 4 comments · Fixed by #10491 or #18797
Closed
Labels
A-assists E-has-instructions Issue has some instructions and pointers to code to get started S-actionable Someone could pick this issue up and work on it right now

Comments

@bpandreotti
Copy link

I tried using "replace with match" on the following examples:

let foo: Option<&[_]> = Some(&[0, 1, 2]);
if let Some([]) = foo {
    ()
} else {
    ()
}

let bar: Result<_, ()> = Ok(Some(1));
if let Ok(Some(_)) = bar {
    ()
} else {
    ()
}

And got this as a result:

let foo: Option<&[_]> = Some(&[0, 1, 2]);
match foo {
    Some([]) => (),
    None => (),
}

let bar: Result<_, ()> = Ok(Some(1));
match bar {
    Ok(Some(_)) => (),
    Err(_) => (),
}

For both cases, I expected the pattern in the second match arm to be the wildcard pattern _. This seems to only happen if the pattern in the if-let expression is a nested Option or Result.

@Veykril
Copy link
Member

Veykril commented Apr 29, 2021

We are trying to be too smart here and only check the outer pattern for Option/Result, so we dont check whether the inner pattern is exhaustive or not here https://github.com/rust-analyzer/rust-analyzer/blob/80bee14e14f67f02746befff77a8a4bbfd3e5849/crates/ide_assists/src/handlers/replace_if_let_with_match.rs#L71-L78

@Veykril Veykril added A-assists E-has-instructions Issue has some instructions and pointers to code to get started S-actionable Someone could pick this issue up and work on it right now labels Apr 29, 2021
@flodiebold
Copy link
Member

Do we actually need the special handling for Option/Result? We could probably have some "make the alternative pattern as specific as possible" logic.

@Veykril
Copy link
Member

Veykril commented May 2, 2021

Mmh, ye ideally we wouldn't special case Option and Result. We should be able to make this more general

@lnicola
Copy link
Member

lnicola commented Oct 19, 2021

The first case still doesn't work: #10583 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-assists E-has-instructions Issue has some instructions and pointers to code to get started S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
4 participants