Skip to content

[WIP] feat: imports: flatten last {self} segments#6950

Closed
Paladynee wants to merge 1 commit into
rust-lang:mainfrom
Paladynee:imports/last-self-flattening
Closed

[WIP] feat: imports: flatten last {self} segments#6950
Paladynee wants to merge 1 commit into
rust-lang:mainfrom
Paladynee:imports/last-self-flattening

Conversation

@Paladynee

@Paladynee Paladynee commented Jun 20, 2026

Copy link
Copy Markdown

this PR adds a feature that aims to do these conversions when in Item imports granularity:

// from
use std::io::{self};
// to
use std::io;

// from
use std::io::{self, Write}
// to
use std::io::Write;
use std::io;

rationale

3rd and 1st clause of https://doc.rust-lang.org/style-guide/items.html?highlight=imports#normalisation combined.

personal rationale

The Item imports granularity is to have a single Item for each import. while use thing::{self}; satisfies being a single import, it often gets formatted like this:

use thing::{
    self,
};

which goes against the use case of Item granularity for having a flat column of use keywords, and not introduce random indentations everywhere, and avoid cognitive load caused by the self. you'll usually never see use a::self; or use a::{self}; in Item import granularity, as it is meaningless module indirection overhead. in fact, i'd argue self imports in general in Item import granularity are completely useless.

HELP NEEDED

i currently implemented half of the functionality inside flatten_use_trees using a new method for UseTree named flatten_trailing_self. I mostly don't know the codebase, there was a feature i wanted and i decided to implement it so its sloppy but it works EXCEPT while flattening the last List segment that only contains a single Slf segment, the comments surrounding them (if any) are lost.

i modified a single test (you can see in the commit diff) to reflect all of the changes this PR should make. the rest of the test file should stay intact (preserving the comments) without flattening the List segment to the parent ident, and i have no idea how to do it properly.

it probably mostly goes through UseTree.list_item but i'm not sure. any and all help is appreciated!

@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Jun 20, 2026
@Paladynee Paladynee marked this pull request as draft June 20, 2026 17:25
@rustbot rustbot added S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: awaiting review from the assignee but also interested parties. labels Jun 20, 2026
@Paladynee

Copy link
Copy Markdown
Author

@rustbot label +E-help-wanted

@rustbot rustbot added the E-help-wanted Call for participation: help is requested to fix this issue label Jun 20, 2026
@Paladynee Paladynee changed the title feat: [WIP] flatten last self segments [WIP] feat: imports: flatten last {self} segments Jun 20, 2026
@ytmimi ytmimi closed this Jun 21, 2026
@rustbot rustbot removed the S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. label Jun 21, 2026
@ytmimi

ytmimi commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

We will not add an option for this. self imports are semantically different.

@Paladynee

Paladynee commented Jun 22, 2026

Copy link
Copy Markdown
Author

We will not add an option for this. self imports are semantically different.

I understand that self imports can have semantic differences in certain contexts, but since the official Rust Style Guide explicitly mandates this normalization (use a::self; into use a; and use a::{b}; into use a::b;), it feels like rustfmt is currently leaving code not normalized specifically when using the Item granularity.

Is the goal for rustfmt to eventually comply with this part of the style guide, or is this a deliberate and permanent divergence? I'd like to know where this decision, or the mention of an option for this behavior came from.

For convenience, i have traced the Style Guide normalization decisions back to their roots:

Can rustfmt definitively determine the equivalence?

They are always equivalent since paths in imports are always absolute

nrc's comment on import equivalence: rust-lang/style-team#24 (comment)

use foo::self -> use foo: rust-lang/style-team#24 (comment)
foo::{bar} to foo::bar: rust-lang/style-team#24 (comment)
first place where "Normalization" is explicitly laid out: rust-lang/style-team#24 (comment)

@jieyouxu jieyouxu removed the E-help-wanted Call for participation: help is requested to fix this issue label Jun 22, 2026
@ytmimi

ytmimi commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

@Paladynee see #6129 and the linked issues. It's possible that these semantic differences have been addressed in new Rust language editions, but from what I remember use std::io::{self}; and use std::io; bring different things into scope. I'd want confirmation that that's no longer the case before we consider moving forward with any changes here. It might be worth opening an issue in https://github.com/rust-lang/rust for the Style Guide because it might be suggesting an incorrect normalization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants