Skip to content

fix(rustfmt): fix struct field formatting with doc comments present #5217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1733,7 +1733,7 @@ pub(crate) fn rewrite_struct_field(
.offset_left(overhead + spacing.len())
.and_then(|ty_shape| field.ty.rewrite(context, ty_shape));
if let Some(ref ty) = orig_ty {
if !ty.contains('\n') {
if !ty.contains('\n') && !contains_comment(&attr_prefix) {
return Some(attr_prefix + &spacing + ty);
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/source/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,12 @@ struct Test {
// #2818
struct Paren((i32)) where i32: Trait;
struct Parens((i32, i32)) where i32: Trait;

// #5215
struct MyTuple(
/// Doc Comments
/* TODO note to add more to Doc Comments */ u32,
/// Doc Comments
// TODO note
u64,
);
2 changes: 1 addition & 1 deletion tests/target/configs/struct_field_align_threshold/20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct Foo {
#[AnAttribute]
g: SomeOtherType,
/// A doc comment on a field
h: AThirdType,
h: AThirdType,
pub i: TypeForPublicField,
}

Expand Down
10 changes: 10 additions & 0 deletions tests/target/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,13 @@ where
struct Parens((i32, i32))
where
i32: Trait;

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