Skip to content

Commit e76cb6a

Browse files
dawirstejecknrc
dawirstejeck
authored andcommitted
Fix overlong impl (#1091)
* Fix issue-1048 * Take possible where-clause into account * Move test to existing test set * Fix wrong variable name
1 parent 78b52ec commit e76cb6a

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/items.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -
440440
ref self_ty,
441441
ref items) = item.node {
442442
let mut result = String::new();
443+
443444
result.push_str(&*format_visibility(&item.vis));
444445
result.push_str(format_unsafety(unsafety));
445446
result.push_str("impl");
@@ -470,7 +471,18 @@ pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -
470471
result.push_str(" for ");
471472
}
472473

473-
let budget = try_opt!(context.config.max_width.checked_sub(result.len()));
474+
let mut used_space = result.len();
475+
if generics.where_clause.predicates.is_empty() {
476+
// If there is no where clause adapt budget for type formatting to take space and curly
477+
// brace into account.
478+
match context.config.item_brace_style {
479+
BraceStyle::AlwaysNextLine => {}
480+
BraceStyle::PreferSameLine => used_space += 2,
481+
BraceStyle::SameLineWhere => used_space += 2,
482+
}
483+
}
484+
485+
let budget = try_opt!(context.config.max_width.checked_sub(used_space));
474486
let indent = offset + result.len();
475487
result.push_str(&*try_opt!(self_ty.rewrite(context, budget, indent)));
476488

tests/source/impls.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,6 @@ mod m {
9797

9898
impl<T> PartialEq for S<T> where T: PartialEq { }
9999
}
100+
101+
impl<BorrowType, K, V, NodeType, HandleType> Handle<NodeRef<BorrowType, K, V, NodeType>, HandleType> {
102+
}

tests/target/impls.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,7 @@ mod m {
124124

125125
impl<T> PartialEq for S<T> where T: PartialEq {}
126126
}
127+
128+
impl<BorrowType, K, V, NodeType, HandleType> Handle<NodeRef<BorrowType, K, V, NodeType>,
129+
HandleType> {
130+
}

0 commit comments

Comments
 (0)