Skip to content

rustfmt deletes comment at start of array length expression #6434

Open
@ctz

Description

@ctz

I write this code:

struct Abc([u8;
// five
5 +
// ten
10 +
// thirteen
13
]);

rustfmt formats this to:

struct Abc(
    [u8; 5 +
// ten
10 +
// thirteen
13],
);

It has lost the // five comment. The remainder of the formatting is not great.

playground

version:

rustfmt 1.8.0-stable (90b35a6239 2024-11-26)

Activity

changed the title [-]rustfmt deletes comments in array length[/-] [+]rustfmt deletes comment at start of array length expression[/+] on Jan 2, 2025
ctz

ctz commented on Jan 2, 2025

@ctz
ContributorAuthor

A workaround is to surround the expression in const { .. }

edit: although a comment before the const is still removed:

struct Abc(
    [u8;
    // blorp
    const {
        // five
        5 +
// ten
10 +
// thirteen
13
    }],
);

is formatted to:

struct Abc(
    [u8; const {
        // five
        5 +
// ten
10 +
// thirteen
13
    }],
);
added
bugPanic, non-idempotency, invalid code, etc.
on Jan 2, 2025
ytmimi

ytmimi commented on Jan 2, 2025

@ytmimi
Contributor

Thanks for the report. This is related to #5297, except the error occurs in expression rewriting:

rustfmt/src/expr.rs

Lines 284 to 291 in a43eef1

ast::ExprKind::Repeat(ref expr, ref repeats) => rewrite_pair(
&**expr,
&*repeats.value,
PairParts::new("[", "; ", "]"),
context,
shape,
SeparatorPlace::Back,
),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a-commentsbugPanic, non-idempotency, invalid code, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ctz@ytmimi

        Issue actions

          rustfmt deletes comment at start of array length expression · Issue #6434 · rust-lang/rustfmt