about summary refs log tree commit diff
path: root/compiler/rustc_hir_pretty
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-04-27 16:08:59 +0200
committerGitHub <noreply@github.com>2025-04-27 16:08:59 +0200
commit405c8afce315639b45a949c4d6a05321f043665d (patch)
tree0ed01b036f266168080f8a42854bf80b11e124ad /compiler/rustc_hir_pretty
parent4f7aed6791027bb57c5bdd8ffbd95ddb731f2397 (diff)
parent7ac2d1f1bdb2348b13eb4f5fb9c7d620e316034f (diff)
downloadrust-405c8afce315639b45a949c4d6a05321f043665d.tar.gz
rust-405c8afce315639b45a949c4d6a05321f043665d.zip
Rollup merge of #140280 - nnethercote:improve-if-else-printing, r=Urgau
Improve if/else pretty printing

AST/HIR pretty printing of if/else is currently pretty bad. This PR improves it a lot.

r? `@Nadrieril`
Diffstat (limited to 'compiler/rustc_hir_pretty')
-rw-r--r--compiler/rustc_hir_pretty/src/lib.rs23
1 files changed, 15 insertions, 8 deletions
diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs
index 779fae80f19..c95d6a277c7 100644
--- a/compiler/rustc_hir_pretty/src/lib.rs
+++ b/compiler/rustc_hir_pretty/src/lib.rs
@@ -1064,18 +1064,18 @@ impl<'a> State<'a> {
         if let Some(els_inner) = els {
             match els_inner.kind {
                 // Another `else if` block.
-                hir::ExprKind::If(i, then, e) => {
-                    self.cbox(INDENT_UNIT - 1);
+                hir::ExprKind::If(i, hir::Expr { kind: hir::ExprKind::Block(t, None), .. }, e) => {
+                    self.cbox(0);
                     self.ibox(0);
                     self.word(" else if ");
                     self.print_expr_as_cond(i);
                     self.space();
-                    self.print_expr(then);
+                    self.print_block(t);
                     self.print_else(e);
                 }
                 // Final `else` block.
-                hir::ExprKind::Block(b, _) => {
-                    self.cbox(INDENT_UNIT - 1);
+                hir::ExprKind::Block(b, None) => {
+                    self.cbox(0);
                     self.ibox(0);
                     self.word(" else ");
                     self.print_block(b);
@@ -1094,11 +1094,18 @@ impl<'a> State<'a> {
         blk: &hir::Expr<'_>,
         elseopt: Option<&hir::Expr<'_>>,
     ) {
-        self.head("if");
+        self.cbox(0);
+        self.ibox(0);
+        self.word_nbsp("if");
         self.print_expr_as_cond(test);
         self.space();
-        self.print_expr(blk);
-        self.print_else(elseopt)
+        match blk.kind {
+            hir::ExprKind::Block(blk, None) => {
+                self.print_block(blk);
+                self.print_else(elseopt)
+            }
+            _ => panic!("non-block then expr"),
+        }
     }
 
     fn print_anon_const(&mut self, constant: &hir::AnonConst) {