diff --git a/src/config.rs b/src/config.rs index b946d006a0b..232e8ed8129 100644 --- a/src/config.rs +++ b/src/config.rs @@ -538,9 +538,7 @@ create_config! { "Maximum width in the body of a struct variant before falling back to vertical formatting"; force_explicit_abi: bool, true, false, "Always print the abi for extern items"; newline_style: NewlineStyle, NewlineStyle::Unix, false, "Unix or Windows line endings"; - fn_brace_style: BraceStyle, BraceStyle::SameLineWhere, false, "Brace style for functions"; - item_brace_style: BraceStyle, BraceStyle::SameLineWhere, false, - "Brace style for structs and enums"; + brace_style: BraceStyle, BraceStyle::SameLineWhere, false, "Brace style for items"; control_brace_style: ControlBraceStyle, ControlBraceStyle::AlwaysSameLine, false, "Brace style for control flow constructs"; impl_empty_single_line: bool, true, false, "Put empty-body implementations on a single line"; diff --git a/src/items.rs b/src/items.rs index d5e02dde6e5..cd3cb48c4db 100644 --- a/src/items.rs +++ b/src/items.rs @@ -314,7 +314,7 @@ impl<'a> FmtVisitor<'a> { rewrite_fn_base(&context, indent, ident, fn_sig, span, newline_brace, true)?; // 2 = ` {` - if self.config.fn_brace_style() == BraceStyle::AlwaysNextLine || force_newline_brace + if self.config.brace_style() == BraceStyle::AlwaysNextLine || force_newline_brace || last_line_width(&result) + 2 > self.shape().width { newline_brace = true; @@ -440,7 +440,7 @@ impl<'a> FmtVisitor<'a> { let generics_str = format_generics( &self.get_context(), generics, - self.config.item_brace_style(), + self.config.brace_style(), if enum_def.variants.is_empty() { BracePos::ForceSameLine } else { @@ -595,7 +595,7 @@ pub fn format_impl( let where_clause_str = rewrite_where_clause( context, &generics.where_clause, - context.config.item_brace_style(), + context.config.brace_style(), Shape::legacy(where_budget, offset.block_only()), context.config.where_density(), "{", @@ -641,7 +641,7 @@ pub fn format_impl( } result.push_str(&where_clause_str); - match context.config.item_brace_style() { + match context.config.brace_style() { _ if last_line_contains_single_line_comment(&result) => result.push_str(&sep), BraceStyle::AlwaysNextLine => result.push_str(&sep), BraceStyle::PreferSameLine => result.push(' '), @@ -784,7 +784,7 @@ fn format_impl_ref_and_type( let curly_brace_overhead = if generics.where_clause.predicates.is_empty() { // If there is no where clause adapt budget for type formatting to take space and curly // brace into account. - match context.config.item_brace_style() { + match context.config.brace_style() { BraceStyle::AlwaysNextLine => 0, _ => 2, } @@ -994,7 +994,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) let where_clause_str = rewrite_where_clause( context, &generics.where_clause, - context.config.item_brace_style(), + context.config.brace_style(), Shape::legacy(where_budget, offset.block_only()), where_density, "{", @@ -1038,7 +1038,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) } } - match context.config.item_brace_style() { + match context.config.brace_style() { _ if last_line_contains_single_line_comment(&result) => { result.push('\n'); result.push_str(&offset.to_string(context.config)); @@ -1103,7 +1103,7 @@ fn format_unit_struct(context: &RewriteContext, p: &StructParts, offset: Indent) format_generics( context, generics, - context.config.item_brace_style(), + context.config.brace_style(), BracePos::None, offset, mk_sp(generics.span.lo(), hi), @@ -1135,7 +1135,7 @@ pub fn format_struct_struct( Some(g) => format_generics( context, g, - context.config.item_brace_style(), + context.config.brace_style(), if fields.is_empty() { BracePos::ForceSameLine } else { @@ -1148,8 +1148,7 @@ pub fn format_struct_struct( None => { // 3 = ` {}`, 2 = ` {`. let overhead = if fields.is_empty() { 3 } else { 2 }; - if (context.config.item_brace_style() == BraceStyle::AlwaysNextLine - && !fields.is_empty()) + if (context.config.brace_style() == BraceStyle::AlwaysNextLine && !fields.is_empty()) || context.config.max_width() < overhead + result.len() { format!("\n{}{{", offset.block_only().to_string(context.config)) @@ -1279,7 +1278,7 @@ fn format_tuple_struct( rewrite_where_clause( context, &generics.where_clause, - context.config.item_brace_style(), + context.config.brace_style(), Shape::legacy(where_budget, offset.block_only()), Density::Compressed, ";", @@ -1365,7 +1364,7 @@ pub fn rewrite_type_alias( let where_clause_str = rewrite_where_clause( context, &generics.where_clause, - context.config.item_brace_style(), + context.config.brace_style(), Shape::legacy(where_budget, indent), context.config.where_density(), "=", @@ -2071,7 +2070,7 @@ fn rewrite_fn_base( if let Some(where_clause_str) = rewrite_where_clause( context, where_clause, - context.config.fn_brace_style(), + context.config.brace_style(), Shape::legacy(budget, indent), Density::Compressed, "{", @@ -2090,7 +2089,7 @@ fn rewrite_fn_base( let where_clause_str = rewrite_where_clause( context, where_clause, - context.config.fn_brace_style(), + context.config.brace_style(), Shape::indented(indent, context.config), Density::Tall, "{", @@ -2386,7 +2385,7 @@ fn newline_for_brace(config: &Config, where_clause: &ast::WhereClause, has_body: if config.where_single_line() && predicate_count == 1 { return false; } - match (config.fn_brace_style(), config.where_density()) { + match (config.brace_style(), config.where_density()) { (BraceStyle::AlwaysNextLine, _) => true, (_, Density::Compressed) if predicate_count == 1 => false, (_, Density::CompressedIfEmpty) if predicate_count == 1 && !has_body => false, diff --git a/src/visitor.rs b/src/visitor.rs index b69edfda0c6..159f73452bd 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -666,7 +666,7 @@ impl<'a> FmtVisitor<'a> { self.buffer.push_str(&ident.to_string()); if is_internal { - match self.config.item_brace_style() { + match self.config.brace_style() { BraceStyle::AlwaysNextLine => self.buffer .push_str(&format!("\n{}{{", self.block_indent.to_string(self.config))), _ => self.buffer.push_str(" {"), diff --git a/tests/config/small_tabs.toml b/tests/config/small_tabs.toml index 264591bc400..298094a7bc9 100644 --- a/tests/config/small_tabs.toml +++ b/tests/config/small_tabs.toml @@ -2,7 +2,7 @@ max_width = 100 comment_width = 80 tab_spaces = 2 newline_style = "Unix" -fn_brace_style = "SameLineWhere" +brace_style = "SameLineWhere" fn_return_indent = "WithArgs" fn_args_paren_newline = true fn_args_density = "Tall" diff --git a/tests/source/configs-fn_brace_style-always_next_line.rs b/tests/source/configs-fn_brace_style-always_next_line.rs index 9e1a6048a5a..d3bd9ac09aa 100644 --- a/tests/source/configs-fn_brace_style-always_next_line.rs +++ b/tests/source/configs-fn_brace_style-always_next_line.rs @@ -1,4 +1,4 @@ -// rustfmt-fn_brace_style: AlwaysNextLine +// rustfmt-brace_style: AlwaysNextLine // Function brace style fn lorem() { diff --git a/tests/source/configs-fn_brace_style-prefer_same_line.rs b/tests/source/configs-fn_brace_style-prefer_same_line.rs index 2bca0f44618..78a4495243d 100644 --- a/tests/source/configs-fn_brace_style-prefer_same_line.rs +++ b/tests/source/configs-fn_brace_style-prefer_same_line.rs @@ -1,4 +1,4 @@ -// rustfmt-fn_brace_style: PreferSameLine +// rustfmt-brace_style: PreferSameLine // Function brace style fn lorem() { diff --git a/tests/source/configs-fn_brace_style-same_line_where.rs b/tests/source/configs-fn_brace_style-same_line_where.rs index 7213634fdc4..3b78932e177 100644 --- a/tests/source/configs-fn_brace_style-same_line_where.rs +++ b/tests/source/configs-fn_brace_style-same_line_where.rs @@ -1,4 +1,4 @@ -// rustfmt-fn_brace_style: SameLineWhere +// rustfmt-brace_style: SameLineWhere // Function brace style fn lorem() { diff --git a/tests/source/configs-item_brace_style-always_next_line.rs b/tests/source/configs-item_brace_style-always_next_line.rs index b85d64b9e94..0cc19b34da7 100644 --- a/tests/source/configs-item_brace_style-always_next_line.rs +++ b/tests/source/configs-item_brace_style-always_next_line.rs @@ -1,4 +1,4 @@ -// rustfmt-item_brace_style: AlwaysNextLine +// rustfmt-brace_style: AlwaysNextLine // Item brace style enum Foo {} diff --git a/tests/source/configs-item_brace_style-prefer_same_line.rs b/tests/source/configs-item_brace_style-prefer_same_line.rs index a67f7e6478e..4412bc869a2 100644 --- a/tests/source/configs-item_brace_style-prefer_same_line.rs +++ b/tests/source/configs-item_brace_style-prefer_same_line.rs @@ -1,4 +1,4 @@ -// rustfmt-item_brace_style: PreferSameLine +// rustfmt-brace_style: PreferSameLine // Item brace style struct Lorem { diff --git a/tests/source/configs-item_brace_style-same_line_where.rs b/tests/source/configs-item_brace_style-same_line_where.rs index 11d4015d4c9..b8e69147dc5 100644 --- a/tests/source/configs-item_brace_style-same_line_where.rs +++ b/tests/source/configs-item_brace_style-same_line_where.rs @@ -1,4 +1,4 @@ -// rustfmt-item_brace_style: SameLineWhere +// rustfmt-brace_style: SameLineWhere // Item brace style struct Lorem { diff --git a/tests/source/fn-custom-6.rs b/tests/source/fn-custom-6.rs index f345e1cb3e9..8e8bb6c274e 100644 --- a/tests/source/fn-custom-6.rs +++ b/tests/source/fn-custom-6.rs @@ -1,5 +1,5 @@ // rustfmt-indent_style: Block -// rustfmt-fn_brace_style: PreferSameLine +// rustfmt-brace_style: PreferSameLine // Test different indents. fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) { diff --git a/tests/source/fn-custom-7.rs b/tests/source/fn-custom-7.rs index 03b9a895970..f7670e07fc8 100644 --- a/tests/source/fn-custom-7.rs +++ b/tests/source/fn-custom-7.rs @@ -1,7 +1,7 @@ // rustfmt-normalize_comments: true // rustfmt-indent_style: Block // rustfmt-fn_args_density: Vertical -// rustfmt-fn_brace_style: AlwaysNextLine +// rustfmt-brace_style: AlwaysNextLine // Case with only one variable. fn foo(a: u8) -> u8 { diff --git a/tests/source/fn-custom-8.rs b/tests/source/fn-custom-8.rs index 40ea44f084a..d3deb20a51c 100644 --- a/tests/source/fn-custom-8.rs +++ b/tests/source/fn-custom-8.rs @@ -1,5 +1,5 @@ // rustfmt-indent_style: Block -// rustfmt-fn_brace_style: PreferSameLine +// rustfmt-brace_style: PreferSameLine // Test different indents. fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) { diff --git a/tests/source/item-brace-style-always-next-line.rs b/tests/source/item-brace-style-always-next-line.rs index 96a628349ea..38094d67a77 100644 --- a/tests/source/item-brace-style-always-next-line.rs +++ b/tests/source/item-brace-style-always-next-line.rs @@ -1,4 +1,4 @@ -// rustfmt-item_brace_style: AlwaysNextLine +// rustfmt-brace_style: AlwaysNextLine mod M { enum A { diff --git a/tests/source/item-brace-style-prefer-same-line.rs b/tests/source/item-brace-style-prefer-same-line.rs index 636a584ff68..dff89b8b66b 100644 --- a/tests/source/item-brace-style-prefer-same-line.rs +++ b/tests/source/item-brace-style-prefer-same-line.rs @@ -1,4 +1,4 @@ -// rustfmt-item_brace_style: PreferSameLine +// rustfmt-brace_style: PreferSameLine mod M { enum A diff --git a/tests/source/item-brace-style-same-line-where.rs b/tests/source/item-brace-style-same-line-where.rs index 7b2a95d3245..ee4a7c5daad 100644 --- a/tests/source/item-brace-style-same-line-where.rs +++ b/tests/source/item-brace-style-same-line-where.rs @@ -1,4 +1,4 @@ -// rustfmt-item_brace_style: SameLineWhere +// rustfmt-brace_style: SameLineWhere mod M { enum A diff --git a/tests/target/configs-fn_brace_style-always_next_line.rs b/tests/target/configs-fn_brace_style-always_next_line.rs index 543fc015cba..2755a264689 100644 --- a/tests/target/configs-fn_brace_style-always_next_line.rs +++ b/tests/target/configs-fn_brace_style-always_next_line.rs @@ -1,4 +1,4 @@ -// rustfmt-fn_brace_style: AlwaysNextLine +// rustfmt-brace_style: AlwaysNextLine // Function brace style fn lorem() diff --git a/tests/target/configs-fn_brace_style-prefer_same_line.rs b/tests/target/configs-fn_brace_style-prefer_same_line.rs index 498ed9f2a99..23f98b6dd7a 100644 --- a/tests/target/configs-fn_brace_style-prefer_same_line.rs +++ b/tests/target/configs-fn_brace_style-prefer_same_line.rs @@ -1,4 +1,4 @@ -// rustfmt-fn_brace_style: PreferSameLine +// rustfmt-brace_style: PreferSameLine // Function brace style fn lorem() { diff --git a/tests/target/configs-fn_brace_style-same_line_where.rs b/tests/target/configs-fn_brace_style-same_line_where.rs index 4da07441eaa..2afe59943a3 100644 --- a/tests/target/configs-fn_brace_style-same_line_where.rs +++ b/tests/target/configs-fn_brace_style-same_line_where.rs @@ -1,4 +1,4 @@ -// rustfmt-fn_brace_style: SameLineWhere +// rustfmt-brace_style: SameLineWhere // Function brace style fn lorem() { diff --git a/tests/target/configs-item_brace_style-always_next_line.rs b/tests/target/configs-item_brace_style-always_next_line.rs index 888d255859b..38ed449d178 100644 --- a/tests/target/configs-item_brace_style-always_next_line.rs +++ b/tests/target/configs-item_brace_style-always_next_line.rs @@ -1,4 +1,4 @@ -// rustfmt-item_brace_style: AlwaysNextLine +// rustfmt-brace_style: AlwaysNextLine // Item brace style enum Foo {} @@ -21,5 +21,7 @@ where mod tests { #[test] - fn it_works() {} + fn it_works() + { + } } diff --git a/tests/target/configs-item_brace_style-prefer_same_line.rs b/tests/target/configs-item_brace_style-prefer_same_line.rs index 1b5dbfa9d49..5143d7517c7 100644 --- a/tests/target/configs-item_brace_style-prefer_same_line.rs +++ b/tests/target/configs-item_brace_style-prefer_same_line.rs @@ -1,4 +1,4 @@ -// rustfmt-item_brace_style: PreferSameLine +// rustfmt-brace_style: PreferSameLine // Item brace style struct Lorem { diff --git a/tests/target/configs-item_brace_style-same_line_where.rs b/tests/target/configs-item_brace_style-same_line_where.rs index ab88c1ca3ab..8a3b2852654 100644 --- a/tests/target/configs-item_brace_style-same_line_where.rs +++ b/tests/target/configs-item_brace_style-same_line_where.rs @@ -1,4 +1,4 @@ -// rustfmt-item_brace_style: SameLineWhere +// rustfmt-brace_style: SameLineWhere // Item brace style struct Lorem { diff --git a/tests/target/fn-custom-6.rs b/tests/target/fn-custom-6.rs index 2c86ade3c83..720d4288c41 100644 --- a/tests/target/fn-custom-6.rs +++ b/tests/target/fn-custom-6.rs @@ -1,5 +1,5 @@ // rustfmt-indent_style: Block -// rustfmt-fn_brace_style: PreferSameLine +// rustfmt-brace_style: PreferSameLine // Test different indents. fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) { diff --git a/tests/target/fn-custom-7.rs b/tests/target/fn-custom-7.rs index 88c6ab10d80..ec113c30d53 100644 --- a/tests/target/fn-custom-7.rs +++ b/tests/target/fn-custom-7.rs @@ -1,7 +1,7 @@ // rustfmt-normalize_comments: true // rustfmt-indent_style: Block // rustfmt-fn_args_density: Vertical -// rustfmt-fn_brace_style: AlwaysNextLine +// rustfmt-brace_style: AlwaysNextLine // Case with only one variable. fn foo(a: u8) -> u8 @@ -29,7 +29,8 @@ fn foo( bar() } -trait Test { +trait Test +{ fn foo(a: u8) { } diff --git a/tests/target/fn-custom-8.rs b/tests/target/fn-custom-8.rs index 151f7f2a0f2..290557b979d 100644 --- a/tests/target/fn-custom-8.rs +++ b/tests/target/fn-custom-8.rs @@ -1,5 +1,5 @@ // rustfmt-indent_style: Block -// rustfmt-fn_brace_style: PreferSameLine +// rustfmt-brace_style: PreferSameLine // Test different indents. fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) { diff --git a/tests/target/indented-impl.rs b/tests/target/indented-impl.rs index 422ed55b2db..eff579dddac 100644 --- a/tests/target/indented-impl.rs +++ b/tests/target/indented-impl.rs @@ -1,11 +1,12 @@ -// rustfmt-item_brace_style: AlwaysNextLine +// rustfmt-brace_style: AlwaysNextLine mod x { struct X(i8); impl Y for X { - fn y(self) -> () { + fn y(self) -> () + { println!("ok"); } } diff --git a/tests/target/item-brace-style-always-next-line.rs b/tests/target/item-brace-style-always-next-line.rs index 270cd35eb2e..531ac598683 100644 --- a/tests/target/item-brace-style-always-next-line.rs +++ b/tests/target/item-brace-style-always-next-line.rs @@ -1,4 +1,4 @@ -// rustfmt-item_brace_style: AlwaysNextLine +// rustfmt-brace_style: AlwaysNextLine mod M { diff --git a/tests/target/item-brace-style-prefer-same-line.rs b/tests/target/item-brace-style-prefer-same-line.rs index bb090479cab..ef8dc028c2d 100644 --- a/tests/target/item-brace-style-prefer-same-line.rs +++ b/tests/target/item-brace-style-prefer-same-line.rs @@ -1,4 +1,4 @@ -// rustfmt-item_brace_style: PreferSameLine +// rustfmt-brace_style: PreferSameLine mod M { enum A { diff --git a/tests/target/item-brace-style-same-line-where.rs b/tests/target/item-brace-style-same-line-where.rs index 54ad69de42a..9a275efc96e 100644 --- a/tests/target/item-brace-style-same-line-where.rs +++ b/tests/target/item-brace-style-same-line-where.rs @@ -1,4 +1,4 @@ -// rustfmt-item_brace_style: SameLineWhere +// rustfmt-brace_style: SameLineWhere mod M { enum A {