-
Notifications
You must be signed in to change notification settings - Fork 896
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 silently removes comments after enum/struct field #5464
Comments
Stumbled upon a similar issue:
|
Ran into another one of these: trait Trait<T> // comment
where
T: Clone,
{
} Here, "comment" is also removed. This is not even a weird place to put a comment. I had a FIXME there which was important and after formatting my code it was gone. I noticed only by luck. |
Hey @ytmimi , is there any development being done fixing this issue? |
@NishantJoshi00 I don't think anyone has picked this up. If you're interested in working on it you can comment |
@rustbot claim |
Hey @ytmimi, I've been going through the codebase, familiarizing myself with how the data flows through the system. Along the way, these are some of the observations that I made:
Now, looking at how we can solve this, a few ideas come to mind:
I'd love to get your thoughts on this before starting any substantial code changes. |
Interesting idea, but I don't think we need to make such a change to tackle this issue. Could be interesting as a standalone PR.
I agree that it's an odd place for a comment. I don't think we want to encourage it, but we can definitely do better than silently removing the comment. One thing we could try is simply not rewriting the struct field or enum variant if a comment would be removed using Lines 426 to 427 in a2625bf
An alternative that I can think of is to make changes to Lines 1914 to 1929 in a2625bf
One wrinkle I can think of is how should we format line comments and multi-line block comments? enum Enum{
Variant1 { field /* multi-
* line
* comment*/ : i32},
Variant2 { field // A line comment
: i32}
} |
Not sure if this is worth a separate issue but it seems like rustfmt eats comments in function signatures as well. This: pub /* const */ fn foo() {} Formats as pub fn foo() {} Confused me when I saved and my temporary hack turned into a permanent one 🙂 Tested on the playground with |
@tgross35 I think a similar issues has been reported before but I can't find it in the backlog right now. Probably best to open up a new issue for this since dropping the comment between the visibility modifier and the |
Examples:
Both comments get removed without any warning.
Since this seems to be a recurring issue (#5297, #4708, #2781, rust-lang/rust#52444), would it be possible to find some (temporary) middle ground, such as emitting warnings? Can rustfmt get a list of all comments in the code before and after formatting to compare if any went missing?
The text was updated successfully, but these errors were encountered: