Skip to content

Commit facba6a

Browse files
rchaser53topecongiro
authored andcommitted
separate by a new line for move || and match (#4007)
1 parent 70ce551 commit facba6a

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

rustfmt-core/rustfmt-lib/src/closures.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub(crate) fn rewrite_closure(
5151

5252
let result = match fn_decl.output {
5353
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)
5555
}
5656
_ => None,
5757
};
@@ -72,13 +72,14 @@ pub(crate) fn rewrite_closure(
7272
fn try_rewrite_without_block(
7373
expr: &ast::Expr,
7474
prefix: &str,
75+
capture: ast::CaptureBy,
7576
context: &RewriteContext<'_>,
7677
shape: Shape,
7778
body_shape: Shape,
7879
) -> Option<String> {
7980
let expr = get_inner_expr(expr, prefix, context);
8081

81-
if is_block_closure_forced(context, expr) {
82+
if is_block_closure_forced(context, expr, capture) {
8283
rewrite_closure_with_block(expr, prefix, context, shape)
8384
} else {
8485
rewrite_closure_expr(expr, prefix, context, body_shape)
@@ -357,7 +358,7 @@ pub(crate) fn rewrite_last_closure(
357358
let body_shape = shape.offset_left(extra_offset)?;
358359

359360
// 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) {
361362
return rewrite_closure_with_block(body, &prefix, context, body_shape).and_then(
362363
|body_str| {
363364
// 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 {
404405
> 1
405406
}
406407

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 {
408413
// If we are inside macro, we do not want to add or remove block from closure body.
409414
if context.inside_macro() {
410415
false
411416
} 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+
}
413425
}
414426
}
415427

416428
fn is_block_closure_forced_inner(expr: &ast::Expr) -> bool {
417429
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,
420434
ast::ExprKind::AddrOf(_, ref expr)
421435
| ast::ExprKind::Box(ref expr)
422436
| ast::ExprKind::Try(ref expr)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
let f = future::poll_fn(
3+
move || match tokio_threadpool::blocking(|| f.poll()).unwrap() {
4+
Async::Ready(v) => v,
5+
Async::NotReady => Ok(Async::NotReady),
6+
},
7+
);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
let f = future::poll_fn(move || {
3+
match tokio_threadpool::blocking(|| f.poll()).unwrap() {
4+
Async::Ready(v) => v,
5+
Async::NotReady => Ok(Async::NotReady),
6+
}
7+
});
8+
}

0 commit comments

Comments
 (0)