Skip to content

Formatting with merge_imports creates invalid Rust #3321

@jonasbb

Description

@jonasbb

As noted below the before version is already not valid Rust. However, it would be nice if it could be supported because I run into such cases during refactoring or adding new code.
I use merge_imports to keep the imports looking clean and sometimes I want to merge different sets of imports. Then it can happen that duplicates arise. This is normally handled well, in that duplicates are merged, just not here.

Before rustfmt:

use std::fmt::{self, Display};
use std::fmt;

fn main() {
    println!("Hello, world!");
}

After rustfmt:

use std::fmt::{
    self, {self, Display},
};

fn main() {
    println!("Hello, world!");
}

rustc error after formatting:

error[E0252]: the name `fmt` is defined multiple times
 --> src/main.rs:2:12
  |
2 |     self, {self, Display},
  |     ----   ^^^^ `fmt` reimported here
  |     |
  |     previous import of the module `fmt` here
  |
  = note: `fmt` must be defined only once in the type namespace of this module

rustfmt.toml

merge_imports = true
use_field_init_shorthand = true

rustc + rustfmt versions

$ rustc --version
rustc 1.34.0-nightly (f6fac4225 2019-02-03)
$ rustfmt --version
rustfmt 1.0.1-nightly (be13559 2018-12-10)

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