Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a6ef302

Browse files
authored
Merge pull request rust-lang#3100 from topecongiro/issue-3092
Fix poor formatting of empty trait with generic bounds
2 parents 2f8c1fe + 5f5d042 commit a6ef302

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

src/expr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1937,7 +1937,9 @@ fn shape_from_rhs_tactic(
19371937
rhs_tactic: RhsTactics,
19381938
) -> Option<Shape> {
19391939
match rhs_tactic {
1940-
RhsTactics::ForceNextLineWithoutIndent => Some(shape.with_max_width(context.config)),
1940+
RhsTactics::ForceNextLineWithoutIndent => shape
1941+
.with_max_width(context.config)
1942+
.sub_width(shape.indent.width()),
19411943
RhsTactics::Default => {
19421944
Shape::indented(shape.indent.block_indent(context.config), context.config)
19431945
.sub_width(shape.rhs_overhead(context.config))

src/items.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
11211121

11221122
let snippet = context.snippet(item.span);
11231123
let open_pos = snippet.find_uncommented("{")? + 1;
1124+
let outer_indent_str = offset.block_only().to_string_with_newline(context.config);
11241125

11251126
if !trait_items.is_empty() || contains_comment(&snippet[open_pos..]) {
11261127
let mut visitor = FmtVisitor::from_context(context);
@@ -1134,13 +1135,12 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
11341135
visitor.format_missing(item.span.hi() - BytePos(1));
11351136

11361137
let inner_indent_str = visitor.block_indent.to_string_with_newline(context.config);
1137-
let outer_indent_str = offset.block_only().to_string_with_newline(context.config);
11381138

11391139
result.push_str(&inner_indent_str);
11401140
result.push_str(visitor.buffer.to_string().trim());
11411141
result.push_str(&outer_indent_str);
11421142
} else if result.contains('\n') {
1143-
result.push('\n');
1143+
result.push_str(&outer_indent_str);
11441144
}
11451145

11461146
result.push('}');

tests/source/trait.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,12 @@ trait Foo<'a> {
106106
impl<'a> Foo<'a> for i32 {
107107
type Bar< 'a > = i32;
108108
}
109+
110+
// #3092
111+
pub mod test {
112+
pub trait ATraitWithALooongName {}
113+
pub trait ATrait
114+
:ATraitWithALooongName + ATraitWithALooongName + ATraitWithALooongName + ATraitWithALooongName
115+
{
116+
}
117+
}

tests/target/trait.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,15 @@ trait Foo<'a> {
144144
impl<'a> Foo<'a> for i32 {
145145
type Bar<'a> = i32;
146146
}
147+
148+
// #3092
149+
pub mod test {
150+
pub trait ATraitWithALooongName {}
151+
pub trait ATrait:
152+
ATraitWithALooongName
153+
+ ATraitWithALooongName
154+
+ ATraitWithALooongName
155+
+ ATraitWithALooongName
156+
{
157+
}
158+
}

0 commit comments

Comments
 (0)