Skip to content

Commit 1ead31a

Browse files
authored
Merge pull request #2795 from jechase/issue-2794
Add test and fix for #2794
2 parents fb55135 + ee5ff2d commit 1ead31a

File tree

15 files changed

+45
-14
lines changed

15 files changed

+45
-14
lines changed

src/attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ impl Rewrite for ast::MetaItem {
247247
shape: item_shape,
248248
ends_with_newline: false,
249249
preserve_newline: false,
250+
nested: false,
250251
config: context.config,
251252
};
252253
let item_str = write_list(&item_vec, &fmt)?;

src/closures.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ fn rewrite_closure_fn_decl(
260260
shape: arg_shape,
261261
ends_with_newline: false,
262262
preserve_newline: true,
263+
nested: false,
263264
config: context.config,
264265
};
265266
let list_str = write_list(&item_vec, &fmt)?;

src/config/lists.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ pub enum DefinitiveListTactic {
1919
Vertical,
2020
Horizontal,
2121
Mixed,
22-
/// Tactic for nested import.
23-
NestedImport,
2422
/// Special case tactic for `format!()`, `write!()` style macros.
2523
SpecialMacro(usize),
2624
}

src/expr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,7 @@ pub fn rewrite_multiple_patterns(
13381338
shape,
13391339
ends_with_newline: false,
13401340
preserve_newline: false,
1341+
nested: false,
13411342
config: context.config,
13421343
};
13431344
write_list(&items, &fmt)
@@ -1902,6 +1903,7 @@ where
19021903
shape,
19031904
ends_with_newline: false,
19041905
preserve_newline: false,
1906+
nested: false,
19051907
config: context.config,
19061908
};
19071909
let list_str = write_list(&item_vec, &fmt)?;

src/imports.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -706,16 +706,12 @@ fn rewrite_nested_use_tree(
706706
shape.width.saturating_sub(2)
707707
};
708708

709-
let tactic = if has_nested_list {
710-
DefinitiveListTactic::NestedImport
711-
} else {
712-
definitive_tactic(
713-
&list_items,
714-
context.config.imports_layout(),
715-
Separator::Comma,
716-
remaining_width,
717-
)
718-
};
709+
let tactic = definitive_tactic(
710+
&list_items,
711+
context.config.imports_layout(),
712+
Separator::Comma,
713+
remaining_width,
714+
);
719715

720716
let ends_with_newline = context.config.imports_indent() == IndentStyle::Block
721717
&& tactic != DefinitiveListTactic::Horizontal;
@@ -731,6 +727,7 @@ fn rewrite_nested_use_tree(
731727
shape: nested_shape,
732728
ends_with_newline,
733729
preserve_newline: true,
730+
nested: has_nested_list,
734731
config: context.config,
735732
};
736733

src/items.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ impl<'a> FmtVisitor<'a> {
533533
shape,
534534
ends_with_newline: true,
535535
preserve_newline: true,
536+
nested: false,
536537
config: self.config,
537538
};
538539

@@ -2307,6 +2308,7 @@ fn rewrite_args(
23072308
shape: Shape::legacy(budget, indent),
23082309
ends_with_newline: tactic.ends_with_newline(context.config.indent_style()),
23092310
preserve_newline: true,
2311+
nested: false,
23102312
config: context.config,
23112313
};
23122314

@@ -2494,6 +2496,7 @@ fn rewrite_where_clause_rfc_style(
24942496
shape: clause_shape,
24952497
ends_with_newline: true,
24962498
preserve_newline: true,
2499+
nested: false,
24972500
config: context.config,
24982501
};
24992502
let preds_str = write_list(&items.collect::<Vec<_>>(), &fmt)?;
@@ -2607,6 +2610,7 @@ fn rewrite_where_clause(
26072610
shape: Shape::legacy(budget, offset),
26082611
ends_with_newline: tactic.ends_with_newline(context.config.indent_style()),
26092612
preserve_newline: true,
2613+
nested: false,
26102614
config: context.config,
26112615
};
26122616
let preds_str = write_list(&item_vec, &fmt)?;

src/lists.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ pub struct ListFormatting<'a> {
3434
pub ends_with_newline: bool,
3535
// Remove newlines between list elements for expressions.
3636
pub preserve_newline: bool,
37+
// Nested import lists get some special handling for the "Mixed" list type
38+
pub nested: bool,
3739
pub config: &'a Config,
3840
}
3941

@@ -282,13 +284,13 @@ where
282284
result.push('\n');
283285
result.push_str(indent_str);
284286
}
285-
DefinitiveListTactic::Mixed | DefinitiveListTactic::NestedImport => {
287+
DefinitiveListTactic::Mixed => {
286288
let total_width = total_item_width(item) + item_sep_len;
287289

288290
// 1 is space between separator and item.
289291
if (line_len > 0 && line_len + 1 + total_width > formatting.shape.width)
290292
|| prev_item_had_post_comment
291-
|| (tactic == DefinitiveListTactic::NestedImport
293+
|| (formatting.nested
292294
&& (prev_item_is_nested_import || (!first && inner_item.contains("::"))))
293295
{
294296
result.push('\n');
@@ -825,6 +827,7 @@ pub fn struct_lit_formatting<'a>(
825827
shape,
826828
ends_with_newline,
827829
preserve_newline: true,
830+
nested: false,
828831
config: context.config,
829832
}
830833
}

src/macros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ pub fn rewrite_macro_def(
399399
shape: arm_shape,
400400
ends_with_newline: true,
401401
preserve_newline: true,
402+
nested: false,
402403
config: context.config,
403404
};
404405

src/matches.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ fn rewrite_match_arms(
224224
shape: arm_shape,
225225
ends_with_newline: true,
226226
preserve_newline: true,
227+
nested: false,
227228
config: context.config,
228229
};
229230

src/overflow.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
388388
_ => false,
389389
},
390390
preserve_newline: false,
391+
nested: false,
391392
config: self.context.config,
392393
};
393394

src/reorder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ fn wrap_reorderable_items(
7777
shape,
7878
ends_with_newline: true,
7979
preserve_newline: false,
80+
nested: false,
8081
config: context.config,
8182
};
8283

src/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ where
366366
shape: list_shape,
367367
ends_with_newline: tactic.ends_with_newline(context.config.indent_style()),
368368
preserve_newline: true,
369+
nested: false,
369370
config: context.config,
370371
};
371372

src/vertical.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
252252
shape: item_shape,
253253
ends_with_newline: true,
254254
preserve_newline: true,
255+
nested: false,
255256
config: context.config,
256257
};
257258
write_list(&items, &fmt)

tests/source/issue-2794.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-indent_style: Block
2+
// rustfmt-imports_indent: Block
3+
// rustfmt-imports_layout: Vertical
4+
5+
use std::{
6+
env, fs, io::{Read, Write},
7+
};

tests/target/issue-2794.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// rustfmt-indent_style: Block
2+
// rustfmt-imports_indent: Block
3+
// rustfmt-imports_layout: Vertical
4+
5+
use std::{
6+
env,
7+
fs,
8+
io::{
9+
Read,
10+
Write,
11+
},
12+
};

0 commit comments

Comments
 (0)