diff --git a/src/expr.rs b/src/expr.rs index 88f56cfa25c0..09220e9ac80c 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -501,10 +501,12 @@ where DefinitiveListTactic::Mixed }, }; + let mut ends_with_newline = tactic.ends_with_newline(context.config.array_layout()); if context.config.array_horizontal_layout_threshold() > 0 && items.len() > context.config.array_horizontal_layout_threshold() { tactic = DefinitiveListTactic::Mixed; + ends_with_newline = false; if context.config.array_layout() == IndentStyle::Block { nested_shape = try_opt!( shape @@ -525,7 +527,7 @@ where SeparatorTactic::Vertical }, shape: nested_shape, - ends_with_newline: false, + ends_with_newline: ends_with_newline, config: context.config, }; let list_str = try_opt!(write_list(&items, &fmt)); diff --git a/src/items.rs b/src/items.rs index eb15e95fabb3..aa83d9a2b0dc 100644 --- a/src/items.rs +++ b/src/items.rs @@ -2204,21 +2204,18 @@ fn rewrite_args( .and_then(|item| item.post_comment.as_ref()) .map_or(false, |s| s.trim().starts_with("//")); - let (indent, trailing_comma, end_with_newline) = match context.config.fn_args_layout() { - IndentStyle::Block if fits_in_one_line => ( - indent.block_indent(context.config), - SeparatorTactic::Never, - true, - ), + let (indent, trailing_comma) = match context.config.fn_args_layout() { + IndentStyle::Block if fits_in_one_line => { + (indent.block_indent(context.config), SeparatorTactic::Never) + } IndentStyle::Block => ( indent.block_indent(context.config), context.config.trailing_comma(), - true, ), IndentStyle::Visual if last_line_ends_with_comment => { - (arg_indent, context.config.trailing_comma(), true) + (arg_indent, context.config.trailing_comma()) } - IndentStyle::Visual => (arg_indent, SeparatorTactic::Never, false), + IndentStyle::Visual => (arg_indent, SeparatorTactic::Never), }; let tactic = definitive_tactic( @@ -2242,7 +2239,7 @@ fn rewrite_args( trailing_comma }, shape: Shape::legacy(budget, indent), - ends_with_newline: end_with_newline, + ends_with_newline: tactic.ends_with_newline(context.config.fn_args_layout()), config: context.config, }; @@ -2406,8 +2403,6 @@ where let item_vec = items.collect::>(); let tactic = definitive_tactic(&item_vec, ListTactic::HorizontalVertical, one_line_budget); - let ends_with_newline = context.config.generics_indent() == IndentStyle::Block && - tactic == DefinitiveListTactic::Vertical; let fmt = ListFormatting { tactic: tactic, separator: ",", @@ -2417,7 +2412,7 @@ where context.config.trailing_comma() }, shape: shape, - ends_with_newline: ends_with_newline, + ends_with_newline: tactic.ends_with_newline(context.config.generics_indent()), config: context.config, }; @@ -2631,7 +2626,7 @@ fn rewrite_where_clause( separator: ",", trailing_separator: comma_tactic, shape: Shape::legacy(budget, offset), - ends_with_newline: true, + ends_with_newline: tactic.ends_with_newline(context.config.where_pred_indent()), config: context.config, }; let preds_str = try_opt!(write_list(&item_vec, &fmt)); diff --git a/src/lists.rs b/src/lists.rs index e55103d0df81..55db7cd9c000 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -124,6 +124,15 @@ pub enum DefinitiveListTactic { Mixed, } +impl DefinitiveListTactic { + pub fn ends_with_newline(&self, indent_style: IndentStyle) -> bool { + match indent_style { + IndentStyle::Block => *self != DefinitiveListTactic::Horizontal, + IndentStyle::Visual => false, + } + } +} + pub fn definitive_tactic(items: I, tactic: ListTactic, width: usize) -> DefinitiveListTactic where I: IntoIterator + Clone, @@ -169,7 +178,7 @@ where // Now that we know how we will layout, we can decide for sure if there // will be a trailing separator. - let trailing_separator = needs_trailing_separator(formatting.trailing_separator, tactic); + let mut trailing_separator = needs_trailing_separator(formatting.trailing_separator, tactic); let mut result = String::new(); let cloned_items = items.clone(); let mut iter = items.into_iter().enumerate().peekable(); @@ -182,7 +191,7 @@ where let inner_item = try_opt!(item.item.as_ref()); let first = i == 0; let last = iter.peek().is_none(); - let separate = !last || trailing_separator; + let mut separate = !last || trailing_separator; let item_sep_len = if separate { sep_len } else { 0 }; // Item string may be multi-line. Its length (used for block comment alignment) @@ -213,6 +222,13 @@ where result.push('\n'); result.push_str(indent_str); line_len = 0; + if tactic == DefinitiveListTactic::Mixed && formatting.ends_with_newline { + if last { + separate = true; + } else { + trailing_separator = true; + } + } } if line_len > 0 { diff --git a/src/types.rs b/src/types.rs index 877502f153c4..c34a3bfaa6a8 100644 --- a/src/types.rs +++ b/src/types.rs @@ -360,7 +360,7 @@ where context.config.trailing_comma() }, shape: list_shape, - ends_with_newline: false, + ends_with_newline: tactic.ends_with_newline(context.config.fn_call_style()), config: context.config, }; diff --git a/tests/target/configs-fn_args_density-compressed.rs b/tests/target/configs-fn_args_density-compressed.rs index 7bc949e55180..99283722beea 100644 --- a/tests/target/configs-fn_args_density-compressed.rs +++ b/tests/target/configs-fn_args_density-compressed.rs @@ -10,12 +10,12 @@ trait Lorem { fn lorem( ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: onsectetur, - adipiscing: Adipiscing, elit: Elit + adipiscing: Adipiscing, elit: Elit, ); fn lorem( ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, consectetur: onsectetur, - adipiscing: Adipiscing, elit: Elit + adipiscing: Adipiscing, elit: Elit, ) { // body } diff --git a/tests/target/fn-custom.rs b/tests/target/fn-custom.rs index 7a2ea722bfd5..bf87553dedcf 100644 --- a/tests/target/fn-custom.rs +++ b/tests/target/fn-custom.rs @@ -4,7 +4,7 @@ // Test compressed layout of args. fn foo( a: Aaaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbbbb, c: Ccccccccccccccccc, d: Ddddddddddddddddddddddddd, - e: Eeeeeeeeeeeeeeeeeee + e: Eeeeeeeeeeeeeeeeeee, ) { foo(); } @@ -12,7 +12,7 @@ fn foo( impl Foo { fn foo( self, a: Aaaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbbbb, c: Ccccccccccccccccc, - d: Ddddddddddddddddddddddddd, e: Eeeeeeeeeeeeeeeeeee + d: Ddddddddddddddddddddddddd, e: Eeeeeeeeeeeeeeeeeee, ) { foo(); } diff --git a/tests/target/fn-simple.rs b/tests/target/fn-simple.rs index e150973a67d0..e381b6e62314 100644 --- a/tests/target/fn-simple.rs +++ b/tests/target/fn-simple.rs @@ -42,7 +42,7 @@ where C, D, // pre comment - E, /* last comment */ + E, // last comment ) -> &SomeType, { arg(a, b, c, d, e) diff --git a/tests/target/hard-tabs.rs b/tests/target/hard-tabs.rs index c2f8d2522370..4ac6df5def85 100644 --- a/tests/target/hard-tabs.rs +++ b/tests/target/hard-tabs.rs @@ -67,7 +67,7 @@ fn main() { C, D, // pre comment - E, /* last comment */ + E, // last comment ) -> &SomeType, { arg(a, b, c, d, e) diff --git a/tests/target/type.rs b/tests/target/type.rs index 80855ee5732a..5a12989c857c 100644 --- a/tests/target/type.rs +++ b/tests/target/type.rs @@ -25,7 +25,7 @@ struct F { y: String, // comment 3 z: Foo, // comment - ... /* comment 2 */ + ... // comment 2 ), }