diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2022-02-08 06:47:36 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-08 06:47:36 +0100 |
| commit | 7d5e2ac5eb78c5d3054267bbec5c31fcf8193507 (patch) | |
| tree | 0ca562fb593e9faa1a6b2c4b26346ae4a9beab2a | |
| parent | b7f785092d84a4b4dadb08fc9fdfe86b0b6139e6 (diff) | |
| parent | a476ec8bd0b1925d349383110b75ff51567d7534 (diff) | |
| download | rust-7d5e2ac5eb78c5d3054267bbec5c31fcf8193507.tar.gz rust-7d5e2ac5eb78c5d3054267bbec5c31fcf8193507.zip | |
Rollup merge of #93715 - GuillaumeGomez:horizontal-trim, r=notriddle
Fix horizontal trim for block doc comments Fixes #93662. r? `@notriddle`
| -rw-r--r-- | compiler/rustc_ast/src/util/comments.rs | 16 | ||||
| -rw-r--r-- | compiler/rustc_ast/src/util/comments/tests.rs | 20 | ||||
| -rw-r--r-- | src/test/rustdoc-ui/block-doc-comment.rs | 16 | ||||
| -rw-r--r-- | src/test/rustdoc-ui/block-doc-comment.stdout | 5 | ||||
| -rw-r--r-- | src/test/rustdoc/strip-block-doc-comments-stars.rs | 2 |
5 files changed, 52 insertions, 7 deletions
diff --git a/compiler/rustc_ast/src/util/comments.rs b/compiler/rustc_ast/src/util/comments.rs index 612ee71f350..f51b0086dc8 100644 --- a/compiler/rustc_ast/src/util/comments.rs +++ b/compiler/rustc_ast/src/util/comments.rs @@ -43,7 +43,7 @@ pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol { if i != 0 || j != lines.len() { Some((i, j)) } else { None } } - fn get_horizontal_trim(lines: &[&str], kind: CommentKind) -> Option<usize> { + fn get_horizontal_trim<'a>(lines: &'a [&str], kind: CommentKind) -> Option<String> { let mut i = usize::MAX; let mut first = true; @@ -51,7 +51,8 @@ pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol { // present. However, we first need to strip the empty lines so they don't get in the middle // when we try to compute the "horizontal trim". let lines = if kind == CommentKind::Block { - let mut i = 0; + // Whatever happens, we skip the first line. + let mut i = if lines[0].trim_start().starts_with('*') { 0 } else { 1 }; let mut j = lines.len(); while i < j && lines[i].trim().is_empty() { @@ -84,7 +85,7 @@ pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol { return None; } } - Some(i) + if lines.is_empty() { None } else { Some(lines[0][..i].into()) } } let data_s = data.as_str(); @@ -102,8 +103,13 @@ pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol { changes = true; // remove a "[ \t]*\*" block from each line, if possible for line in lines.iter_mut() { - if horizontal + 1 < line.len() { - *line = &line[horizontal + 1..]; + if let Some(tmp) = line.strip_prefix(&horizontal) { + *line = tmp; + if kind == CommentKind::Block + && (*line == "*" || line.starts_with("* ") || line.starts_with("**")) + { + *line = &line[1..]; + } } } } diff --git a/compiler/rustc_ast/src/util/comments/tests.rs b/compiler/rustc_ast/src/util/comments/tests.rs index 98f692a7724..11d50603a10 100644 --- a/compiler/rustc_ast/src/util/comments/tests.rs +++ b/compiler/rustc_ast/src/util/comments/tests.rs @@ -24,7 +24,7 @@ fn test_block_doc_comment_3() { create_default_session_globals_then(|| { let comment = "\n let a: *i32;\n *a = 5;\n"; let stripped = beautify_doc_string(Symbol::intern(comment), CommentKind::Block); - assert_eq!(stripped.as_str(), " let a: *i32;\n *a = 5;"); + assert_eq!(stripped.as_str(), "let a: *i32;\n*a = 5;"); }) } @@ -41,3 +41,21 @@ fn test_line_doc_comment() { assert_eq!(stripped.as_str(), "!test"); }) } + +#[test] +fn test_doc_blocks() { + create_default_session_globals_then(|| { + let stripped = + beautify_doc_string(Symbol::intern(" # Returns\n *\n "), CommentKind::Block); + assert_eq!(stripped.as_str(), " # Returns\n\n"); + + let stripped = beautify_doc_string( + Symbol::intern("\n * # Returns\n *\n "), + CommentKind::Block, + ); + assert_eq!(stripped.as_str(), " # Returns\n\n"); + + let stripped = beautify_doc_string(Symbol::intern("\n * a\n "), CommentKind::Block); + assert_eq!(stripped.as_str(), " a\n"); + }) +} diff --git a/src/test/rustdoc-ui/block-doc-comment.rs b/src/test/rustdoc-ui/block-doc-comment.rs new file mode 100644 index 00000000000..c60dfa3f951 --- /dev/null +++ b/src/test/rustdoc-ui/block-doc-comment.rs @@ -0,0 +1,16 @@ +// check-pass +// compile-flags:--test + +// This test ensures that no code block is detected in the doc comments. + +pub mod Wormhole { + /** # Returns + * + */ + pub fn foofoo() {} + /** + * # Returns + * + */ + pub fn barbar() {} +} diff --git a/src/test/rustdoc-ui/block-doc-comment.stdout b/src/test/rustdoc-ui/block-doc-comment.stdout new file mode 100644 index 00000000000..e5c27bebbdb --- /dev/null +++ b/src/test/rustdoc-ui/block-doc-comment.stdout @@ -0,0 +1,5 @@ + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s + diff --git a/src/test/rustdoc/strip-block-doc-comments-stars.rs b/src/test/rustdoc/strip-block-doc-comments-stars.rs index ed2297b4fac..ea28d84f1ff 100644 --- a/src/test/rustdoc/strip-block-doc-comments-stars.rs +++ b/src/test/rustdoc/strip-block-doc-comments-stars.rs @@ -1,6 +1,6 @@ #![crate_name = "foo"] -// The goal of this test is to answer that it won't be generated as a list because +// The goal of this test is to ensure that it won't be generated as a list because // block doc comments can have their lines starting with a star. // @has foo/fn.foo.html |
