1
Fork 0

Put empty trait braces on same line if possible

This commit is contained in:
mujpao 2021-11-01 23:13:30 -07:00 committed by Caleb Cartwright
parent 9027db984b
commit 4d50e7c760
5 changed files with 163 additions and 3 deletions

View file

@ -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..]) {

View file

@ -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, {}

View file

@ -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, {}

View file

@ -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,
{
}

View file

@ -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,
{
}