@@ -43,15 +43,35 @@ pub fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr {
4343
4444pub fn extract_trivial_expression ( block : & ast:: BlockExpr ) -> Option < ast:: Expr > {
4545 let block = block. block ( ) ?;
46- let expr = block. expr ( ) ?;
47- let non_trivial_children = block. syntax ( ) . children ( ) . filter ( |it| match it. kind ( ) {
48- WHITESPACE | T ! [ '{' ] | T ! [ '}' ] => false ,
49- _ => it != expr. syntax ( ) ,
50- } ) ;
51- if non_trivial_children. count ( ) > 0 {
52- return None ;
46+ let has_anything_else = |thing : & SyntaxNode | -> bool {
47+ let mut non_trivial_children =
48+ block. syntax ( ) . children_with_tokens ( ) . filter ( |it| match it. kind ( ) {
49+ WHITESPACE | T ! [ '{' ] | T ! [ '}' ] => false ,
50+ _ => it. as_node ( ) != Some ( thing) ,
51+ } ) ;
52+ non_trivial_children. next ( ) . is_some ( )
53+ } ;
54+
55+ if let Some ( expr) = block. expr ( ) {
56+ if has_anything_else ( expr. syntax ( ) ) {
57+ return None ;
58+ }
59+ return Some ( expr) ;
60+ } else {
61+ // Unwrap `{ continue; }`
62+ let ( stmt, ) = block. statements ( ) . next_tuple ( ) ?;
63+ if has_anything_else ( stmt. syntax ( ) ) {
64+ return None ;
65+ }
66+ if let ast:: Stmt :: ExprStmt ( expr_stmt) = stmt {
67+ let expr = expr_stmt. expr ( ) ?;
68+ match expr. syntax ( ) . kind ( ) {
69+ CONTINUE_EXPR | BREAK_EXPR | RETURN_EXPR => return Some ( expr) ,
70+ _ => ( ) ,
71+ }
72+ }
5373 }
54- Some ( expr )
74+ None
5575}
5676
5777pub fn compute_ws ( left : SyntaxKind , right : SyntaxKind ) -> & ' static str {
0 commit comments