]> git.lizzy.rs Git - rust.git/commitdiff
fix unwrap_block by removing double trimming
authorucrhh <ucrrh@sutdent.kit.edu>
Sat, 18 Dec 2021 22:19:53 +0000 (23:19 +0100)
committerucrhh <ucrrh@sutdent.kit.edu>
Sat, 18 Dec 2021 22:19:53 +0000 (23:19 +0100)
crates/ide_assists/src/handlers/unwrap_block.rs

index 9171874e9d4bbaeb5e18e128ccafb63a8b7b8b63..42c5e6f088321df26abf61bac4acab990f0552be 100644 (file)
@@ -6,7 +6,7 @@
     AstNode, SyntaxKind, TextRange, T,
 };
 
-use crate::{utils::unwrap_trivial_block, AssistContext, AssistId, AssistKind, Assists};
+use crate::{AssistContext, AssistId, AssistKind, Assists};
 
 // Assist: unwrap_block
 //
@@ -88,9 +88,8 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
         _ => return None,
     };
 
-    let unwrapped = unwrap_trivial_block(block);
     acc.add(assist_id, assist_label, target, |builder| {
-        builder.replace(parent.syntax().text_range(), update_expr_string(unwrapped.to_string()));
+        builder.replace(parent.syntax().text_range(), update_expr_string(block.to_string()));
     })
 }
 
@@ -672,6 +671,48 @@ fn main() {
 fn main() {
     /* foo */ foo()
 }
+"#,
+        );
+    }
+
+    #[test]
+    fn if_single_statement() {
+        check_assist(
+            unwrap_block,
+            r#"
+fn main() {
+    if true {$0
+        return 3;
+    }
+}
+"#,
+            r#"
+fn main() {
+    return 3;
+}
+"#,
+        );
+    }
+
+    #[test]
+    fn multiple_statements() {
+        check_assist(
+            unwrap_block,
+            r#"
+fn main() -> i32 {
+    if 2 > 1 {$0
+        let a = 5;
+        return 3;
+    }
+    5
+}
+"#,
+            r#"
+fn main() -> i32 {
+    let a = 5;
+    return 3;
+    5
+}
 "#,
         );
     }