Skip to content

Commit a6a052f

Browse files
committed
Revert "Apply make_else_arm to general case"
This reverts commit aeee703.
1 parent aeee703 commit a6a052f

File tree

1 file changed

+54
-34
lines changed

1 file changed

+54
-34
lines changed

crates/ide_assists/src/handlers/replace_if_let_with_match.rs

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext)
9393
available_range,
9494
move |edit| {
9595
let match_expr = {
96-
let else_arm = make_else_arm(else_block);
96+
let else_arm = make_else_arm(ctx, else_block, &cond_bodies);
9797
let make_match_arm = |(pat, body): (_, ast::BlockExpr)| {
9898
let body = body.reset_indent().indent(IndentLevel(1));
9999
match pat {
@@ -125,9 +125,30 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext)
125125
)
126126
}
127127

128-
fn make_else_arm(else_block: Option<ast::BlockExpr>) -> ast::MatchArm {
128+
fn make_else_arm(
129+
ctx: &AssistContext,
130+
else_block: Option<ast::BlockExpr>,
131+
conditionals: &[(Either<ast::Pat, ast::Expr>, ast::BlockExpr)],
132+
) -> ast::MatchArm {
129133
if let Some(else_block) = else_block {
130-
let pattern = make::wildcard_pat().into();
134+
let pattern = if let [(Either::Left(pat), _)] = conditionals {
135+
ctx.sema
136+
.type_of_pat(pat)
137+
.and_then(|ty| TryEnum::from_ty(&ctx.sema, &ty.adjusted()))
138+
.zip(Some(pat))
139+
} else {
140+
None
141+
};
142+
let pattern = match pattern {
143+
Some((it, pat)) => {
144+
if does_pat_match_variant(pat, &it.sad_pattern()) {
145+
it.happy_pattern_wildcard()
146+
} else {
147+
it.sad_pattern()
148+
}
149+
}
150+
None => make::wildcard_pat().into(),
151+
};
131152
make::match_arm(iter::once(pattern), None, unwrap_trivial_block(else_block))
132153
} else {
133154
make::match_arm(iter::once(make::wildcard_pat().into()), None, make::expr_unit())
@@ -439,7 +460,7 @@ fn foo(x: Option<i32>) {
439460
fn foo(x: Option<i32>) {
440461
match x {
441462
Some(x) => println!("{}", x),
442-
_ => println!("none"),
463+
None => println!("none"),
443464
}
444465
}
445466
"#,
@@ -464,7 +485,7 @@ fn foo(x: Option<i32>) {
464485
fn foo(x: Option<i32>) {
465486
match x {
466487
None => println!("none"),
467-
_ => println!("some"),
488+
Some(_) => println!("some"),
468489
}
469490
}
470491
"#,
@@ -489,7 +510,7 @@ fn foo(x: Result<i32, ()>) {
489510
fn foo(x: Result<i32, ()>) {
490511
match x {
491512
Ok(x) => println!("{}", x),
492-
_ => println!("none"),
513+
Err(_) => println!("none"),
493514
}
494515
}
495516
"#,
@@ -514,7 +535,7 @@ fn foo(x: Result<i32, ()>) {
514535
fn foo(x: Result<i32, ()>) {
515536
match x {
516537
Err(x) => println!("{}", x),
517-
_ => println!("ok"),
538+
Ok(_) => println!("ok"),
518539
}
519540
}
520541
"#,
@@ -553,33 +574,6 @@ fn main() {
553574
)
554575
}
555576

556-
#[test]
557-
fn replace_if_let_with_match_nested_type() {
558-
check_assist(
559-
replace_if_let_with_match,
560-
r#"
561-
//- minicore: result
562-
fn foo(x: Result<i32, ()>) {
563-
let bar: Result<_, ()> = Ok(Some(1));
564-
$0if let Ok(Some(_)) = bar {
565-
()
566-
} else {
567-
()
568-
}
569-
}
570-
"#,
571-
r#"
572-
fn foo(x: Result<i32, ()>) {
573-
let bar: Result<_, ()> = Ok(Some(1));
574-
match bar {
575-
Ok(Some(_)) => (),
576-
_ => (),
577-
}
578-
}
579-
"#,
580-
);
581-
}
582-
583577
#[test]
584578
fn test_replace_match_with_if_let_unwraps_simple_expressions() {
585579
check_assist(
@@ -891,6 +885,32 @@ fn foo() {
891885
Bar(bar) => println!("bar {}", bar),
892886
}
893887
}
888+
"#,
889+
);
890+
}
891+
892+
#[test]
893+
fn nested_type() {
894+
check_assist(
895+
replace_if_let_with_match,
896+
r#"
897+
//- minicore: result
898+
fn foo(x: Result<i32, ()>) {
899+
let bar: Result<_, ()> = Ok(Some(1));
900+
$0if let Ok(Some(_)) = bar {
901+
()
902+
} else {
903+
()
904+
}
905+
}
906+
"#,
907+
r#"
908+
fn foo(x: Result<i32, ()>) {
909+
let bar: Result<_, ()> = Ok(Some(1));
910+
match bar {
911+
Ok(Some(_)) => (),
912+
_ => (),
913+
}
894914
"#,
895915
);
896916
}

0 commit comments

Comments
 (0)