Skip to content

Commit aeee703

Browse files
committed
Apply make_else_arm to general case
1 parent 388525f commit aeee703

File tree

1 file changed

+34
-54
lines changed

1 file changed

+34
-54
lines changed

crates/ide_assists/src/handlers/replace_if_let_with_match.rs

Lines changed: 34 additions & 54 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(ctx, else_block, &cond_bodies);
96+
let else_arm = make_else_arm(else_block);
9797
let make_match_arm = |(pat, body): (_, ast::BlockExpr)| {
9898
let body = body.reset_indent().indent(IndentLevel(1));
9999
match pat {
@@ -125,30 +125,9 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext)
125125
)
126126
}
127127

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 {
128+
fn make_else_arm(else_block: Option<ast::BlockExpr>) -> ast::MatchArm {
133129
if let Some(else_block) = else_block {
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-
};
130+
let pattern = make::wildcard_pat().into();
152131
make::match_arm(iter::once(pattern), None, unwrap_trivial_block(else_block))
153132
} else {
154133
make::match_arm(iter::once(make::wildcard_pat().into()), None, make::expr_unit())
@@ -460,7 +439,7 @@ fn foo(x: Option<i32>) {
460439
fn foo(x: Option<i32>) {
461440
match x {
462441
Some(x) => println!("{}", x),
463-
None => println!("none"),
442+
_ => println!("none"),
464443
}
465444
}
466445
"#,
@@ -485,7 +464,7 @@ fn foo(x: Option<i32>) {
485464
fn foo(x: Option<i32>) {
486465
match x {
487466
None => println!("none"),
488-
Some(_) => println!("some"),
467+
_ => println!("some"),
489468
}
490469
}
491470
"#,
@@ -510,7 +489,7 @@ fn foo(x: Result<i32, ()>) {
510489
fn foo(x: Result<i32, ()>) {
511490
match x {
512491
Ok(x) => println!("{}", x),
513-
Err(_) => println!("none"),
492+
_ => println!("none"),
514493
}
515494
}
516495
"#,
@@ -535,7 +514,7 @@ fn foo(x: Result<i32, ()>) {
535514
fn foo(x: Result<i32, ()>) {
536515
match x {
537516
Err(x) => println!("{}", x),
538-
Ok(_) => println!("ok"),
517+
_ => println!("ok"),
539518
}
540519
}
541520
"#,
@@ -574,6 +553,33 @@ fn main() {
574553
)
575554
}
576555

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+
577583
#[test]
578584
fn test_replace_match_with_if_let_unwraps_simple_expressions() {
579585
check_assist(
@@ -885,32 +891,6 @@ fn foo() {
885891
Bar(bar) => println!("bar {}", bar),
886892
}
887893
}
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-
}
914894
"#,
915895
);
916896
}

0 commit comments

Comments
 (0)