]> git.lizzy.rs Git - rust.git/commitdiff
Fix joinLines panic if run on the empty last line
authorEdwin Cheng <edwin0cheng@gmail.com>
Sat, 3 Apr 2021 03:20:16 +0000 (11:20 +0800)
committerEdwin Cheng <edwin0cheng@gmail.com>
Sat, 3 Apr 2021 03:20:16 +0000 (11:20 +0800)
crates/ide/src/join_lines.rs

index d584190f7a873044cdd067e6f65eb6fab9106195..fe2a349e63f2daf291202bc4ea2eac2b4a63c7ce 100644 (file)
@@ -88,8 +88,11 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextS
     }
 
     // The node is between two other nodes
-    let prev = token.prev_sibling_or_token().unwrap();
-    let next = token.next_sibling_or_token().unwrap();
+    let (prev, next) = match (token.prev_sibling_or_token(), token.next_sibling_or_token()) {
+        (Some(prev), Some(next)) => (prev, next),
+        _ => return,
+    };
+
     if is_trailing_comma(prev.kind(), next.kind()) {
         // Removes: trailing comma, newline (incl. surrounding whitespace)
         edit.delete(TextRange::new(prev.text_range().start(), token.text_range().end()));
@@ -826,6 +829,17 @@ fn main() {
 $0hello world
 ";
 }
+"#,
+        );
+    }
+    #[test]
+    fn join_last_line_empty() {
+        check_join_lines(
+            r#"
+fn main() {$0}
+"#,
+            r#"
+fn main() {$0}
 "#,
         );
     }