};
let list_str = try_opt!(write_list(&item_vec, &fmt));
let mut prefix = format!("{}|{}|", mover, list_str);
+ // 1 = space between `|...|` and body.
let extra_offset = extra_offset(&prefix, shape) + 1;
if !ret_str.is_empty() {
}
}
+// Rewrite closure with a single expression wrapping its body with block.
fn rewrite_closure_with_block(
context: &RewriteContext,
shape: Shape,
rewrite_closure_block(&block, prefix, context, shape)
}
+// Rewrite closure with a single expression without wrapping its body with block.
fn rewrite_closure_expr(
expr: &ast::Expr,
prefix: &str,
rewrite.map(|rw| format!("{} {}", prefix, rw))
}
+// Rewrite closure whose body is block.
fn rewrite_closure_block(
block: &ast::Block,
prefix: &str,
// When overflowing the closure which consists of a single control flow expression,
// force to use block if its condition uses multi line.
ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) => {
- if rewrite_cond(context, body, shape).map_or(false, |cond| cond.contains('\n')) {
- rewrite_closure_fn_decl(capture, fn_decl, body, expr.span, context, shape)
- .and_then(|(prefix, extra_offset)| {
- // 1 = space between `|...|` and body.
- shape.offset_left(extra_offset).and_then(|body_shape| {
- let body = match body.node {
- ast::ExprKind::Block(ref block) => {
- stmt_expr(&block.stmts[0]).unwrap()
- }
- _ => body,
- };
- rewrite_closure_with_block(context, body_shape, &prefix, body)
- })
- })
- .or_else(|| expr.rewrite(context, shape))
- } else {
- expr.rewrite(context, shape)
- }
+ let try_closure_with_block = || {
+ let body = match body.node {
+ ast::ExprKind::Block(ref block) if block.stmts.len() == 1 => {
+ try_opt!(stmt_expr(&block.stmts[0]))
+ }
+ _ => body,
+ };
+ let (prefix, extra_offset) = try_opt!(rewrite_closure_fn_decl(
+ capture,
+ fn_decl,
+ body,
+ expr.span,
+ context,
+ shape,
+ ));
+ let shape = try_opt!(shape.offset_left(extra_offset));
+ rewrite_cond(context, body, shape).map_or(None, |cond| if cond.contains('\n') {
+ rewrite_closure_with_block(context, shape, &prefix, body)
+ } else {
+ None
+ })
+ };
+ try_closure_with_block().or_else(|| expr.rewrite(context, shape))
}
_ => expr.rewrite(context, shape),
}
// rustfmt-max_width: 80
+// We would like to surround closure body with block when overflowing the last
+// argument of function call if the last argument has condition and without
+// block it may go multi lines.
fn foo() {
- // This is where it gets good
refmut_map_result(self.cache.borrow_mut(), |cache| {
match cache.entry(cache_key) {
Occupied(entry) => Ok(entry.into_mut()),
Ok(entry.insert(try!(statement)))
}
- // and now, casually call a method on this
}
}).map(MaybeCached::Cached)
}