Skip to content

Commit 24e3725

Browse files
committed
Force block closure with a single control flow body
1 parent 0ee76be commit 24e3725

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

src/expr.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,7 @@ fn rewrite_closure(
669669
}
670670

671671
// 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 ||
674673
block_contains_comment(block, context.codemap) ||
675674
prefix.contains('\n');
676675

@@ -680,9 +679,13 @@ fn rewrite_closure(
680679
false
681680
};
682681
if no_return_type && !needs_block {
683-
// lock.stmts.len() == 1
682+
// block.stmts.len() == 1
684683
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+
} {
686689
return Some(rw);
687690
}
688691
}
@@ -2335,7 +2338,23 @@ fn rewrite_last_closure(
23352338
if prefix.contains('\n') {
23362339
return None;
23372340
}
2341+
23382342
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+
23392358
// When overflowing the closure which consists of a single control flow expression,
23402359
// force to use block if its condition uses multi line.
23412360
let is_multi_lined_cond = rewrite_cond(context, body, body_shape)
@@ -2351,6 +2370,23 @@ fn rewrite_last_closure(
23512370
None
23522371
}
23532372

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+
23542390
fn rewrite_last_arg_with_overflow<'a, T>(
23552391
context: &RewriteContext,
23562392
args: &[&T],

0 commit comments

Comments
 (0)