Skip to content

Commit 87f2897

Browse files
Improve code in unindent_comment a bit more
1 parent fcee70f commit 87f2897

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

src/librustdoc/passes/unindent_comments.rs

+22-21
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn unindent_fragments(docs: &mut Vec<DocFragment>) {
4646
// #[doc = "another"]
4747
//
4848
// In this case, you want "hello! another" and not "hello! another".
49-
let add = if !docs.windows(2).all(|arr| arr[0].kind == arr[1].kind)
49+
let add = if docs.windows(2).any(|arr| arr[0].kind != arr[1].kind)
5050
&& docs.iter().any(|d| d.kind == DocFragmentKind::SugaredDoc)
5151
{
5252
// In case we have a mix of sugared doc comments and "raw" ones, we want the sugared one to
@@ -87,27 +87,28 @@ fn unindent_fragments(docs: &mut Vec<DocFragment>) {
8787
};
8888

8989
for fragment in docs {
90-
let lines: Vec<_> = fragment.doc.lines().collect();
90+
if fragment.doc.lines().count() == 0 {
91+
continue;
92+
}
9193

92-
if !lines.is_empty() {
93-
let min_indent = if fragment.kind != DocFragmentKind::SugaredDoc && min_indent > 0 {
94-
min_indent - add
95-
} else {
96-
min_indent
97-
};
94+
let min_indent = if fragment.kind != DocFragmentKind::SugaredDoc && min_indent > 0 {
95+
min_indent - add
96+
} else {
97+
min_indent
98+
};
9899

99-
fragment.doc = lines
100-
.iter()
101-
.map(|&line| {
102-
if line.chars().all(|c| c.is_whitespace()) {
103-
line.to_string()
104-
} else {
105-
assert!(line.len() >= min_indent);
106-
line[min_indent..].to_string()
107-
}
108-
})
109-
.collect::<Vec<_>>()
110-
.join("\n");
111-
}
100+
fragment.doc = fragment
101+
.doc
102+
.lines()
103+
.map(|line| {
104+
if line.chars().all(|c| c.is_whitespace()) {
105+
line.to_string()
106+
} else {
107+
assert!(line.len() >= min_indent);
108+
line[min_indent..].to_string()
109+
}
110+
})
111+
.collect::<Vec<_>>()
112+
.join("\n");
112113
}
113114
}

0 commit comments

Comments
 (0)