Rustfmt: Rustfmt removes comments in `use`s

Created on 26 Dec 2019  路  10Comments  路  Source: rust-lang/rustfmt

Rustfmt sometimes removes comments in uses with {}. For example (diffs from cargo fmt -- --check):

-use a::{item /* comment */};
+use a::item;

With merge_imports = true:

-use a::{
-    x,
-    // comment
-    item,
-};
+use a::{item, x};
a-comments a-imports bug

All 10 comments

Another interesting case:

-use b::{
-    x,
-    y,
-};
-use a::item; // comment
+use a::item;
+use b::{x, y}; // comment

// comment moved to other use

I've just found new variant:

-use b::{
-    x,
-    y,
-};
-use a::item /* comment */;
+use a::item;
+use b::{x, y};

Sounds like there are some problems with formatting comments in uses :D

Should I open a separate issue for this?

Wrong indent:

-use a::item; // really long comment (with `use` exactly 100 characters) ____________________________
+use a::item; /* really long comment (with `use` exactly 100 characters)
+               * ____________________________ */
 use b::{x, y};

(is not reproducable with rustfmt 1.4.12-nightly (9f53665 2020-02-10))

@WaffleLapkin - what version of rustfmt are you using, and do you have a rustfmt config file/are you passing any config options via the cli?

@calebcartwright version:

% cargo +nightly fmt -- --version
rustfmt 1.4.11-nightly (1838235 2019-12-03)

I don't have rustfmt config nor I pass config options via the cli, except the second example where I have rustfmt.toml with the following line:

merge_imports = true

Thanks! I can't reproduce your last example with the latest version of rustfmt on the master branch (which contains a lot of unreleased changes), but the others are still all reproducible

I worked on this with wise86-android.

We noticed that when the use is in the form use a::{...}, comments are handled correctly.
However, there are problems when there are not curly braces.
For example, use a::item /* comment */; is converted to use a::item; // comment */;, while what we would expect is that it remains unchanged or something like use a::item; // comment.

We see that the comment transformation is done here:
https://github.com/rust-lang/rustfmt/blob/9124dd88d6ef14d0ae77dc52ff5e9598f24a75a0/rustfmt-core/rustfmt-lib/src/comment.rs#L247-L254

In the case above,/* comment1 */; is the orig parameter and// comment1 */; is returned.

Maybe the problem is that the orig parameter has a trailing ;

@calebcartwright It seems like the case fixed by PR #3999 does not currently work. I was a little surprised by this - given that there's a test case here, but (I would assume) the playground does not lie. The following example (playground link) exhibits the old behavior:

-use std::foo::{/* it's a comment! */ bar /* and another */};
+use std::foo::bar;

// filler

-use std::foo::{/* it's a comment! */ bar};
+use std::foo::bar;

// filler

-use std::foo::{bar /* and another */};
+use std::foo::bar;

I noticed this occuring with rustfmt 1.4.18-stable, and (per the playground) it seems to still be there in 1.14.21-nightly (2020-09-04 01f2ead). As a side note, it still occurs with just std::{ ... } -- there's no need for two leading path segments for this to happen.

@sharnoff - note that the referenced PR was merged to the master branch, so it's included in the unreleased rustfmt v2.0, but not in the released 1.x rustfmt versions you referenced

@calebcartwright Ah, that makes a bunch of sense! Thanks :)

Was this page helpful?
0 / 5 - 0 ratings