@@ -669,8 +669,7 @@ fn rewrite_closure(
669
669
}
670
670
671
671
// Figure out if the block is necessary.
672
- let needs_block = block. rules != ast:: BlockCheckMode :: Default || block. stmts . len ( ) > 1 ||
673
- context. inside_macro ||
672
+ let needs_block = is_unsafe_block ( block) || block. stmts . len ( ) > 1 || context. inside_macro ||
674
673
block_contains_comment ( block, context. codemap ) ||
675
674
prefix. contains ( '\n' ) ;
676
675
@@ -680,9 +679,13 @@ fn rewrite_closure(
680
679
false
681
680
} ;
682
681
if no_return_type && !needs_block {
683
- // lock .stmts.len() == 1
682
+ // block .stmts.len() == 1
684
683
if let Some ( ref expr) = stmt_expr ( & block. stmts [ 0 ] ) {
685
- if let Some ( rw) = rewrite_closure_expr ( expr, & prefix, context, body_shape) {
684
+ if let Some ( rw) = if is_block_closure_forced ( expr) {
685
+ rewrite_closure_with_block ( context, shape, & prefix, expr)
686
+ } else {
687
+ rewrite_closure_expr ( expr, & prefix, context, body_shape)
688
+ } {
686
689
return Some ( rw) ;
687
690
}
688
691
}
@@ -2335,7 +2338,23 @@ fn rewrite_last_closure(
2335
2338
if prefix. contains ( '\n' ) {
2336
2339
return None ;
2337
2340
}
2341
+
2338
2342
let body_shape = try_opt ! ( shape. offset_left( extra_offset) ) ;
2343
+
2344
+ // We force to use block for the body of the closure for certain kinds of expressions.
2345
+ if is_block_closure_forced ( body) {
2346
+ return rewrite_closure_with_block ( context, body_shape, & prefix, body) . and_then (
2347
+ |body_str| {
2348
+ // If the expression fits in a single line, we need not force block closure.
2349
+ if !body_str. contains ( '\n' ) {
2350
+ rewrite_closure_expr ( body, & prefix, context, shape)
2351
+ } else {
2352
+ Some ( body_str)
2353
+ }
2354
+ } ,
2355
+ ) ;
2356
+ }
2357
+
2339
2358
// When overflowing the closure which consists of a single control flow expression,
2340
2359
// force to use block if its condition uses multi line.
2341
2360
let is_multi_lined_cond = rewrite_cond ( context, body, body_shape)
@@ -2351,6 +2370,23 @@ fn rewrite_last_closure(
2351
2370
None
2352
2371
}
2353
2372
2373
+ fn is_block_closure_forced ( expr : & ast:: Expr ) -> bool {
2374
+ match expr. node {
2375
+ ast:: ExprKind :: If ( ..) |
2376
+ ast:: ExprKind :: IfLet ( ..) |
2377
+ ast:: ExprKind :: Loop ( ..) |
2378
+ ast:: ExprKind :: While ( ..) |
2379
+ ast:: ExprKind :: WhileLet ( ..) |
2380
+ ast:: ExprKind :: ForLoop ( ..) => true ,
2381
+ ast:: ExprKind :: AddrOf ( _, ref expr) |
2382
+ ast:: ExprKind :: Box ( ref expr) |
2383
+ ast:: ExprKind :: Try ( ref expr) |
2384
+ ast:: ExprKind :: Unary ( _, ref expr) |
2385
+ ast:: ExprKind :: Cast ( ref expr, _) => is_block_closure_forced ( expr) ,
2386
+ _ => false ,
2387
+ }
2388
+ }
2389
+
2354
2390
fn rewrite_last_arg_with_overflow < ' a , T > (
2355
2391
context : & RewriteContext ,
2356
2392
args : & [ & T ] ,
0 commit comments