3668: disable invert-if assist for if-let r=matklad a=JoshMcguigan

Fixes #3281 

This disables the invert-if assist for if-let expressions, fixing the bug reported in #3281.

While in the exact case reported in #3281, `if let Some(_) = foo { ...`, it would be possible to invert the if-let pattern, in most cases it will not be possible, so disabling this assist for if-let expressions seems reasonable. 

Co-authored-by: Josh Mcguigan <joshmcg88@gmail.com>
This commit is contained in:
bors[bot] 2020-03-21 13:59:03 +00:00 committed by GitHub
commit 5e827bd948
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -33,6 +33,11 @@ pub(crate) fn invert_if(ctx: AssistCtx) -> Option<Assist> {
return None;
}
// This assist should not apply for if-let.
if expr.condition()?.pat().is_some() {
return None;
}
let cond = expr.condition()?.expr()?;
let then_node = expr.then_branch()?.syntax().clone();
@ -90,4 +95,12 @@ mod tests {
fn invert_if_doesnt_apply_with_cursor_not_on_if() {
check_assist_not_applicable(invert_if, "fn f() { if !<|>cond { 3 * 2 } else { 1 } }")
}
#[test]
fn invert_if_doesnt_apply_with_if_let() {
check_assist_not_applicable(
invert_if,
"fn f() { i<|>f let Some(_) = Some(1) { 1 } else { 0 } }",
)
}
}