rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
6.04k stars 889 forks source link

Comments on `extern crates` that are reordered get misplaced #3127

Open shepmaster opened 6 years ago

shepmaster commented 6 years ago

0.99.6-nightly (2018-10-18 750b252)

Before

extern crate serde; // 1.0.78
extern crate serde_json; // 1.0.27
extern crate serde_derive; // 1.0.78

After

extern crate serde; // 1.0.78
extern crate serde_derive;
extern crate serde_json; // 1.0.27 // 1.0.78

Expected

extern crate serde; // 1.0.78
extern crate serde_derive; // 1.0.78
extern crate serde_json; // 1.0.27 
nrc commented 6 years ago

Happens with use imports too

nrc commented 6 years ago

See https://github.com/rust-lang-nursery/rustfmt/issues/3128#issuecomment-433155914 for an explanation of why.

After some investigation it seems that fixing this is pretty difficult, so un-blocking the milestone, would be good to fix this though.

ytmimi commented 2 years ago

Confirming I can still reproduce this with rustfmt 1.5.1-nightly (f2c31ba0 2022-07-19). The issue is with reorder_imports, which is a stable option. One way to work around this is to set reorder_imports=false.

input

extern crate serde; // 1.0.78
extern crate serde_json; // 1.0.27
extern crate serde_derive; // 1.0.78

output reorder_imports=false

extern crate serde; // 1.0.78
extern crate serde_json; // 1.0.27
extern crate serde_derive; // 1.0.78