Skip to content

Commit 6ab782d

Browse files
committed
Force block closure with a single control flow body
1 parent a1d28bf commit 6ab782d

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
@@ -621,8 +621,7 @@ fn rewrite_closure(
621621
}
622622

623623
// 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 ||
626625
block_contains_comment(block, context.codemap) ||
627626
prefix.contains('\n');
628627

@@ -632,9 +631,13 @@ fn rewrite_closure(
632631
false
633632
};
634633
if no_return_type && !needs_block {
635-
// lock.stmts.len() == 1
634+
// block.stmts.len() == 1
636635
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+
} {
638641
return Some(rw);
639642
}
640643
}
@@ -2287,7 +2290,23 @@ fn rewrite_last_closure(
22872290
if prefix.contains('\n') {
22882291
return None;
22892292
}
2293+
22902294
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+
22912310
// When overflowing the closure which consists of a single control flow expression,
22922311
// force to use block if its condition uses multi line.
22932312
let is_multi_lined_cond = rewrite_cond(context, body, body_shape)
@@ -2303,6 +2322,23 @@ fn rewrite_last_closure(
23032322
None
23042323
}
23052324

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+
23062342
fn rewrite_last_arg_with_overflow<'a, T>(
23072343
context: &RewriteContext,
23082344
args: &[&T],

0 commit comments

Comments
 (0)