Skip to content

Deduplicate imports #5410

Open
Open
@IceTDrinker

Description

@IceTDrinker

Hello!

I searched the issues and could not find a similar issue, if there is already one on that topic I missed feel free to close this one.

I find myself solving some merge conflicts lately that happen for use statements, sometimes ending up with more than one import of the same name e.g.:

use std::vec::{Vec, Vec};

And running rustfmt on that does not deduplicate the import.

In practice the use statements causing issues are much bigger and it can be a bit of pain to deduplicate imports manually. In addition, the duplicate import prevents compilation because of the name Vec being redefined

error[E0252]: the name Vec is defined multiple times

Rust playground:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0cd8ef07ca6cde1a2892c9b8e1697ff4

It feels like it should be possible as I've already seen rustfmt merging multiple imports from the same module into one use statement, so there is a mechanism to group names together and sort them, I'm guessing that switching the underlying container for the grouped names to some Set-like object should do the trick.

I haven't had time to check the source for that particular case, but I thought I might as well open the issue here if someone knew where the code for this is and how it could be changed to manage that case 🙂

Cheers!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions