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 all commits
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(context.snippet(missing_span)) {
return Some(attr_prefix + &spacing + ty);
}
}
Expand Down
72 changes: 72 additions & 0 deletions tests/source/struct_field_doc_comment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// #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,
}
69 changes: 69 additions & 0 deletions tests/target/struct_field_doc_comment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// #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,
}