Skip to content

Commit f8c734d

Browse files
Allow formatting of use behind #[cfg*]
Fix #6666 We would like to allow formatting items with attributes that conceptually allows outer styling as well. Signed-off-by: Xiangfei Ding <[email protected]>
1 parent ced5993 commit f8c734d

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

src/imports.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::borrow::Cow;
21
use std::cmp::Ordering;
32
use std::fmt;
3+
use std::{borrow::Cow, vec};
44

55
use core::hash::{Hash, Hasher};
66

@@ -212,6 +212,15 @@ impl UseSegment {
212212
}
213213
}
214214

215+
fn attrs_disallow_outer_style(attrs: &ast::AttrVec) -> bool {
216+
!attrs.iter().all(|attr| {
217+
let ast::AttrKind::Normal(attr) = &attr.kind else {
218+
return false;
219+
};
220+
attr.item.is_valid_for_outer_style()
221+
})
222+
}
223+
215224
pub(crate) fn normalize_use_trees_with_granularity(
216225
use_trees: Vec<UseTree>,
217226
import_granularity: ImportGranularity,
@@ -226,13 +235,24 @@ pub(crate) fn normalize_use_trees_with_granularity(
226235

227236
let mut result = Vec::with_capacity(use_trees.len());
228237
for use_tree in use_trees {
229-
if use_tree.contains_comment() || use_tree.attrs.is_some() {
238+
if use_tree.contains_comment()
239+
|| use_tree
240+
.attrs
241+
.as_ref()
242+
.map_or(false, attrs_disallow_outer_style)
243+
{
230244
result.push(use_tree);
231245
continue;
232246
}
247+
let attrs = use_tree.attrs.clone();
248+
let result_buf = if attrs.is_some() {
249+
&mut vec![]
250+
} else {
251+
&mut result
252+
};
233253

234254
for mut flattened in use_tree.flatten(import_granularity) {
235-
if let Some(tree) = result
255+
if let Some(tree) = result_buf
236256
.iter_mut()
237257
.find(|tree| tree.share_prefix(&flattened, merge_by))
238258
{
@@ -242,9 +262,19 @@ pub(crate) fn normalize_use_trees_with_granularity(
242262
if merge_by == SharedPrefix::Module {
243263
flattened = flattened.nest_trailing_self();
244264
}
245-
result.push(flattened);
265+
result_buf.push(flattened);
246266
}
247267
}
268+
if let Some(attrs) = attrs {
269+
let result_buf: Vec<_> = result_buf
270+
.drain(..)
271+
.map(|mut use_tree| {
272+
use_tree.attrs = Some(attrs.clone());
273+
use_tree
274+
})
275+
.collect();
276+
result.extend(result_buf);
277+
}
248278
}
249279
result
250280
}

tests/source/imports/imports_granularity_crate.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,7 @@ use b::v::{
6363
};
6464
use b::t::{/* Before b::t::self */ self};
6565
use b::c;
66+
67+
// https://github.com/rust-lang/rustfmt/issues/6666
68+
#[cfg(true)]
69+
use a::{b::c, d::e, d::f};

tests/target/imports/imports_granularity_crate.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,10 @@ use b::{
5757
/* Before b::l group */ l::{self, m, n::o, p::*},
5858
q,
5959
};
60+
61+
// https://github.com/rust-lang/rustfmt/issues/6666
62+
#[cfg(true)]
63+
use a::{
64+
b::c,
65+
d::{e, f},
66+
};

tests/target/imports/imports_granularity_module.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ pub use a::t;
1313
use b::c::d;
1414
use b::{self};
1515

16-
use foo::e;
1716
#[cfg(test)]
18-
use foo::{a::b, c::d};
17+
use foo::a::b;
18+
#[cfg(test)]
19+
use foo::c::d;
20+
use foo::e;
1921

2022
use bar::{
2123
// comment

0 commit comments

Comments
 (0)