Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit bd29e16

Browse files
authored
Merge pull request rust-lang#3171 from lqd/whitespace_jam
normalize_doc_attributes: remove whitespace from the doc comment opener
2 parents 2a34414 + 83d1d9a commit bd29e16

File tree

3 files changed

+70
-10
lines changed

3 files changed

+70
-10
lines changed

src/attr.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,11 @@ impl Rewrite for ast::Attribute {
326326
ast::AttrStyle::Outer => CommentStyle::TripleSlash,
327327
};
328328

329-
let doc_comment = format!("{}{}", comment_style.opener(), literal);
329+
// Remove possible whitespace from the `CommentStyle::opener()` so that
330+
// the literal itself has control over the comment's leading spaces.
331+
let opener = comment_style.opener().trim_end();
332+
333+
let doc_comment = format!("{}{}", opener, literal);
330334
return rewrite_doc_comment(
331335
&doc_comment,
332336
shape.comment(context.config),

tests/source/doc-attrib.rs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,42 @@
22
// rustfmt-normalize_doc_attributes: true
33

44
// Only doc = "" attributes should be normalized
5-
#![doc = "Example doc attribute comment"]
5+
#![doc = " Example doc attribute comment"]
6+
#![doc = " Example doc attribute comment with 10 leading spaces"]
67
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
78
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
89
html_root_url = "https://doc.rust-lang.org/nightly/",
910
html_playground_url = "https://play.rust-lang.org/", test(attr(deny(warnings))))]
1011

1112

1213
// Long `#[doc = "..."]`
13-
struct A { #[doc = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"] b: i32 }
14+
struct A { #[doc = " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"] b: i32 }
1415

1516

16-
#[doc = "The `nodes` and `edges` method each return instantiations of `Cow<[T]>` to leave implementers the freedom to create entirely new vectors or to pass back slices into internally owned vectors."]
17+
#[doc = " The `nodes` and `edges` method each return instantiations of `Cow<[T]>` to leave implementers the freedom to create entirely new vectors or to pass back slices into internally owned vectors."]
1718
struct B { b: i32 }
1819

1920

20-
#[doc = "Level 1 comment"]
21+
#[doc = " Level 1 comment"]
2122
mod tests {
22-
#[doc = "Level 2 comment"]
23+
#[doc = " Level 2 comment"]
2324
impl A {
24-
#[doc = "Level 3 comment"]
25+
#[doc = " Level 3 comment"]
2526
fn f() {
26-
#[doc = "Level 4 comment"]
27+
#[doc = " Level 4 comment"]
2728
fn g() {
2829
}
2930
}
3031
}
3132
}
3233

3334
struct C {
34-
#[doc = "item doc attrib comment"]
35+
#[doc = " item doc attrib comment"]
3536
// regular item comment
3637
b: i32,
3738

3839
// regular item comment
39-
#[doc = "item doc attrib comment"]
40+
#[doc = " item doc attrib comment"]
4041
c: i32,
4142
}
4243

@@ -89,3 +90,30 @@ pub struct Params {
8990
all(target_arch = "wasm32", feature = "wasm-bindgen"),
9091
))))]
9192
type Os = NoSource;
93+
94+
// use cases from bindgen needing precise control over leading spaces
95+
#[doc = " <div rustbindgen accessor></div>"]
96+
#[repr(C)]
97+
#[derive(Debug, Default, Copy, Clone)]
98+
pub struct ContradictAccessors {
99+
#[doc = "<foo>no leading spaces here</foo>"]
100+
pub mBothAccessors: ::std::os::raw::c_int,
101+
#[doc = " <div rustbindgen accessor=\"false\"></div>"]
102+
pub mNoAccessors: ::std::os::raw::c_int,
103+
#[doc = " <div rustbindgen accessor=\"unsafe\"></div>"]
104+
pub mUnsafeAccessors: ::std::os::raw::c_int,
105+
#[doc = " <div rustbindgen accessor=\"immutable\"></div>"]
106+
pub mImmutableAccessor: ::std::os::raw::c_int,
107+
}
108+
109+
#[doc = " \\brief MPI structure"]
110+
#[repr(C)]
111+
#[derive(Debug, Copy, Clone)]
112+
pub struct mbedtls_mpi {
113+
#[doc = "< integer sign"]
114+
pub s: ::std::os::raw::c_int,
115+
#[doc = "< total # of limbs"]
116+
pub n: ::std::os::raw::c_ulong,
117+
#[doc = "< pointer to limbs"]
118+
pub p: *mut mbedtls_mpi_uint,
119+
}

tests/target/doc-attrib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
// Only doc = "" attributes should be normalized
55
//! Example doc attribute comment
6+
//! Example doc attribute comment with 10 leading spaces
67
#![doc(
78
html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
89
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
@@ -103,3 +104,30 @@ mod issue_2620 {
103104
)
104105
)))]
105106
type Os = NoSource;
107+
108+
// use cases from bindgen needing precise control over leading spaces
109+
/// <div rustbindgen accessor></div>
110+
#[repr(C)]
111+
#[derive(Debug, Default, Copy, Clone)]
112+
pub struct ContradictAccessors {
113+
///<foo>no leading spaces here</foo>
114+
pub mBothAccessors: ::std::os::raw::c_int,
115+
/// <div rustbindgen accessor="false"></div>
116+
pub mNoAccessors: ::std::os::raw::c_int,
117+
/// <div rustbindgen accessor="unsafe"></div>
118+
pub mUnsafeAccessors: ::std::os::raw::c_int,
119+
/// <div rustbindgen accessor="immutable"></div>
120+
pub mImmutableAccessor: ::std::os::raw::c_int,
121+
}
122+
123+
/// \brief MPI structure
124+
#[repr(C)]
125+
#[derive(Debug, Copy, Clone)]
126+
pub struct mbedtls_mpi {
127+
///< integer sign
128+
pub s: ::std::os::raw::c_int,
129+
///< total # of limbs
130+
pub n: ::std::os::raw::c_ulong,
131+
///< pointer to limbs
132+
pub p: *mut mbedtls_mpi_uint,
133+
}

0 commit comments

Comments
 (0)