]> git.lizzy.rs Git - rust.git/commitdiff
Revert "Apply make_else_arm to general case"
authork-nasa <htilcs1115@gmail.com>
Wed, 13 Oct 2021 11:36:04 +0000 (20:36 +0900)
committerk-nasa <htilcs1115@gmail.com>
Wed, 13 Oct 2021 11:36:04 +0000 (20:36 +0900)
This reverts commit aeee70397e8230117f0097fa8624d59d910bd324.

crates/ide_assists/src/handlers/replace_if_let_with_match.rs

index 5b766ecbeae2e2c3ba95a6ee16704017e42b6d15..6f383ae8bba94e7450cf20874cce62df09f256e9 100644 (file)
@@ -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"),
     }
 }
 "#,
@@ -553,33 +574,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(
@@ -891,6 +885,32 @@ fn foo() {
         Bar(bar) => println!("bar {}", bar),
     }
 }
+"#,
+        );
+    }
+
+    #[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(_)) => (),
+        _ => (),
+    }
 "#,
         );
     }