]> git.lizzy.rs Git - rust.git/commitdiff
fix 2190; add test for "replace if let with match"
authorFelix Kohlgrüber <felix.kohlgrueber@gmail.com>
Wed, 20 Nov 2019 18:01:06 +0000 (19:01 +0100)
committerFelix Kohlgrüber <felix.kohlgrueber@gmail.com>
Wed, 20 Nov 2019 18:01:06 +0000 (19:01 +0100)
crates/ra_assists/src/assists/replace_if_let_with_match.rs
crates/ra_fmt/src/lib.rs
crates/ra_ide_api/src/join_lines.rs

index dff84d86516cba7aaaa26c8ccc399bf3190c7a5a..3272801ff81081738ae1eb5310051b53d187968a 100644 (file)
@@ -66,8 +66,8 @@ fn build_match_expr(
 
 fn format_arm(block: &ast::BlockExpr) -> String {
     match extract_trivial_expression(block) {
-        None => block.syntax().text().to_string(),
-        Some(e) => format!("{},", e.syntax().text()),
+        Some(e) if !e.syntax().text().contains_char('\n') => format!("{},", e.syntax().text()),
+        _ => block.syntax().text().to_string(),
     }
 }
 
@@ -102,6 +102,34 @@ pub fn is_struct(&self) -> bool {
         )
     }
 
+    #[test]
+    fn test_replace_if_let_with_match_doesnt_unwrap_multiline_expressions() {
+        check_assist(
+            replace_if_let_with_match,
+            "
+fn foo() {
+    if <|>let VariantData::Struct(..) = a {
+        bar(
+            123
+        )
+    } else {
+        false
+    }
+}           ",
+            "
+fn foo() {
+    <|>match a {
+        VariantData::Struct(..) => {
+            bar(
+                123
+            )
+        }
+        _ => false,
+    }
+}           ",
+        )
+    }
+
     #[test]
     fn replace_if_let_with_match_target() {
         check_assist_target(
index a30ed4cbb863ea052a2a39392a215f12e823774e..10f592257c13ecaee117a95cf9377b97b73a31e3 100644 (file)
@@ -38,9 +38,6 @@ fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
 pub fn extract_trivial_expression(expr: &ast::BlockExpr) -> Option<ast::Expr> {
     let block = expr.block()?;
     let expr = block.expr()?;
-    if expr.syntax().text().contains_char('\n') {
-        return None;
-    }
     let non_trivial_children = block.syntax().children().filter(|it| match it.kind() {
         WHITESPACE | T!['{'] | T!['}'] => false,
         _ => it != expr.syntax(),
index 6f71b27db265ca12a09168fd31c3e012eeaaa247..7deeb34947aceea7eb5a0fca825560d7f1d368d6 100644 (file)
@@ -243,6 +243,34 @@ fn foo(e: Result<U, V>) {
         );
     }
 
+    #[test]
+    fn join_lines_multiline_in_block() {
+        check_join_lines(
+            r"
+fn foo() {
+    match ty {
+        <|> Some(ty) => {
+            match ty {
+                _ => false,
+            }
+        }
+        _ => true,
+    }
+}
+",
+            r"
+fn foo() {
+    match ty {
+        <|> Some(ty) => match ty {
+                _ => false,
+            },
+        _ => true,
+    }
+}
+",
+        );
+    }
+
     #[test]
     fn join_lines_keeps_comma_for_block_in_match_arm() {
         // We already have a comma