Skip to content

Commit c591e66

Browse files
committed
remove trailing whitespace from multi-line tuple struct field prefix
Fixes 5703, Fixes 5525 visibility modifiers always contain a trailing space after them. If the formatted tuple field needs to be written over multiple lines then the extra space will cause issues. In the best case the space will offset the type name by an extra space and in the worst case it will lead to a "left behind trailing whitespace" error.
1 parent b636723 commit c591e66

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

src/items.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1851,7 +1851,7 @@ pub(crate) fn rewrite_struct_field(
18511851
}
18521852

18531853
let type_annotation_spacing = type_annotation_spacing(context.config);
1854-
let prefix = rewrite_struct_field_prefix(context, field)?;
1854+
let mut prefix = rewrite_struct_field_prefix(context, field)?;
18551855

18561856
let attrs_str = field.attrs.rewrite(context, shape)?;
18571857
let attrs_extendable = field.ident.is_none() && is_attributes_extendable(&attrs_str);
@@ -1894,6 +1894,11 @@ pub(crate) fn rewrite_struct_field(
18941894

18951895
let is_prefix_empty = prefix.is_empty();
18961896
// We must use multiline. We are going to put attributes and a field on different lines.
1897+
if context.config.version() == Version::Two {
1898+
// Remove any additional whitespace at the end of the prefix.
1899+
// For example if there is a space after a visibility modifier.
1900+
prefix.truncate(prefix.trim_end().len());
1901+
}
18971902
let field_str = rewrite_assign_rhs(context, prefix, &*field.ty, &RhsAssignKind::Ty, shape)?;
18981903
// Remove a leading white-space from `rewrite_assign_rhs()` when rewriting a tuple struct.
18991904
let field_str = if is_prefix_empty {

tests/target/issue_5525.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// rustfmt-version: Two
2+
3+
pub struct SomeCallback(
4+
pub extern "C" fn(
5+
long_argument_name_to_avoid_wrap: u32,
6+
second_long_argument_name: u32,
7+
third_long_argument_name: u32,
8+
),
9+
);

tests/target/issue_5703.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-version: Two
2+
3+
#[derive(Clone, Debug, Default)]
4+
pub struct ReactionGroup(
5+
pub(in crate::room::timeline)
6+
IndexMap<(Option<OwnedTransactionId>, Option<OwnedEventId>), OwnedUserId>,
7+
);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// rustfmt-version: Two
2+
3+
// #5215
4+
struct MyTuple(
5+
/// Doc Comments
6+
/* TODO note to add more to Doc Comments */
7+
u32,
8+
/// Doc Comments
9+
// TODO note
10+
u64,
11+
);
12+
13+
struct MyTuple(
14+
#[cfg(unix)] // some comment
15+
u64,
16+
#[cfg(not(unix))] /*block comment */ u32,
17+
);
18+
19+
struct MyTuple(
20+
#[cfg(unix)]
21+
// some comment
22+
u64,
23+
#[cfg(not(unix))]
24+
/*block comment */
25+
u32,
26+
);
27+
28+
struct MyTuple(
29+
#[cfg(unix)] // some comment
30+
pub u64,
31+
#[cfg(not(unix))] /*block comment */ pub(crate) u32,
32+
);
33+
34+
struct MyTuple(
35+
/// Doc Comments
36+
/* TODO note to add more to Doc Comments */
37+
pub u32,
38+
/// Doc Comments
39+
// TODO note
40+
pub(crate) u64,
41+
);
42+
43+
struct MyStruct {
44+
#[cfg(unix)] // some comment
45+
a: u64,
46+
#[cfg(not(unix))] /*block comment */ b: u32,
47+
}
48+
49+
struct MyStruct {
50+
#[cfg(unix)] // some comment
51+
pub a: u64,
52+
#[cfg(not(unix))] /*block comment */ pub(crate) b: u32,
53+
}
54+
55+
struct MyStruct {
56+
/// Doc Comments
57+
/* TODO note to add more to Doc Comments */
58+
a: u32,
59+
/// Doc Comments
60+
// TODO note
61+
b: u64,
62+
}
63+
64+
struct MyStruct {
65+
/// Doc Comments
66+
/* TODO note to add more to Doc Comments */
67+
pub a: u32,
68+
/// Doc Comments
69+
// TODO note
70+
pub(crate) b: u64,
71+
}

0 commit comments

Comments
 (0)