]> git.lizzy.rs Git - rust.git/commitdiff
Fix a bunch of false-positives in join-lines
authorAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 30 Apr 2020 19:36:31 +0000 (21:36 +0200)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 30 Apr 2020 20:08:50 +0000 (22:08 +0200)
crates/ra_fmt/src/lib.rs
crates/ra_ide/src/join_lines.rs
crates/ra_syntax/src/ast/expr_extensions.rs
crates/ra_syntax/src/ast/generated/nodes.rs
xtask/src/ast_src.rs

index 0b4ba1bbe6541a05fddcf1418c34e4526287e4e2..1a30b2b3ab8721e7f7249e3ded2070ad0d1c9738 100644 (file)
@@ -57,18 +57,17 @@ pub fn extract_trivial_expression(block: &ast::BlockExpr) -> Option<ast::Expr> {
             return None;
         }
         return Some(expr);
-    } else {
-        // Unwrap `{ continue; }`
-        let (stmt,) = block.statements().next_tuple()?;
-        if let ast::Stmt::ExprStmt(expr_stmt) = stmt {
-            if has_anything_else(expr_stmt.syntax()) {
-                return None;
-            }
-            let expr = expr_stmt.expr()?;
-            match expr.syntax().kind() {
-                CONTINUE_EXPR | BREAK_EXPR | RETURN_EXPR => return Some(expr),
-                _ => (),
-            }
+    }
+    // Unwrap `{ continue; }`
+    let (stmt,) = block.statements().next_tuple()?;
+    if let ast::Stmt::ExprStmt(expr_stmt) = stmt {
+        if has_anything_else(expr_stmt.syntax()) {
+            return None;
+        }
+        let expr = expr_stmt.expr()?;
+        match expr.syntax().kind() {
+            CONTINUE_EXPR | BREAK_EXPR | RETURN_EXPR => return Some(expr),
+            _ => (),
         }
     }
     None
index fde0bfa98b34d3820f2a80d2e7b760b432d72243..d0def7eaafd091e3301f045a6cf2549abb66461f 100644 (file)
@@ -131,6 +131,9 @@ fn has_comma_after(node: &SyntaxNode) -> bool {
 fn join_single_expr_block(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Option<()> {
     let block = ast::Block::cast(token.parent())?;
     let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?;
+    if !block_expr.is_standalone() {
+        return None;
+    }
     let expr = extract_trivial_expression(&block_expr)?;
 
     let block_range = block_expr.syntax().text_range();
@@ -662,4 +665,67 @@ fn main() {
         ",
         )
     }
+
+    #[test]
+    fn join_lines_mandatory_blocks_block() {
+        check_join_lines(
+            r"
+<|>fn foo() {
+    92
+}
+        ",
+            r"
+<|>fn foo() { 92
+}
+        ",
+        );
+
+        check_join_lines(
+            r"
+fn foo() {
+    <|>if true {
+        92
+    }
+}
+        ",
+            r"
+fn foo() {
+    <|>if true { 92
+    }
+}
+        ",
+        );
+
+        check_join_lines(
+            r"
+fn foo() {
+    <|>loop {
+        92
+    }
+}
+        ",
+            r"
+fn foo() {
+    <|>loop { 92
+    }
+}
+        ",
+        );
+
+        check_join_lines(
+            r"
+fn foo() {
+    <|>unsafe {
+        92
+    }
+}
+        ",
+            r"
+fn foo() {
+    <|>unsafe { 92
+    }
+}
+        ",
+        );
+    }
 }
index 93aa3d45faf4f0bdd8b3834b11232d58e857a096..ecf74fd3662fdb1f7fd51d6d33a5fe05c1b3ffd1 100644 (file)
@@ -368,12 +368,15 @@ impl ast::BlockExpr {
     /// const FOO: () = { stand_alone };
     /// ```
     pub fn is_standalone(&self) -> bool {
+        if self.unsafe_token().is_some() || self.async_token().is_some() {
+            return false;
+        }
         let kind = match self.syntax().parent() {
             None => return true,
             Some(it) => it.kind(),
         };
         match kind {
-            FN_DEF | MATCH_ARM | IF_EXPR | WHILE_EXPR | LOOP_EXPR | TRY_BLOCK_EXPR => false,
+            FN_DEF | IF_EXPR | WHILE_EXPR | LOOP_EXPR | TRY_BLOCK_EXPR => false,
             _ => true,
         }
     }
index 3b5e05af92eb671c0540b2f92f24dfea2baa042f..d2253d4af2710f89fc18aeee2a803bd9abe1e120 100644 (file)
@@ -554,6 +554,7 @@ impl ast::AttrsOwner for BlockExpr {}
 impl BlockExpr {
     pub fn label(&self) -> Option<Label> { support::child(&self.syntax) }
     pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) }
+    pub fn async_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![async]) }
     pub fn block(&self) -> Option<Block> { support::child(&self.syntax) }
 }
 
index 98c8644e455e9c7c9bbd1e57c5c072f6a269875b..c14804aad67e5ebee2f6dae244f5db1149aac4a8 100644 (file)
@@ -451,7 +451,7 @@ struct WhileExpr: AttrsOwner, LoopBodyOwner { T![while], Condition }
         struct ContinueExpr: AttrsOwner { T![continue], T![lifetime] }
         struct BreakExpr: AttrsOwner { T![break], T![lifetime], Expr }
         struct Label { T![lifetime] }
-        struct BlockExpr: AttrsOwner { Label, T![unsafe], Block  }
+        struct BlockExpr: AttrsOwner { Label, T![unsafe], T![async], Block  }
         struct ReturnExpr: AttrsOwner { Expr }
         struct CallExpr: ArgListOwner { Expr }
         struct MethodCallExpr: AttrsOwner, ArgListOwner {