@@ -621,8 +621,7 @@ fn rewrite_closure(
621
621
}
622
622
623
623
// Figure out if the block is necessary.
624
- let needs_block = block. rules != ast:: BlockCheckMode :: Default || block. stmts . len ( ) > 1 ||
625
- context. inside_macro ||
624
+ let needs_block = is_unsafe_block ( block) || block. stmts . len ( ) > 1 || context. inside_macro ||
626
625
block_contains_comment ( block, context. codemap ) ||
627
626
prefix. contains ( '\n' ) ;
628
627
@@ -632,9 +631,13 @@ fn rewrite_closure(
632
631
false
633
632
} ;
634
633
if no_return_type && !needs_block {
635
- // lock .stmts.len() == 1
634
+ // block .stmts.len() == 1
636
635
if let Some ( ref expr) = stmt_expr ( & block. stmts [ 0 ] ) {
637
- if let Some ( rw) = rewrite_closure_expr ( expr, & prefix, context, body_shape) {
636
+ if let Some ( rw) = if is_block_closure_forced ( expr) {
637
+ rewrite_closure_with_block ( context, shape, & prefix, expr)
638
+ } else {
639
+ rewrite_closure_expr ( expr, & prefix, context, body_shape)
640
+ } {
638
641
return Some ( rw) ;
639
642
}
640
643
}
@@ -2287,7 +2290,23 @@ fn rewrite_last_closure(
2287
2290
if prefix. contains ( '\n' ) {
2288
2291
return None ;
2289
2292
}
2293
+
2290
2294
let body_shape = try_opt ! ( shape. offset_left( extra_offset) ) ;
2295
+
2296
+ // We force to use block for the body of the closure for certain kinds of expressions.
2297
+ if is_block_closure_forced ( body) {
2298
+ return rewrite_closure_with_block ( context, body_shape, & prefix, body) . and_then (
2299
+ |body_str| {
2300
+ // If the expression fits in a single line, we need not force block closure.
2301
+ if !body_str. contains ( '\n' ) {
2302
+ rewrite_closure_expr ( body, & prefix, context, shape)
2303
+ } else {
2304
+ Some ( body_str)
2305
+ }
2306
+ } ,
2307
+ ) ;
2308
+ }
2309
+
2291
2310
// When overflowing the closure which consists of a single control flow expression,
2292
2311
// force to use block if its condition uses multi line.
2293
2312
let is_multi_lined_cond = rewrite_cond ( context, body, body_shape)
@@ -2303,6 +2322,23 @@ fn rewrite_last_closure(
2303
2322
None
2304
2323
}
2305
2324
2325
+ fn is_block_closure_forced ( expr : & ast:: Expr ) -> bool {
2326
+ match expr. node {
2327
+ ast:: ExprKind :: If ( ..) |
2328
+ ast:: ExprKind :: IfLet ( ..) |
2329
+ ast:: ExprKind :: Loop ( ..) |
2330
+ ast:: ExprKind :: While ( ..) |
2331
+ ast:: ExprKind :: WhileLet ( ..) |
2332
+ ast:: ExprKind :: ForLoop ( ..) => true ,
2333
+ ast:: ExprKind :: AddrOf ( _, ref expr) |
2334
+ ast:: ExprKind :: Box ( ref expr) |
2335
+ ast:: ExprKind :: Try ( ref expr) |
2336
+ ast:: ExprKind :: Unary ( _, ref expr) |
2337
+ ast:: ExprKind :: Cast ( ref expr, _) => is_block_closure_forced ( expr) ,
2338
+ _ => false ,
2339
+ }
2340
+ }
2341
+
2306
2342
fn rewrite_last_arg_with_overflow < ' a , T > (
2307
2343
context : & RewriteContext ,
2308
2344
args : & [ & T ] ,
0 commit comments