@@ -51,7 +51,7 @@ pub(crate) fn rewrite_closure(
51
51
52
52
let result = match fn_decl. output {
53
53
ast:: FunctionRetTy :: Default ( _) if !context. inside_macro ( ) => {
54
- try_rewrite_without_block ( body, & prefix, context, shape, body_shape)
54
+ try_rewrite_without_block ( body, & prefix, capture , context, shape, body_shape)
55
55
}
56
56
_ => None ,
57
57
} ;
@@ -72,13 +72,14 @@ pub(crate) fn rewrite_closure(
72
72
fn try_rewrite_without_block (
73
73
expr : & ast:: Expr ,
74
74
prefix : & str ,
75
+ capture : ast:: CaptureBy ,
75
76
context : & RewriteContext < ' _ > ,
76
77
shape : Shape ,
77
78
body_shape : Shape ,
78
79
) -> Option < String > {
79
80
let expr = get_inner_expr ( expr, prefix, context) ;
80
81
81
- if is_block_closure_forced ( context, expr) {
82
+ if is_block_closure_forced ( context, expr, capture ) {
82
83
rewrite_closure_with_block ( expr, prefix, context, shape)
83
84
} else {
84
85
rewrite_closure_expr ( expr, prefix, context, body_shape)
@@ -357,7 +358,7 @@ pub(crate) fn rewrite_last_closure(
357
358
let body_shape = shape. offset_left ( extra_offset) ?;
358
359
359
360
// We force to use block for the body of the closure for certain kinds of expressions.
360
- if is_block_closure_forced ( context, body) {
361
+ if is_block_closure_forced ( context, body, capture ) {
361
362
return rewrite_closure_with_block ( body, & prefix, context, body_shape) . and_then (
362
363
|body_str| {
363
364
// If the expression can fit in a single line, we need not force block closure.
@@ -404,19 +405,32 @@ pub(crate) fn args_have_many_closure(args: &[OverflowableItem<'_>]) -> bool {
404
405
> 1
405
406
}
406
407
407
- fn is_block_closure_forced ( context : & RewriteContext < ' _ > , expr : & ast:: Expr ) -> bool {
408
+ fn is_block_closure_forced (
409
+ context : & RewriteContext < ' _ > ,
410
+ expr : & ast:: Expr ,
411
+ capture : ast:: CaptureBy ,
412
+ ) -> bool {
408
413
// If we are inside macro, we do not want to add or remove block from closure body.
409
414
if context. inside_macro ( ) {
410
415
false
411
416
} else {
412
- is_block_closure_forced_inner ( expr)
417
+ if let ast:: ExprKind :: Match ( ..) = expr. kind {
418
+ let is_move_closure_without_brace =
419
+ capture == ast:: CaptureBy :: Value && !context. snippet ( expr. span ) . trim ( ) . starts_with ( "{" ) ;
420
+
421
+ is_block_closure_forced_inner ( expr) || is_move_closure_without_brace
422
+ } else {
423
+ is_block_closure_forced_inner ( expr)
424
+ }
413
425
}
414
426
}
415
427
416
428
fn is_block_closure_forced_inner ( expr : & ast:: Expr ) -> bool {
417
429
match expr. kind {
418
- ast:: ExprKind :: If ( ..) | ast:: ExprKind :: While ( ..) | ast:: ExprKind :: ForLoop ( ..) => true ,
419
- ast:: ExprKind :: Loop ( ..) => true ,
430
+ ast:: ExprKind :: If ( ..)
431
+ | ast:: ExprKind :: While ( ..)
432
+ | ast:: ExprKind :: ForLoop ( ..)
433
+ | ast:: ExprKind :: Loop ( ..) => true ,
420
434
ast:: ExprKind :: AddrOf ( _, ref expr)
421
435
| ast:: ExprKind :: Box ( ref expr)
422
436
| ast:: ExprKind :: Try ( ref expr)
0 commit comments