Skip to content

group_imports reorders use statements arounds comments, changing their meaning #6241

@RalfJung

Description

@RalfJung

This is similar to #3720, but for group_imports settings such as StdExternalCrate. rust-lang/rust#126776 shows the consequences of that: it is now impossible to have a "chunk" of use statements that have a comment above them. rustfmt will happily reorder them and keep the comment attached to the immediate use item after it, therefore fundamentally changing its meaning. That's extremely frustrating -- having the tool make it essentially impossible to have comments that clarify the intent of my code.

In situations like this:

use a;
use b;
use c;

// comment
use d;
use e;

rustfmt should view the comment as "breaking apart" the use block into multiple blocks, similar to what it does if there was something else, like a mod, in place of the comment. group_imports should then be applied to both groups separately. That would avoid such semantics-altering reformatting.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-commentsArea: commentsA-importsArea: imports, e.g. `use` syntaxC-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICEI-unexpected-motionIssue: unexpected motion of code or commentsUO-group_importsUnstable option: group_importsonly-with-optionRequires a non-default option value to reproduce

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions