Skip to content

Commit

Permalink
Enhance check for parenthesization of let-chains
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Sep 15, 2023
1 parent 299530b commit bf5b461
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions tests/test_precedence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ fn librustc_brackets(mut librustc_expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
failed: bool,
}

fn contains_let_chain(expr: &Expr) -> bool {
match &expr.kind {
ExprKind::Let(..) => true,
ExprKind::Binary(binop, left, right) => {
binop.node == BinOpKind::And
&& (contains_let_chain(left) || contains_let_chain(right))
}
_ => false,
}
}

fn flat_map_field<T: MutVisitor>(mut f: ExprField, vis: &mut T) -> Vec<ExprField> {
if f.is_shorthand {
noop_visit_expr(&mut f.expr, vis);
Expand Down Expand Up @@ -244,12 +255,7 @@ fn librustc_brackets(mut librustc_expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
noop_visit_expr(e, self);
match e.kind {
ExprKind::Block(..) | ExprKind::If(..) | ExprKind::Let(..) => {}
ExprKind::Binary(binop, ref left, ref right)
if match (&left.kind, binop.node, &right.kind) {
(ExprKind::Let(..), BinOpKind::And, _)
| (_, BinOpKind::And, ExprKind::Let(..)) => true,
_ => false,
} => {}
ExprKind::Binary(..) if contains_let_chain(e) => {}
_ => {
let inner = mem::replace(
e,
Expand Down Expand Up @@ -374,14 +380,22 @@ fn syn_brackets(syn_expr: syn::Expr) -> syn::Expr {
match expr {
Expr::Group(_) => unreachable!(),
Expr::If(_) | Expr::Unsafe(_) | Expr::Block(_) | Expr::Let(_) => false,
Expr::Binary(bin) => match (&*bin.left, bin.op, &*bin.right) {
(Expr::Let(_), BinOp::And(_), _) | (_, BinOp::And(_), Expr::Let(_)) => false,
_ => true,
},
Expr::Binary(_) => !contains_let_chain(expr),
_ => true,
}
}

fn contains_let_chain(expr: &Expr) -> bool {
match expr {
Expr::Let(_) => true,
Expr::Binary(expr) => {
matches!(expr.op, BinOp::And(_))
&& (contains_let_chain(&expr.left) || contains_let_chain(&expr.right))
}
_ => false,
}
}

impl Fold for ParenthesizeEveryExpr {
fn fold_expr(&mut self, expr: Expr) -> Expr {
if needs_paren(&expr) {
Expand Down

0 comments on commit bf5b461

Please sign in to comment.