Skip to content
/ rust Public
forked from rust-lang/rust

Commit 1dcdfb2

Browse files
authored
fix(rustfmt): fix struct field formatting with doc comments present (rust-lang#5217)
* fix(rustfmt): fix struct field formatting with doc comments present Fixes rust-lang#5215 * fix review feedbacks * add unit test without doc comment * move tests to a seperate file * add additional test cases * reintroduce a newline at the of test/souce/structs.rs
1 parent 5ff7b63 commit 1dcdfb2

File tree

3 files changed

+142
-1
lines changed

3 files changed

+142
-1
lines changed

Diff for: src/items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1771,7 +1771,7 @@ pub(crate) fn rewrite_struct_field(
17711771
.offset_left(overhead + spacing.len())
17721772
.and_then(|ty_shape| field.ty.rewrite(context, ty_shape));
17731773
if let Some(ref ty) = orig_ty {
1774-
if !ty.contains('\n') {
1774+
if !ty.contains('\n') && !contains_comment(context.snippet(missing_span)) {
17751775
return Some(attr_prefix + &spacing + ty);
17761776
}
17771777
}

Diff for: tests/source/struct_field_doc_comment.rs

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

Diff for: tests/target/struct_field_doc_comment.rs

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

0 commit comments

Comments
 (0)