Skip to content

Commit 5df8c8f

Browse files
authored
Fix doc of generic items formmating error (#5124)
* Fix doc of generic items formmating error * Remove tracked `attrs_end_with_doc_comment` flag in `RewriteContext` * Fix duplicated doc comments of const generic params * Fix `<ast::GenericParam as Spanned>::span()` * Remove duplicated source file of `doc-of-generic-item.rs`
1 parent fd6e11c commit 5df8c8f

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

src/spanned.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,10 @@ impl Spanned for ast::Param {
113113

114114
impl Spanned for ast::GenericParam {
115115
fn span(&self) -> Span {
116-
let lo = if let ast::GenericParamKind::Const {
117-
ty: _,
118-
kw_span,
119-
default: _,
120-
} = self.kind
121-
{
122-
kw_span.lo()
123-
} else if self.attrs.is_empty() {
124-
self.ident.span.lo()
125-
} else {
126-
self.attrs[0].span.lo()
116+
let lo = match self.kind {
117+
_ if !self.attrs.is_empty() => self.attrs[0].span.lo(),
118+
ast::GenericParamKind::Const { kw_span, .. } => kw_span.lo(),
119+
_ => self.ident.span.lo(),
127120
};
128121
let hi = if self.bounds.is_empty() {
129122
self.ident.span.hi()

src/types.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,16 @@ impl Rewrite for ast::GenericParam {
575575
let mut result = String::with_capacity(128);
576576
// FIXME: If there are more than one attributes, this will force multiline.
577577
match self.attrs.rewrite(context, shape) {
578-
Some(ref rw) if !rw.is_empty() => result.push_str(&format!("{} ", rw)),
578+
Some(ref rw) if !rw.is_empty() => {
579+
result.push_str(rw);
580+
// When rewriting generic params, an extra newline should be put
581+
// if the attributes end with a doc comment
582+
if let Some(true) = self.attrs.last().map(|a| a.is_doc_comment()) {
583+
result.push_str(&shape.indent.to_string_with_newline(context.config));
584+
} else {
585+
result.push(' ');
586+
}
587+
}
579588
_ => (),
580589
}
581590

tests/target/doc-of-generic-item.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Non-doc pre-comment of Foo
2+
/// doc of Foo
3+
// Non-doc post-comment of Foo
4+
struct Foo<
5+
// Non-doc pre-comment of 'a
6+
/// doc of 'a
7+
'a,
8+
// Non-doc pre-comment of T
9+
/// doc of T
10+
T,
11+
// Non-doc pre-comment of N
12+
/// doc of N
13+
const N: item,
14+
>;

0 commit comments

Comments
 (0)