Skip to content

Commit e394287

Browse files
Fix horizontal trim for block doc comments
1 parent a00e130 commit e394287

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

compiler/rustc_ast/src/util/comments.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,16 @@ pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol {
4343
if i != 0 || j != lines.len() { Some((i, j)) } else { None }
4444
}
4545

46-
fn get_horizontal_trim(lines: &[&str], kind: CommentKind) -> Option<usize> {
46+
fn get_horizontal_trim<'a>(lines: &'a [&str], kind: CommentKind) -> Option<String> {
4747
let mut i = usize::MAX;
4848
let mut first = true;
4949

5050
// In case we have doc comments like `/**` or `/*!`, we want to remove stars if they are
5151
// present. However, we first need to strip the empty lines so they don't get in the middle
5252
// when we try to compute the "horizontal trim".
5353
let lines = if kind == CommentKind::Block {
54-
let mut i = 0;
54+
// Whatever happens, we skip the first line.
55+
let mut i = if lines[0].trim_start().starts_with('*') { 0 } else { 1 };
5556
let mut j = lines.len();
5657

5758
while i < j && lines[i].trim().is_empty() {
@@ -84,7 +85,7 @@ pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol {
8485
return None;
8586
}
8687
}
87-
Some(i)
88+
if lines.is_empty() { None } else { Some(lines[0][..i].into()) }
8889
}
8990

9091
let data_s = data.as_str();
@@ -102,8 +103,13 @@ pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol {
102103
changes = true;
103104
// remove a "[ \t]*\*" block from each line, if possible
104105
for line in lines.iter_mut() {
105-
if horizontal + 1 < line.len() {
106-
*line = &line[horizontal + 1..];
106+
if let Some(tmp) = line.strip_prefix(&horizontal) {
107+
*line = tmp;
108+
if kind == CommentKind::Block
109+
&& (*line == "*" || line.starts_with("* ") || line.starts_with("**"))
110+
{
111+
*line = &line[1..];
112+
}
107113
}
108114
}
109115
}

compiler/rustc_ast/src/util/comments/tests.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn test_block_doc_comment_3() {
2424
create_default_session_globals_then(|| {
2525
let comment = "\n let a: *i32;\n *a = 5;\n";
2626
let stripped = beautify_doc_string(Symbol::intern(comment), CommentKind::Block);
27-
assert_eq!(stripped.as_str(), " let a: *i32;\n *a = 5;");
27+
assert_eq!(stripped.as_str(), "let a: *i32;\n*a = 5;");
2828
})
2929
}
3030

@@ -41,3 +41,29 @@ fn test_line_doc_comment() {
4141
assert_eq!(stripped.as_str(), "!test");
4242
})
4343
}
44+
45+
#[test]
46+
fn test_doc_blocks() {
47+
create_default_session_globals_then(|| {
48+
let stripped = beautify_doc_string(
49+
Symbol::intern(
50+
" # Returns
51+
*
52+
",
53+
),
54+
CommentKind::Block,
55+
);
56+
assert_eq!(stripped.as_str(), " # Returns\n\n");
57+
58+
let stripped = beautify_doc_string(
59+
Symbol::intern(
60+
"
61+
* # Returns
62+
*
63+
",
64+
),
65+
CommentKind::Block,
66+
);
67+
assert_eq!(stripped.as_str(), " # Returns\n\n");
68+
})
69+
}

0 commit comments

Comments
 (0)