diff options
| author | mujpao <mujpao@gmail.com> | 2021-11-01 23:13:30 -0700 |
|---|---|---|
| committer | Caleb Cartwright <calebcartwright@users.noreply.github.com> | 2021-11-05 20:40:49 -0500 |
| commit | 4d50e7c7606efca32bd937a4c60772a616fdbc33 (patch) | |
| tree | 03487c8b756af01e3b4d19a371629f501759c654 | |
| parent | 9027db984be06dc99fd88264e9dbe84541547f08 (diff) | |
| download | rust-4d50e7c7606efca32bd937a4c60772a616fdbc33.tar.gz rust-4d50e7c7606efca32bd937a4c60772a616fdbc33.zip | |
Put empty trait braces on same line if possible
| -rw-r--r-- | src/items.rs | 15 | ||||
| -rw-r--r-- | tests/source/empty-item-single-line-false.rs | 46 | ||||
| -rw-r--r-- | tests/source/item-brace-style-always-next-line.rs | 35 | ||||
| -rw-r--r-- | tests/target/empty-item-single-line-false.rs | 41 | ||||
| -rw-r--r-- | tests/target/item-brace-style-always-next-line.rs | 29 |
5 files changed, 163 insertions, 3 deletions
diff --git a/src/items.rs b/src/items.rs index 1c7899b3ac3..e37a1b69658 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1122,12 +1122,24 @@ pub(crate) fn format_trait( } } + let block_span = mk_sp(generics.where_clause.span.hi(), item.span.hi()); + let snippet = context.snippet(block_span); + let open_pos = snippet.find_uncommented("{")? + 1; + match context.config.brace_style() { _ if last_line_contains_single_line_comment(&result) || last_line_width(&result) + 2 > context.budget(offset.width()) => { result.push_str(&offset.to_string_with_newline(context.config)); } + _ if context.config.empty_item_single_line() + && trait_items.is_empty() + && !result.contains('\n') + && !contains_comment(&snippet[open_pos..]) => + { + result.push_str(" {}"); + return Some(result); + } BraceStyle::AlwaysNextLine => { result.push_str(&offset.to_string_with_newline(context.config)); } @@ -1144,9 +1156,6 @@ pub(crate) fn format_trait( } result.push('{'); - let block_span = mk_sp(generics.where_clause.span.hi(), item.span.hi()); - let snippet = context.snippet(block_span); - let open_pos = snippet.find_uncommented("{")? + 1; let outer_indent_str = offset.block_only().to_string_with_newline(context.config); if !trait_items.is_empty() || contains_comment(&snippet[open_pos..]) { diff --git a/tests/source/empty-item-single-line-false.rs b/tests/source/empty-item-single-line-false.rs new file mode 100644 index 00000000000..20c5bc83b46 --- /dev/null +++ b/tests/source/empty-item-single-line-false.rs @@ -0,0 +1,46 @@ +// rustfmt-brace_style: AlwaysNextLine +// rustfmt-empty_item_single_line: false + +fn function() +{ + +} + +struct Struct +{ + +} + +enum Enum +{ + +} + +trait Trait +{ + +} + +impl<T> Trait for T +{ + +} + +trait Trait2<T> +where + T: Copy + Display + Write + Read + FromStr, {} + +trait Trait3<T> +where + T: Something + + SomethingElse + + Sync + + Send + + Display + + Debug + + Copy + + Hash + + Debug + + Display + + Write + + Read, {} diff --git a/tests/source/item-brace-style-always-next-line.rs b/tests/source/item-brace-style-always-next-line.rs index 38094d67a77..0fb6405120a 100644 --- a/tests/source/item-brace-style-always-next-line.rs +++ b/tests/source/item-brace-style-always-next-line.rs @@ -27,3 +27,38 @@ mod M { struct D<T> where T: Copy {} } + + +fn function() +{ + +} + +trait Trait +{ + +} + +impl<T> Trait for T +{ + +} + +trait Trait2<T> +where + T: Copy + Display + Write + Read + FromStr, {} + +trait Trait3<T> +where + T: Something + + SomethingElse + + Sync + + Send + + Display + + Debug + + Copy + + Hash + + Debug + + Display + + Write + + Read, {} diff --git a/tests/target/empty-item-single-line-false.rs b/tests/target/empty-item-single-line-false.rs new file mode 100644 index 00000000000..bf7f70e7c43 --- /dev/null +++ b/tests/target/empty-item-single-line-false.rs @@ -0,0 +1,41 @@ +// rustfmt-brace_style: AlwaysNextLine +// rustfmt-empty_item_single_line: false + +fn function() +{ +} + +struct Struct {} + +enum Enum {} + +trait Trait +{ +} + +impl<T> Trait for T +{ +} + +trait Trait2<T> +where + T: Copy + Display + Write + Read + FromStr, +{ +} + +trait Trait3<T> +where + T: Something + + SomethingElse + + Sync + + Send + + Display + + Debug + + Copy + + Hash + + Debug + + Display + + Write + + Read, +{ +} diff --git a/tests/target/item-brace-style-always-next-line.rs b/tests/target/item-brace-style-always-next-line.rs index 531ac598683..4935fac04f1 100644 --- a/tests/target/item-brace-style-always-next-line.rs +++ b/tests/target/item-brace-style-always-next-line.rs @@ -40,3 +40,32 @@ mod M where T: Copy, {} } + +fn function() {} + +trait Trait {} + +impl<T> Trait for T {} + +trait Trait2<T> +where + T: Copy + Display + Write + Read + FromStr, +{ +} + +trait Trait3<T> +where + T: Something + + SomethingElse + + Sync + + Send + + Display + + Debug + + Copy + + Hash + + Debug + + Display + + Write + + Read, +{ +} |
