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
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();
",
)
}
+
+ #[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
+ }
+}
+ ",
+ );
+ }
}
/// 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,
}
}
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) }
}
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 {