Skip to content

Commit 22de7ce

Browse files
dawirstejecknrc
dawirstejeck
authored andcommitted
Fix overlong function signature (#1089)
* Fix issue-1049 * Add testcase suggested by pepyakin * Fix last commit * Handle special case * Remove debugging println * Fix grammar in comment * Change word in comment * Add test for long func without ret type * Add one more test
1 parent 6380937 commit 22de7ce

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

src/items.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,10 +1369,21 @@ fn rewrite_fn_base(context: &RewriteContext,
13691369
FnArgLayoutStyle::Block if put_args_in_block => false,
13701370
FnArgLayoutStyle::BlockAlways => false,
13711371
_ => {
1372-
// If we've already gone multi-line, or the return type would push
1373-
// over the max width, then put the return type on a new line.
1374-
result.contains("\n") || multi_line_ret_str ||
1375-
result.len() + indent.width() + ret_str_len > context.config.max_width
1372+
// If we've already gone multi-line, or the return type would push over the max
1373+
// width, then put the return type on a new line. With the +1 for the signature
1374+
// length an additional space between the closing parenthesis of the argument and
1375+
// the arrow '->' is considered.
1376+
let mut sig_length = result.len() + indent.width() + ret_str_len + 1;
1377+
1378+
// If there is no where clause, take into account the space after the return type
1379+
// and the brace.
1380+
if where_clause.predicates.is_empty() {
1381+
sig_length += 2;
1382+
}
1383+
1384+
let overlong_sig = sig_length > context.config.max_width;
1385+
1386+
result.contains("\n") || multi_line_ret_str || overlong_sig
13761387
}
13771388
};
13781389
let ret_indent = if ret_should_indent {
@@ -1400,10 +1411,8 @@ fn rewrite_fn_base(context: &RewriteContext,
14001411
if multi_line_ret_str {
14011412
// Now that we know the proper indent and width, we need to
14021413
// re-layout the return type.
1403-
14041414
let budget = try_opt!(context.config.max_width.checked_sub(ret_indent.width()));
1405-
let ret_str = try_opt!(fd.output
1406-
.rewrite(context, budget, ret_indent));
1415+
let ret_str = try_opt!(fd.output.rewrite(context, budget, ret_indent));
14071416
result.push_str(&ret_str);
14081417
} else {
14091418
result.push_str(&ret_str);

tests/source/issue-1049.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Test overlong function signature
2+
pub unsafe fn reborrow_mut(&mut X: Abcde) -> Handle<NodeRef<marker::Mut, K, V, NodeType>, HandleType> {
3+
}
4+
5+
pub fn merge(mut X: Abcdef) -> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::Edge> {
6+
}
7+
8+
impl Handle {
9+
pub fn merge(a: Abcd) -> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::Edge> {
10+
}
11+
}
12+
13+
// Long function without return type that should not be reformated.
14+
fn veeeeeeeeeeeeeeeeeeeeery_long_name(a: FirstTypeeeeeeeeee, b: SecondTypeeeeeeeeeeeeeeeeeeeeeee) {}
15+
16+
fn veeeeeeeeeeeeeeeeeeeeeery_long_name(a: FirstTypeeeeeeeeee, b: SecondTypeeeeeeeeeeeeeeeeeeeeeee) {}

tests/target/issue-1049.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Test overlong function signature
2+
pub unsafe fn reborrow_mut(&mut X: Abcde)
3+
-> Handle<NodeRef<marker::Mut, K, V, NodeType>, HandleType> {
4+
}
5+
6+
pub fn merge(mut X: Abcdef)
7+
-> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::Edge> {
8+
}
9+
10+
impl Handle {
11+
pub fn merge(a: Abcd)
12+
-> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::Edge> {
13+
}
14+
}
15+
16+
// Long function without return type that should not be reformated.
17+
fn veeeeeeeeeeeeeeeeeeeeery_long_name(a: FirstTypeeeeeeeeee, b: SecondTypeeeeeeeeeeeeeeeeeeeeeee) {}
18+
19+
fn veeeeeeeeeeeeeeeeeeeeeery_long_name(a: FirstTypeeeeeeeeee,
20+
b: SecondTypeeeeeeeeeeeeeeeeeeeeeee) {
21+
}

0 commit comments

Comments
 (0)