From 9de9693a4566bc60bf6d1c95fff762076fd82987 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Fri, 27 Oct 2017 15:39:15 +0900 Subject: [PATCH 1/2] Add a test rustfmt fails to format a function call when it has a single closure argument and that closure has a block body which contains comments at the beginnig of the block, and the block only contains a single expression as its statement. Phew! --- tests/source/closure.rs | 11 ++++++++++- tests/target/closure.rs | 12 +++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/tests/source/closure.rs b/tests/source/closure.rs index 39059f40807f..5bf177838420 100644 --- a/tests/source/closure.rs +++ b/tests/source/closure.rs @@ -41,7 +41,16 @@ fn main() { let closure_with_return_type = |aaaaaaaaaaaaaaaaaaaaaaarg1, aaaaaaaaaaaaaaaaaaaaaaarg2| -> Strong { "sup".to_owned() }; |arg1, arg2, _, _, arg3, arg4| { let temp = arg4 + arg3; - arg2 * arg1 - temp } + arg2 * arg1 - temp }; + + let block_body_with_comment = args.iter() + .map(|a| { + // Emitting only dep-info is possible only for final crate type, as + // as others may emit required metadata for dependent crate types + if a.starts_with("--emit") && is_final_crate_type && !self.workspace_mode { + "--emit=dep-info" + } else { a } + }); } fn issue311() { diff --git a/tests/target/closure.rs b/tests/target/closure.rs index 9d59bfb64cbd..2005d85bf175 100644 --- a/tests/target/closure.rs +++ b/tests/target/closure.rs @@ -60,7 +60,17 @@ fn main() { |arg1, arg2, _, _, arg3, arg4| { let temp = arg4 + arg3; arg2 * arg1 - temp - } + }; + + let block_body_with_comment = args.iter().map(|a| { + // Emitting only dep-info is possible only for final crate type, as + // as others may emit required metadata for dependent crate types + if a.starts_with("--emit") && is_final_crate_type && !self.workspace_mode { + "--emit=dep-info" + } else { + a + } + }); } fn issue311() { From ab81011a5b4613d154bb31a98cd1d70afd705949 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Fri, 27 Oct 2017 15:41:42 +0900 Subject: [PATCH 2/2] Force to use block for body of closure when it contains comment --- src/expr.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 4eaf52681add..5cd2190f8e33 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -899,9 +899,6 @@ fn rewrite_cond(context: &RewriteContext, expr: &ast::Expr, shape: Shape) -> Opt }; cond.rewrite(context, cond_shape) } - ast::ExprKind::Block(ref block) if block.stmts.len() == 1 => { - stmt_expr(&block.stmts[0]).and_then(|e| rewrite_cond(context, e, shape)) - } _ => to_control_flow(expr, ExprType::SubExpression).and_then(|control_flow| { let alt_block_sep = String::from("\n") + &shape.indent.block_only().to_string(context.config); @@ -2219,7 +2216,7 @@ fn rewrite_last_closure( ) -> Option { if let ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) = expr.node { let body = match body.node { - ast::ExprKind::Block(ref block) if block.stmts.len() == 1 => { + ast::ExprKind::Block(ref block) if is_simple_block(block, context.codemap) => { stmt_expr(&block.stmts[0]).unwrap_or(body) } _ => body,