Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustfmt deletes comment at start of array length expression #6434

Open
ctz opened this issue Jan 2, 2025 · 2 comments
Open

rustfmt deletes comment at start of array length expression #6434

ctz opened this issue Jan 2, 2025 · 2 comments
Labels
a-comments bug Panic, non-idempotency, invalid code, etc.

Comments

@ctz
Copy link
Contributor

ctz commented Jan 2, 2025

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)
@ctz ctz changed the title rustfmt deletes comments in array length rustfmt deletes comment at start of array length expression Jan 2, 2025
@ctz
Copy link
Contributor Author

ctz commented Jan 2, 2025

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
    }],
);

@ytmimi ytmimi added bug Panic, non-idempotency, invalid code, etc. a-comments labels Jan 2, 2025
@ytmimi
Copy link
Contributor

ytmimi commented Jan 2, 2025

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
Labels
a-comments bug Panic, non-idempotency, invalid code, etc.
Projects
None yet
Development

No branches or pull requests

2 participants