Skip to content

imports_granularity=Item removes #[cfg()] attribute from imports #5030

Closed

Description

If I format this file

#[cfg(feature = "foo")]
use std::collections::{
    HashMap,
    HashSet,
};

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

with rustfmt --config imports_granularity=Item, rustfmt removes the cfg attribute:

$ rustfmt --config imports_granularity=Item main.rs

Output:

use std::collections::HashMap;
use std::collections::HashSet;

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

This only happens if there is more than one import in the use statement, i. e. formatting

#[cfg(feature = "foo")]
use std::collections::{
    HashMap,
};

correctly retains the attribute.

rustfmt was installed via rustup, current nightly:

$ rustfmt --version
rustfmt 1.4.37-nightly (c102653 2021-10-15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugPanic, non-idempotency, invalid code, etc.help wantedonly-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