Skip to content

Commit 9a35232

Browse files
committed
Auto merge of #84445 - jyn514:hidden, r=<try>
rustdoc: Hide `#text` in doc-tests Since `#![attr]` and `#[attr]` are the only valid syntax that start with `#`, we can just special case those two tokens. Fixes #83284.
2 parents 236580b + af6c320 commit 9a35232

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

src/librustdoc/html/markdown.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,19 @@ fn map_line(s: &str) -> Line<'_> {
147147
let trimmed = s.trim();
148148
if trimmed.starts_with("##") {
149149
Line::Shown(Cow::Owned(s.replacen("##", "#", 1)))
150-
} else if let Some(stripped) = trimmed.strip_prefix("# ") {
151-
// # text
152-
Line::Hidden(&stripped)
153-
} else if trimmed == "#" {
154-
// We cannot handle '#text' because it could be #[attr].
155-
Line::Hidden("")
150+
} else if trimmed.starts_with('#') {
151+
let mut without_hash = trimmed[1..].trim_start();
152+
if without_hash.starts_with('!') {
153+
// #! text
154+
without_hash = without_hash[1..].trim_start_matches(' ');
155+
}
156+
if without_hash.starts_with('[') {
157+
// #[attr] or #![attr]
158+
Line::Shown(Cow::Borrowed(s))
159+
} else {
160+
// #text
161+
Line::Hidden(without_hash)
162+
}
156163
} else {
157164
Line::Shown(Cow::Borrowed(s))
158165
}

src/test/rustdoc-ui/test-hidden.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// check-pass
2+
// compile-flags:--test
3+
// normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
4+
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
5+
6+
/// ```
7+
// If `const_err` becomes a hard error in the future, please replace this with another
8+
// deny-by-default lint instead of removing it altogether
9+
/// # ! [allow(const_err)]
10+
/// const C: usize = 1/0;
11+
///
12+
/// # use std::path::PathBuf;
13+
/// #use std::path::Path;
14+
/// let x = Path::new("y.rs");
15+
/// let x = PathBuf::from("y.rs");
16+
///
17+
/// #[cfg(FALSE)]
18+
/// assert!(false);
19+
///
20+
/// # [cfg(FALSE)]
21+
/// assert!(false);
22+
/// ```
23+
fn main() {
24+
panic!();
25+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
running 1 test
3+
test $DIR/test-hidden.rs - main (line 6) ... ok
4+
5+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
6+

0 commit comments

Comments
 (0)