@@ -545,13 +545,33 @@ impl Rewrite for ast::Stmt {
545
545
} ;
546
546
547
547
let shape = shape. sub_width ( suffix. len ( ) ) ?;
548
- let expr_type =
549
- if stmt_is_expr ( & self ) && context. snippet ( self . span ) . starts_with ( "if " ) {
550
- ExprType :: SubExpression
551
- } else {
552
- ExprType :: Statement
553
- } ;
554
- format_expr ( ex, expr_type, context, shape) . map ( |s| s + suffix)
548
+ format_expr ( ex, ExprType :: Statement , context, shape) . map ( |s| s + suffix)
549
+ }
550
+ ast:: StmtKind :: Mac ( ..) | ast:: StmtKind :: Item ( ..) => None ,
551
+ } ;
552
+ result. and_then ( |res| recover_comment_removed ( res, self . span ( ) , context) )
553
+ }
554
+ }
555
+
556
+ pub ( crate ) trait RewriteLastStmt {
557
+ fn rewrite_last_stmt ( & self , context : & RewriteContext , shape : Shape ) -> Option < String > ;
558
+ }
559
+
560
+ impl RewriteLastStmt for ast:: Stmt {
561
+ fn rewrite_last_stmt ( & self , context : & RewriteContext , shape : Shape ) -> Option < String > {
562
+ skip_out_of_file_lines_range ! ( context, self . span( ) ) ;
563
+
564
+ let result = match self . node {
565
+ ast:: StmtKind :: Local ( ref local) => local. rewrite ( context, shape) ,
566
+ ast:: StmtKind :: Expr ( ref ex) | ast:: StmtKind :: Semi ( ref ex) => {
567
+ let suffix = if semicolon_for_stmt ( context, self ) {
568
+ ";"
569
+ } else {
570
+ ""
571
+ } ;
572
+
573
+ let shape = shape. sub_width ( suffix. len ( ) ) ?;
574
+ format_expr ( ex, ExprType :: SubExpression , context, shape) . map ( |s| s + suffix)
555
575
}
556
576
ast:: StmtKind :: Mac ( ..) | ast:: StmtKind :: Item ( ..) => None ,
557
577
} ;
@@ -1136,26 +1156,16 @@ pub fn stmt_is_expr(stmt: &ast::Stmt) -> bool {
1136
1156
}
1137
1157
}
1138
1158
1139
- fn stmt_is_if ( stmt : & ast:: Stmt ) -> bool {
1159
+ pub ( crate ) fn stmt_is_if ( stmt : & ast:: Stmt ) -> bool {
1140
1160
match stmt. node {
1141
- ast:: StmtKind :: Semi ( ref e) | ast:: StmtKind :: Expr ( ref e) => {
1142
- match e. node {
1143
- ast:: ExprKind :: If ( ..) => true ,
1144
- _ => false ,
1145
- }
1161
+ ast:: StmtKind :: Semi ( ref e) | ast:: StmtKind :: Expr ( ref e) => match e. node {
1162
+ ast:: ExprKind :: If ( ..) => true ,
1163
+ _ => false ,
1146
1164
} ,
1147
1165
_ => false ,
1148
1166
}
1149
1167
}
1150
1168
1151
- fn block_last_stmt_is_if ( block : & ast:: Block ) -> bool {
1152
- if let Some ( ref stmt) = block. stmts . last ( ) {
1153
- stmt_is_if ( stmt)
1154
- } else {
1155
- false
1156
- }
1157
- }
1158
-
1159
1169
pub fn is_unsafe_block ( block : & ast:: Block ) -> bool {
1160
1170
if let ast:: BlockCheckMode :: Unsafe ( ..) = block. rules {
1161
1171
true
0 commit comments