Auto merge of #16081 - riverbl:trailing-whitespace, r=Veykril

Don't trim trailing whitespace from doc comments

Don't trim trailing whitespace from doc comments as multiple trailing spaces indicates a hard line break in Markdown.

I'd have liked to add a unit test for `docs_from_attrs`, but couldn't find a reasonable way to get an `&Attrs` object for use in the test.

Fixes #15877.
This commit is contained in:
bors 2024-01-02 10:30:58 +00:00
commit ee0d99d98e

View File

@ -138,15 +138,13 @@ pub fn docs_from_attrs(attrs: &hir::Attrs) -> Option<String> {
for doc in docs { for doc in docs {
// str::lines doesn't yield anything for the empty string // str::lines doesn't yield anything for the empty string
if !doc.is_empty() { if !doc.is_empty() {
buf.extend(Itertools::intersperse( // We don't trim trailing whitespace from doc comments as multiple trailing spaces
doc.lines().map(|line| { // indicates a hard line break in Markdown.
line.char_indices() let lines = doc.lines().map(|line| {
.nth(indent) line.char_indices().nth(indent).map_or(line, |(offset, _)| &line[offset..])
.map_or(line, |(offset, _)| &line[offset..]) });
.trim_end()
}), buf.extend(Itertools::intersperse(lines, "\n"));
"\n",
));
} }
buf.push('\n'); buf.push('\n');
} }