Skip to content

Commit f159d32

Browse files
authored
Merge pull request #2089 from topecongiro/closure-block-body-with-comment
Fix rustfmt failing to format closure block body with comment
2 parents 6cfeb1f + ab81011 commit f159d32

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/expr.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -899,9 +899,6 @@ fn rewrite_cond(context: &RewriteContext, expr: &ast::Expr, shape: Shape) -> Opt
899899
};
900900
cond.rewrite(context, cond_shape)
901901
}
902-
ast::ExprKind::Block(ref block) if block.stmts.len() == 1 => {
903-
stmt_expr(&block.stmts[0]).and_then(|e| rewrite_cond(context, e, shape))
904-
}
905902
_ => to_control_flow(expr, ExprType::SubExpression).and_then(|control_flow| {
906903
let alt_block_sep =
907904
String::from("\n") + &shape.indent.block_only().to_string(context.config);
@@ -2228,7 +2225,7 @@ fn rewrite_last_closure(
22282225
) -> Option<String> {
22292226
if let ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) = expr.node {
22302227
let body = match body.node {
2231-
ast::ExprKind::Block(ref block) if block.stmts.len() == 1 => {
2228+
ast::ExprKind::Block(ref block) if is_simple_block(block, context.codemap) => {
22322229
stmt_expr(&block.stmts[0]).unwrap_or(body)
22332230
}
22342231
_ => body,

tests/source/closure.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,16 @@ fn main() {
4141
let closure_with_return_type = |aaaaaaaaaaaaaaaaaaaaaaarg1, aaaaaaaaaaaaaaaaaaaaaaarg2| -> Strong { "sup".to_owned() };
4242

4343
|arg1, arg2, _, _, arg3, arg4| { let temp = arg4 + arg3;
44-
arg2 * arg1 - temp }
44+
arg2 * arg1 - temp };
45+
46+
let block_body_with_comment = args.iter()
47+
.map(|a| {
48+
// Emitting only dep-info is possible only for final crate type, as
49+
// as others may emit required metadata for dependent crate types
50+
if a.starts_with("--emit") && is_final_crate_type && !self.workspace_mode {
51+
"--emit=dep-info"
52+
} else { a }
53+
});
4554
}
4655

4756
fn issue311() {

tests/target/closure.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,17 @@ fn main() {
6060
|arg1, arg2, _, _, arg3, arg4| {
6161
let temp = arg4 + arg3;
6262
arg2 * arg1 - temp
63-
}
63+
};
64+
65+
let block_body_with_comment = args.iter().map(|a| {
66+
// Emitting only dep-info is possible only for final crate type, as
67+
// as others may emit required metadata for dependent crate types
68+
if a.starts_with("--emit") && is_final_crate_type && !self.workspace_mode {
69+
"--emit=dep-info"
70+
} else {
71+
a
72+
}
73+
});
6474
}
6575

6676
fn issue311() {

0 commit comments

Comments
 (0)