about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2023-02-08 12:03:11 +0100
committerLukas Wirth <lukastw97@gmail.com>2023-02-08 12:06:35 +0100
commit5e6208b1dfdca994420f1d3b9b964ed2bc08b0b3 (patch)
tree7d3f9d6daf733a030c28977ea04a37406156df45
parent3c89945e78c5f164c03cabb1c5cd459a86ef963a (diff)
downloadrust-5e6208b1dfdca994420f1d3b9b964ed2bc08b0b3.tar.gz
rust-5e6208b1dfdca994420f1d3b9b964ed2bc08b0b3.zip
fix: Don't insert a semicolon when typing = if parse errors are encountered
-rw-r--r--crates/ide/src/typing.rs51
1 files changed, 31 insertions, 20 deletions
diff --git a/crates/ide/src/typing.rs b/crates/ide/src/typing.rs
index c2654875625..c7e403f6b1a 100644
--- a/crates/ide/src/typing.rs
+++ b/crates/ide/src/typing.rs
@@ -253,6 +253,10 @@ fn on_eq_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
         if file.syntax().text().slice(offset..expr_range.start()).contains_char('\n') {
             return None;
         }
+        // Good indicator that we will insert into a bad spot, so bail out.
+        if expr.syntax().descendants().any(|it| it.kind() == SyntaxKind::ERROR) {
+            return None;
+        }
         let offset = let_stmt.syntax().text_range().end();
         Some(TextEdit::insert(offset, ";".to_string()))
     }
@@ -407,15 +411,14 @@ mod tests {
 
     #[test]
     fn test_semi_after_let() {
-        //     do_check(r"
-        // fn foo() {
-        //     let foo =$0
-        // }
-        // ", r"
-        // fn foo() {
-        //     let foo =;
-        // }
-        // ");
+        type_char_noop(
+            '=',
+            r"
+fn foo() {
+    let foo =$0
+}
+",
+        );
         type_char(
             '=',
             r#"
@@ -429,17 +432,25 @@ fn foo() {
 }
 "#,
         );
-        //     do_check(r"
-        // fn foo() {
-        //     let foo =$0
-        //     let bar = 1;
-        // }
-        // ", r"
-        // fn foo() {
-        //     let foo =;
-        //     let bar = 1;
-        // }
-        // ");
+        type_char_noop(
+            '=',
+            r#"
+fn foo() {
+    let difference $0(counts: &HashMap<(char, char), u64>, last: char) -> u64 {
+        // ...
+    }
+}
+"#,
+        );
+        type_char_noop(
+            '=',
+            r"
+fn foo() {
+    let foo =$0
+    let bar = 1;
+}
+",
+        );
     }
 
     #[test]