From 7f314acc34949df28b19054d38c6da2158072862 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 29 Nov 2023 21:19:22 -0800 Subject: [PATCH] Delete special handling of some expr kinds from print_let In all four of Break, Closure, Ret, Yeet, the needs_par_as_let_scrutinee is guaranteed to return true because the .precedence().order() of those expr kinds is <= AssocOp::LAnd.precedence(). The relevant functions in rustc_ast::util::parser are: fn needs_par_as_let_scrutinee(order: i8) -> bool { order <= prec_let_scrutinee_needs_par() as i8 } fn prec_let_scrutinee_needs_par() -> usize { AssocOp::LAnd.precedence() } The .precedence().order() of Closure is PREC_CLOSURE (-40) and of Break, Ret, Yeet is PREC_JUMP (-30). The value of AssocOp::LAnd.precedence() is 6. So this commit causes no change in behavior, only potentially performance by doing a redundant call to contains_exterior_struct_lit in those four cases. This is fine because Break, Closure, Ret, Yeet should be exceedingly rare in the position of a let scrutinee. --- compiler/rustc_ast_pretty/src/pprust/state.rs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index b05215d7a34..bc6da9371aa 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -1161,16 +1161,8 @@ impl<'a> State<'a> { self.word_space("="); self.print_expr_cond_paren( expr, - match expr.kind { - ast::ExprKind::Break(..) - | ast::ExprKind::Closure(..) - | ast::ExprKind::Ret(..) - | ast::ExprKind::Yeet(..) => true, - _ => { - parser::contains_exterior_struct_lit(expr) - || parser::needs_par_as_let_scrutinee(expr.precedence().order()) - } - }, + parser::contains_exterior_struct_lit(expr) + || parser::needs_par_as_let_scrutinee(expr.precedence().order()), ); }