@@ -1102,8 +1102,14 @@ impl<'a> Rewrite for ControlFlow<'a> {
11021102 } ;
11031103 let block_str = {
11041104 let old_val = context. is_if_else_block . replace ( self . else_block . is_some ( ) ) ;
1105- let result =
1106- rewrite_block_with_visitor ( context, "" , self . block , None , None , block_shape, true ) ;
1105+ let allow_single_line =
1106+ allow_single_line_if ( & cond_str, self . block ) && self . keyword == "if" ;
1107+
1108+ let result = if allow_single_line {
1109+ rewrite_block_inner ( self . block , None , None , true , context, block_shape)
1110+ } else {
1111+ rewrite_block_with_visitor ( context, "" , self . block , None , None , block_shape, true )
1112+ } ;
11071113 context. is_if_else_block . replace ( old_val) ;
11081114 result?
11091115 } ;
@@ -1158,6 +1164,30 @@ impl<'a> Rewrite for ControlFlow<'a> {
11581164 }
11591165}
11601166
1167+ fn allow_single_line_if ( result : & str , block : & ast:: Block ) -> bool {
1168+ if result. contains ( '\n' ) {
1169+ return false ;
1170+ }
1171+
1172+ if block. stmts . len ( ) == 0 {
1173+ return true ;
1174+ }
1175+ if block. stmts . len ( ) == 1 {
1176+ return is_simple_stmt ( & block. stmts [ 0 ] ) ;
1177+ }
1178+ false
1179+ }
1180+
1181+ fn is_simple_stmt ( stmt : & ast:: Stmt ) -> bool {
1182+ match stmt. kind {
1183+ ast:: StmtKind :: Expr ( ref expr) => match expr. kind {
1184+ ast:: ExprKind :: Ret ( ..) | ast:: ExprKind :: Continue ( ..) | ast:: ExprKind :: Break ( ..) => true ,
1185+ _ => false ,
1186+ } ,
1187+ _ => false ,
1188+ }
1189+ }
1190+
11611191fn rewrite_label ( opt_label : Option < ast:: Label > ) -> Cow < ' static , str > {
11621192 match opt_label {
11631193 Some ( label) => Cow :: from ( format ! ( "{}: " , label. ident) ) ,
0 commit comments