Unnamed repository; edit this file 'description' to name the repository.
Revert "Apply make_else_arm to general case"
This reverts commit aeee70397e8230117f0097fa8624d59d910bd324.
k-nasa 2021-10-13
parent aeee703 · commit a6a052f
-rw-r--r--crates/ide_assists/src/handlers/replace_if_let_with_match.rs88
1 files changed, 54 insertions, 34 deletions
diff --git a/crates/ide_assists/src/handlers/replace_if_let_with_match.rs b/crates/ide_assists/src/handlers/replace_if_let_with_match.rs
index 5b766ecbea..6f383ae8bb 100644
--- a/crates/ide_assists/src/handlers/replace_if_let_with_match.rs
+++ b/crates/ide_assists/src/handlers/replace_if_let_with_match.rs
@@ -93,7 +93,7 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext)
available_range,
move |edit| {
let match_expr = {
- let else_arm = make_else_arm(else_block);
+ let else_arm = make_else_arm(ctx, else_block, &cond_bodies);
let make_match_arm = |(pat, body): (_, ast::BlockExpr)| {
let body = body.reset_indent().indent(IndentLevel(1));
match pat {
@@ -125,9 +125,30 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext)
)
}
-fn make_else_arm(else_block: Option<ast::BlockExpr>) -> ast::MatchArm {
+fn make_else_arm(
+ ctx: &AssistContext,
+ else_block: Option<ast::BlockExpr>,
+ conditionals: &[(Either<ast::Pat, ast::Expr>, ast::BlockExpr)],
+) -> ast::MatchArm {
if let Some(else_block) = else_block {
- let pattern = make::wildcard_pat().into();
+ let pattern = if let [(Either::Left(pat), _)] = conditionals {
+ ctx.sema
+ .type_of_pat(pat)
+ .and_then(|ty| TryEnum::from_ty(&ctx.sema, &ty.adjusted()))
+ .zip(Some(pat))
+ } else {
+ None
+ };
+ let pattern = match pattern {
+ Some((it, pat)) => {
+ if does_pat_match_variant(pat, &it.sad_pattern()) {
+ it.happy_pattern_wildcard()
+ } else {
+ it.sad_pattern()
+ }
+ }
+ None => make::wildcard_pat().into(),
+ };
make::match_arm(iter::once(pattern), None, unwrap_trivial_block(else_block))
} else {
make::match_arm(iter::once(make::wildcard_pat().into()), None, make::expr_unit())
@@ -439,7 +460,7 @@ fn foo(x: Option<i32>) {
fn foo(x: Option<i32>) {
match x {
Some(x) => println!("{}", x),
- _ => println!("none"),
+ None => println!("none"),
}
}
"#,
@@ -464,7 +485,7 @@ fn foo(x: Option<i32>) {
fn foo(x: Option<i32>) {
match x {
None => println!("none"),
- _ => println!("some"),
+ Some(_) => println!("some"),
}
}
"#,
@@ -489,7 +510,7 @@ fn foo(x: Result<i32, ()>) {
fn foo(x: Result<i32, ()>) {
match x {
Ok(x) => println!("{}", x),
- _ => println!("none"),
+ Err(_) => println!("none"),
}
}
"#,
@@ -514,7 +535,7 @@ fn foo(x: Result<i32, ()>) {
fn foo(x: Result<i32, ()>) {
match x {
Err(x) => println!("{}", x),
- _ => println!("ok"),
+ Ok(_) => println!("ok"),
}
}
"#,
@@ -554,33 +575,6 @@ fn main() {
}
#[test]
- fn replace_if_let_with_match_nested_type() {
- check_assist(
- replace_if_let_with_match,
- r#"
-//- minicore: result
-fn foo(x: Result<i32, ()>) {
- let bar: Result<_, ()> = Ok(Some(1));
- $0if let Ok(Some(_)) = bar {
- ()
- } else {
- ()
- }
-}
-"#,
- r#"
-fn foo(x: Result<i32, ()>) {
- let bar: Result<_, ()> = Ok(Some(1));
- match bar {
- Ok(Some(_)) => (),
- _ => (),
- }
-}
-"#,
- );
- }
-
- #[test]
fn test_replace_match_with_if_let_unwraps_simple_expressions() {
check_assist(
replace_match_with_if_let,
@@ -894,4 +888,30 @@ fn foo() {
"#,
);
}
+
+ #[test]
+ fn nested_type() {
+ check_assist(
+ replace_if_let_with_match,
+ r#"
+//- minicore: result
+fn foo(x: Result<i32, ()>) {
+ let bar: Result<_, ()> = Ok(Some(1));
+ $0if let Ok(Some(_)) = bar {
+ ()
+ } else {
+ ()
+ }
+}
+"#,
+ r#"
+fn foo(x: Result<i32, ()>) {
+ let bar: Result<_, ()> = Ok(Some(1));
+ match bar {
+ Ok(Some(_)) => (),
+ _ => (),
+ }
+"#,
+ );
+ }
}