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 forcefully moves trailing comments to irrelevant code above (and not idempotent either) #6347

Open
msrd0 opened this issue Sep 23, 2024 · 3 comments

Comments

@msrd0
Copy link

msrd0 commented Sep 23, 2024

I sometimes write enums with a comment at the end saying something like "I should add foo in the future". Like

enum MyEnum {
    Bar,

    // I should add Foo
}

Unfortunately, rustfmt thinks it would be wise to reformat this into

enum MyEnum {
    Bar,
    // I should add Foo
}

Even worse, when enabling trailing_comma = "Never", I get the non-idempotent output where first it looks like above without the trailing comma, and then gets moved even further up:

enum MyEnum {
    Bar // I should add Foo
}

That formatting is very far from my original intention when writing code, and actively hinders readability as one might now ask why the comment should be related to that last enum variant. I think the behaviour should be changed so that rustfmt leaves the trailing comment with an empty newline as it is.

@ytmimi
Copy link
Contributor

ytmimi commented Sep 23, 2024

@msrd0 thanks for the report.

What version of rustfmt are you using?

@msrd0
Copy link
Author

msrd0 commented Sep 23, 2024

For the version with no parameters, I used play.rust-lang.org which claims to use 1.7.1-stable (2024-09-04 eeb90cd).

For the version with trailing_comma = "Never", I used rustfmt 1.7.1-nightly (5aea140 2024-08-20).

@ytmimi
Copy link
Contributor

ytmimi commented Oct 4, 2024

I believe this is related to #4757

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

No branches or pull requests

2 participants