From e7240f5e24f083aebe3cb32964e943a618210b1e Mon Sep 17 00:00:00 2001 From: topecongiro Date: Fri, 16 Jun 2017 08:28:12 +0900 Subject: [PATCH] Add and remove comments --- src/expr.rs | 45 +++++++++++++++++++++++--------------- tests/target/issue-1681.rs | 5 +++-- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 22f963e0609..92c62b08125 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -590,6 +590,7 @@ fn rewrite_closure_fn_decl( }; 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() { @@ -682,6 +683,7 @@ fn rewrite_closure( } } +// Rewrite closure with a single expression wrapping its body with block. fn rewrite_closure_with_block( context: &RewriteContext, shape: Shape, @@ -703,6 +705,7 @@ fn rewrite_closure_with_block( 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, @@ -716,6 +719,7 @@ fn rewrite_closure_expr( rewrite.map(|rw| format!("{} {}", prefix, rw)) } +// Rewrite closure whose body is block. fn rewrite_closure_block( block: &ast::Block, prefix: &str, @@ -2217,24 +2221,29 @@ where // 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), } diff --git a/tests/target/issue-1681.rs b/tests/target/issue-1681.rs index a13f323e88f..5734fd5502d 100644 --- a/tests/target/issue-1681.rs +++ b/tests/target/issue-1681.rs @@ -1,7 +1,9 @@ // 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()), @@ -13,7 +15,6 @@ fn foo() { Ok(entry.insert(try!(statement))) } - // and now, casually call a method on this } }).map(MaybeCached::Cached) }