Skip to content

Commit

Permalink
Preserve FixupContext when printing closure body without brace
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jan 1, 2025
1 parent 668d52e commit c7403e5
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3218,7 +3218,7 @@ pub(crate) mod printing {
Expr::Call(e) => print_expr_call(e, tokens, fixup),
Expr::Cast(e) => print_expr_cast(e, tokens, fixup),
#[cfg(feature = "full")]
Expr::Closure(e) => e.to_tokens(tokens),
Expr::Closure(e) => print_expr_closure(e, tokens, fixup),
#[cfg(feature = "full")]
Expr::Const(e) => e.to_tokens(tokens),
#[cfg(feature = "full")]
Expand Down Expand Up @@ -3485,25 +3485,30 @@ pub(crate) mod printing {
#[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
impl ToTokens for ExprClosure {
fn to_tokens(&self, tokens: &mut TokenStream) {
outer_attrs_to_tokens(&self.attrs, tokens);
self.lifetimes.to_tokens(tokens);
self.constness.to_tokens(tokens);
self.movability.to_tokens(tokens);
self.asyncness.to_tokens(tokens);
self.capture.to_tokens(tokens);
self.or1_token.to_tokens(tokens);
self.inputs.to_tokens(tokens);
self.or2_token.to_tokens(tokens);
self.output.to_tokens(tokens);
if matches!(self.output, ReturnType::Default)
|| matches!(&*self.body, Expr::Block(body) if body.attrs.is_empty() && body.label.is_none())
{
self.body.to_tokens(tokens);
} else {
token::Brace::default().surround(tokens, |tokens| {
print_expr(&self.body, tokens, FixupContext::new_stmt());
});
}
print_expr_closure(self, tokens, FixupContext::NONE);
}
}

#[cfg(feature = "full")]
fn print_expr_closure(e: &ExprClosure, tokens: &mut TokenStream, fixup: FixupContext) {
outer_attrs_to_tokens(&e.attrs, tokens);
e.lifetimes.to_tokens(tokens);
e.constness.to_tokens(tokens);
e.movability.to_tokens(tokens);
e.asyncness.to_tokens(tokens);
e.capture.to_tokens(tokens);
e.or1_token.to_tokens(tokens);
e.inputs.to_tokens(tokens);
e.or2_token.to_tokens(tokens);
e.output.to_tokens(tokens);
if matches!(e.output, ReturnType::Default)
|| matches!(&*e.body, Expr::Block(body) if body.attrs.is_empty() && body.label.is_none())
{
print_expr(&e.body, tokens, fixup.rightmost_subexpression());
} else {
token::Brace::default().surround(tokens, |tokens| {
print_expr(&e.body, tokens, FixupContext::new_stmt());
});
}
}

Expand Down

0 comments on commit c7403e5

Please sign in to comment.