1
Fork 0

Rollup merge of #139132 - m-ou-se:hir-pp-struct-expr, r=compiler-errors

Improve hir_pretty for struct expressions.

While working on https://github.com/rust-lang/rust/pull/139131 I noticed the hir pretty printer outputs an empty line between each field, and is also missing a space before the `{` and the `}`:

```rust
    let a =
        StructWithSomeFields{
            field_1: 1,

            field_2: 2,

            field_3: 3,

            field_4: 4,

            field_5: 5,

            field_6: 6,};

    let a = StructWithSomeFields{ field_1: 1,  field_2: 2, ..a};
```

This changes it to:

```rust
    let a =
        StructWithSomeFields {
            field_1: 1,
            field_2: 2,
            field_3: 3,
            field_4: 4,
            field_5: 5,
            field_6: 6 };

    let a = StructWithSomeFields { field_1: 1, field_2: 2, ..a };
```
This commit is contained in:
Jacob Pratt 2025-03-30 17:59:29 -04:00 committed by GitHub
commit f07a0d117f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 59 additions and 13 deletions

View file

@ -1193,7 +1193,8 @@ impl<'a> State<'a> {
wth: hir::StructTailExpr<'_>,
) {
self.print_qpath(qpath, true);
self.word("{");
self.nbsp();
self.word_space("{");
self.commasep_cmnt(Consistent, fields, |s, field| s.print_expr_field(field), |f| f.span);
match wth {
hir::StructTailExpr::Base(expr) => {
@ -1215,20 +1216,13 @@ impl<'a> State<'a> {
self.word("..");
self.end();
}
hir::StructTailExpr::None => {
if !fields.is_empty() {
self.word(",");
}
}
hir::StructTailExpr::None => {}
}
self.space();
self.word("}");
}
fn print_expr_field(&mut self, field: &hir::ExprField<'_>) {
if self.attrs(field.hir_id).is_empty() {
self.space();
}
self.cbox(INDENT_UNIT);
self.print_attrs_as_outer(self.attrs(field.hir_id));
if !field.is_shorthand {