Skip to content

Commit

Permalink
remove trailing whitespace from multi-line tuple struct field prefix
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ytmimi committed Aug 7, 2023
1 parent a72613b commit 39387c8
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1853,7 +1853,7 @@ pub(crate) fn rewrite_struct_field(
}

let type_annotation_spacing = type_annotation_spacing(context.config);
let prefix = rewrite_struct_field_prefix(context, field)?;
let mut prefix = rewrite_struct_field_prefix(context, field)?;

let attrs_str = field.attrs.rewrite(context, shape)?;
let attrs_extendable = field.ident.is_none() && is_attributes_extendable(&attrs_str);
Expand Down Expand Up @@ -1896,6 +1896,11 @@ pub(crate) fn rewrite_struct_field(

let is_prefix_empty = prefix.is_empty();
// We must use multiline. We are going to put attributes and a field on different lines.
if context.config.version() == Version::Two {
// Remove any additional whitespace at the end of the prefix.
// For example if there is a space after a visibility modifier.
prefix.truncate(prefix.trim_end().len());
}
let field_str = rewrite_assign_rhs(context, prefix, &*field.ty, &RhsAssignKind::Ty, shape)?;
// Remove a leading white-space from `rewrite_assign_rhs()` when rewriting a tuple struct.
let field_str = if is_prefix_empty {
Expand Down
9 changes: 9 additions & 0 deletions tests/target/issue_5525.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// rustfmt-version: Two

pub struct SomeCallback(
pub extern "C" fn(
long_argument_name_to_avoid_wrap: u32,
second_long_argument_name: u32,
third_long_argument_name: u32,
),
);
7 changes: 7 additions & 0 deletions tests/target/issue_5703.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// rustfmt-version: Two

#[derive(Clone, Debug, Default)]
pub struct ReactionGroup(
pub(in crate::room::timeline)
IndexMap<(Option<OwnedTransactionId>, Option<OwnedEventId>), OwnedUserId>,
);
71 changes: 71 additions & 0 deletions tests/target/struct_field_doc_comment_v2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// rustfmt-version: Two

// #5215
struct MyTuple(
/// Doc Comments
/* TODO note to add more to Doc Comments */
u32,
/// Doc Comments
// TODO note
u64,
);

struct MyTuple(
#[cfg(unix)] // some comment
u64,
#[cfg(not(unix))] /*block comment */ u32,
);

struct MyTuple(
#[cfg(unix)]
// some comment
u64,
#[cfg(not(unix))]
/*block comment */
u32,
);

struct MyTuple(
#[cfg(unix)] // some comment
pub u64,
#[cfg(not(unix))] /*block comment */ pub(crate) u32,
);

struct MyTuple(
/// Doc Comments
/* TODO note to add more to Doc Comments */
pub u32,
/// Doc Comments
// TODO note
pub(crate) u64,
);

struct MyStruct {
#[cfg(unix)] // some comment
a: u64,
#[cfg(not(unix))] /*block comment */ b: u32,
}

struct MyStruct {
#[cfg(unix)] // some comment
pub a: u64,
#[cfg(not(unix))] /*block comment */ pub(crate) b: u32,
}

struct MyStruct {
/// Doc Comments
/* TODO note to add more to Doc Comments */
a: u32,
/// Doc Comments
// TODO note
b: u64,
}

struct MyStruct {
/// Doc Comments
/* TODO note to add more to Doc Comments */
pub a: u32,
/// Doc Comments
// TODO note
pub(crate) b: u64,
}

0 comments on commit 39387c8

Please sign in to comment.