-
Notifications
You must be signed in to change notification settings - Fork 898
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
Incorrect formatting when reordering module declarations #5168
Comments
Thanks for the report! I can confirm that I can also reproduce the error on This is a bit of a tricky one because comments aren't explicitly represented in the ast that rustfmt uses to reformat your code. Here's a high level overview of what's happening: When going to rewrite and reorder the modules the | start here
v
pub mod a; // a
pub mod c; // c
pub mod b; // b
^
| end here Mentally You can think that rustfmt is grouping each mod with it's associated post comment (except for the last one because it's outside the span). Here's what those groups would look like: pub mod a; // a | <-- end of item 1
pub mod c; // c | <-- end of item 2
pub mod b; | <--end of item 3 (remember we don't know about this comment because it's outside the span) Then rustfmt reorders and writes the modules: pub mod a; // a
pub mod b;
pub mod c; // c And lastly, rustfmt checks to see if there were any trailing comments that it might have missed and that's when it appends pub mod a; // a
pub mod b;
pub mod c; // c // b I hope that explanation of the issue helps! I've started looking into this, and I have a few ideas of how we might solve it, but I'll need a little more time to work out issues and fully test what I'm thinking! |
In case anyone else wants to give this a go the Lines 290 to 300 in 5056f4c
|
Yup, looks like this is a duplicate |
Going to close in that case to try to avoid dialog getting split across too many issues |
This:
Gets formated to:
Using cargo fmt.
cargo 1.57.0 (b2e52d7ca 2021-10-21)
rustfmt 1.4.37-stable (f1edd04 2021-11-29)
found by @Patiga
The text was updated successfully, but these errors were encountered: