Skip to content

Commit c7403e5

Browse files
committed
Preserve FixupContext when printing closure body without brace
1 parent 668d52e commit c7403e5

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

src/expr.rs

+25-20
Original file line numberDiff line numberDiff line change
@@ -3218,7 +3218,7 @@ pub(crate) mod printing {
32183218
Expr::Call(e) => print_expr_call(e, tokens, fixup),
32193219
Expr::Cast(e) => print_expr_cast(e, tokens, fixup),
32203220
#[cfg(feature = "full")]
3221-
Expr::Closure(e) => e.to_tokens(tokens),
3221+
Expr::Closure(e) => print_expr_closure(e, tokens, fixup),
32223222
#[cfg(feature = "full")]
32233223
Expr::Const(e) => e.to_tokens(tokens),
32243224
#[cfg(feature = "full")]
@@ -3485,25 +3485,30 @@ pub(crate) mod printing {
34853485
#[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
34863486
impl ToTokens for ExprClosure {
34873487
fn to_tokens(&self, tokens: &mut TokenStream) {
3488-
outer_attrs_to_tokens(&self.attrs, tokens);
3489-
self.lifetimes.to_tokens(tokens);
3490-
self.constness.to_tokens(tokens);
3491-
self.movability.to_tokens(tokens);
3492-
self.asyncness.to_tokens(tokens);
3493-
self.capture.to_tokens(tokens);
3494-
self.or1_token.to_tokens(tokens);
3495-
self.inputs.to_tokens(tokens);
3496-
self.or2_token.to_tokens(tokens);
3497-
self.output.to_tokens(tokens);
3498-
if matches!(self.output, ReturnType::Default)
3499-
|| matches!(&*self.body, Expr::Block(body) if body.attrs.is_empty() && body.label.is_none())
3500-
{
3501-
self.body.to_tokens(tokens);
3502-
} else {
3503-
token::Brace::default().surround(tokens, |tokens| {
3504-
print_expr(&self.body, tokens, FixupContext::new_stmt());
3505-
});
3506-
}
3488+
print_expr_closure(self, tokens, FixupContext::NONE);
3489+
}
3490+
}
3491+
3492+
#[cfg(feature = "full")]
3493+
fn print_expr_closure(e: &ExprClosure, tokens: &mut TokenStream, fixup: FixupContext) {
3494+
outer_attrs_to_tokens(&e.attrs, tokens);
3495+
e.lifetimes.to_tokens(tokens);
3496+
e.constness.to_tokens(tokens);
3497+
e.movability.to_tokens(tokens);
3498+
e.asyncness.to_tokens(tokens);
3499+
e.capture.to_tokens(tokens);
3500+
e.or1_token.to_tokens(tokens);
3501+
e.inputs.to_tokens(tokens);
3502+
e.or2_token.to_tokens(tokens);
3503+
e.output.to_tokens(tokens);
3504+
if matches!(e.output, ReturnType::Default)
3505+
|| matches!(&*e.body, Expr::Block(body) if body.attrs.is_empty() && body.label.is_none())
3506+
{
3507+
print_expr(&e.body, tokens, fixup.rightmost_subexpression());
3508+
} else {
3509+
token::Brace::default().surround(tokens, |tokens| {
3510+
print_expr(&e.body, tokens, FixupContext::new_stmt());
3511+
});
35073512
}
35083513
}
35093514

0 commit comments

Comments
 (0)