Skip to content

Commit

Permalink
Factor out function to add syn parens
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Sep 15, 2023
1 parent bf5b461 commit 2dc903f
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions tests/test_precedence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,14 @@ fn syn_brackets(syn_expr: syn::Expr) -> syn::Expr {

struct ParenthesizeEveryExpr;

fn parenthesize(expr: Expr) -> Expr {
Expr::Paren(ExprParen {
attrs: Vec::new(),
expr: Box::new(expr),
paren_token: token::Paren::default(),
})
}

fn needs_paren(expr: &Expr) -> bool {
match expr {
Expr::Group(_) => unreachable!(),
Expand All @@ -399,11 +407,7 @@ fn syn_brackets(syn_expr: syn::Expr) -> syn::Expr {
impl Fold for ParenthesizeEveryExpr {
fn fold_expr(&mut self, expr: Expr) -> Expr {
if needs_paren(&expr) {
Expr::Paren(ExprParen {
attrs: Vec::new(),
expr: Box::new(fold_expr(self, expr)),
paren_token: token::Paren::default(),
})
parenthesize(fold_expr(self, expr))
} else {
fold_expr(self, expr)
}
Expand Down

0 comments on commit 2dc903f

Please sign in to comment.