Skip to content

[import_granularity] Allow formatting of use behind #[cfg*]#6723

Closed
dingxiangfei2009 wants to merge 0 commit intorust-lang:mainfrom
dingxiangfei2009:master
Closed

[import_granularity] Allow formatting of use behind #[cfg*]#6723
dingxiangfei2009 wants to merge 0 commit intorust-lang:mainfrom
dingxiangfei2009:master

Conversation

@dingxiangfei2009
Copy link
Copy Markdown

@dingxiangfei2009 dingxiangfei2009 commented Nov 10, 2025

Fix #6666

Related to #4991

This patch fixes the issue that import_granularity does not apply import_granuarity to uses behind attributes known to the compiler that the out coding style applies transparently. Attributes like #[cfg], #[cfg_attr], #[warn], #[deny], #[allow].

We achieve this by temporarily swap out the default use tree and merge sub-items in a temporary use tree. This tree is then later merged back into the default use tree, with the original attributes grandfathered.

@dingxiangfei2009 dingxiangfei2009 changed the title Allow formatting of use behind #[cfg*] import_granularity: Allow formatting of use behind #[cfg*] Nov 20, 2025
@dingxiangfei2009 dingxiangfei2009 changed the title import_granularity: Allow formatting of use behind #[cfg*] [import_granularity] Allow formatting of use behind #[cfg*] Nov 20, 2025
Comment thread src/imports.rs Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be worthwhile adding some extra tests:

  • That attributes we don't want to merge are left alone, e.g. that both of the following are left alone with granularity=crate
    /// some doc comment
    use a::{b::c, d::e, d::f};
    
    #[my_atttribute]
    use a::{b::c, d::e, d::f};
  • That the change works as expected across all relevant values of imports_granularity

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

Labels

pr-not-reviewed UO-imports_granularity Unstable option: imports_granularity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

imports_granularity is not respected for #[cfg]'d items

4 participants