Skip to content

Commit 40950dd

Browse files
committed
Remove recursion from printing of Expr::If
1 parent e2e7f2d commit 40950dd

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/expr.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3334,16 +3334,33 @@ pub(crate) mod printing {
33343334
impl ToTokens for ExprIf {
33353335
fn to_tokens(&self, tokens: &mut TokenStream) {
33363336
outer_attrs_to_tokens(&self.attrs, tokens);
3337-
self.if_token.to_tokens(tokens);
3338-
wrap_bare_struct(tokens, &self.cond);
3339-
self.then_branch.to_tokens(tokens);
3340-
if let Some((else_token, else_)) = &self.else_branch {
3337+
3338+
let mut expr = self;
3339+
loop {
3340+
expr.if_token.to_tokens(tokens);
3341+
wrap_bare_struct(tokens, &expr.cond);
3342+
expr.then_branch.to_tokens(tokens);
3343+
3344+
let (else_token, else_) = match &expr.else_branch {
3345+
Some(else_branch) => else_branch,
3346+
None => break,
3347+
};
3348+
33413349
else_token.to_tokens(tokens);
3342-
// If we are not one of the valid expressions to exist in an else
3343-
// clause, wrap ourselves in a block.
3344-
match **else_ {
3345-
Expr::If(_) | Expr::Block(_) => else_.to_tokens(tokens),
3346-
_ => token::Brace::default().surround(tokens, |tokens| else_.to_tokens(tokens)),
3350+
match &**else_ {
3351+
Expr::If(next) => {
3352+
expr = next;
3353+
}
3354+
Expr::Block(last) => {
3355+
last.to_tokens(tokens);
3356+
break;
3357+
}
3358+
// If this is not one of the valid expressions to exist in
3359+
// an else clause, wrap it in a block.
3360+
other => {
3361+
token::Brace::default().surround(tokens, |tokens| other.to_tokens(tokens));
3362+
break;
3363+
}
33473364
}
33483365
}
33493366
}

0 commit comments

Comments
 (0)