Skip to content

separate by a new line for move || and match #4007

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 1 commit into from
Jan 14, 2020
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
28 changes: 21 additions & 7 deletions rustfmt-core/rustfmt-lib/src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub(crate) fn rewrite_closure(

let result = match fn_decl.output {
ast::FunctionRetTy::Default(_) if !context.inside_macro() => {
try_rewrite_without_block(body, &prefix, context, shape, body_shape)
try_rewrite_without_block(body, &prefix, capture, context, shape, body_shape)
}
_ => None,
};
Expand All @@ -72,13 +72,14 @@ pub(crate) fn rewrite_closure(
fn try_rewrite_without_block(
expr: &ast::Expr,
prefix: &str,
capture: ast::CaptureBy,
context: &RewriteContext<'_>,
shape: Shape,
body_shape: Shape,
) -> Option<String> {
let expr = get_inner_expr(expr, prefix, context);

if is_block_closure_forced(context, expr) {
if is_block_closure_forced(context, expr, capture) {
rewrite_closure_with_block(expr, prefix, context, shape)
} else {
rewrite_closure_expr(expr, prefix, context, body_shape)
Expand Down Expand Up @@ -357,7 +358,7 @@ pub(crate) fn rewrite_last_closure(
let body_shape = shape.offset_left(extra_offset)?;

// We force to use block for the body of the closure for certain kinds of expressions.
if is_block_closure_forced(context, body) {
if is_block_closure_forced(context, body, capture) {
return rewrite_closure_with_block(body, &prefix, context, body_shape).and_then(
|body_str| {
// If the expression can fit in a single line, we need not force block closure.
Expand Down Expand Up @@ -404,19 +405,32 @@ pub(crate) fn args_have_many_closure(args: &[OverflowableItem<'_>]) -> bool {
> 1
}

fn is_block_closure_forced(context: &RewriteContext<'_>, expr: &ast::Expr) -> bool {
fn is_block_closure_forced(
context: &RewriteContext<'_>,
expr: &ast::Expr,
capture: ast::CaptureBy,
) -> bool {
// If we are inside macro, we do not want to add or remove block from closure body.
if context.inside_macro() {
false
} else {
is_block_closure_forced_inner(expr)
if let ast::ExprKind::Match(..) = expr.kind {
let is_move_closure_without_brace =
capture == ast::CaptureBy::Value && !context.snippet(expr.span).trim().starts_with("{");

is_block_closure_forced_inner(expr) || is_move_closure_without_brace
} else {
is_block_closure_forced_inner(expr)
}
}
}

fn is_block_closure_forced_inner(expr: &ast::Expr) -> bool {
match expr.kind {
ast::ExprKind::If(..) | ast::ExprKind::While(..) | ast::ExprKind::ForLoop(..) => true,
ast::ExprKind::Loop(..) => true,
ast::ExprKind::If(..)
| ast::ExprKind::While(..)
| ast::ExprKind::ForLoop(..)
| ast::ExprKind::Loop(..) => true,
ast::ExprKind::AddrOf(_, ref expr)
| ast::ExprKind::Box(ref expr)
| ast::ExprKind::Try(ref expr)
Expand Down
8 changes: 8 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/source/issue-3502.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {
let f = future::poll_fn(
move || match tokio_threadpool::blocking(|| f.poll()).unwrap() {
Async::Ready(v) => v,
Async::NotReady => Ok(Async::NotReady),
},
);
}
8 changes: 8 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/target/issue-3502.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {
let f = future::poll_fn(move || {
match tokio_threadpool::blocking(|| f.poll()).unwrap() {
Async::Ready(v) => v,
Async::NotReady => Ok(Async::NotReady),
}
});
}