Skip to content

Fix rustfmt failing to format closure block body with comment #2089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -2219,7 +2216,7 @@ fn rewrite_last_closure(
) -> Option<String> {
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,
Expand Down
11 changes: 10 additions & 1 deletion tests/source/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
12 changes: 11 additions & 1 deletion tests/target/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down