Accept E<error_number> notation in doctests

```compile_fail,E0000
```

The code was stolen from rustdoc at 392ba2ba1a/src/librustdoc/html/markdown.rs (L866-L867)
This commit is contained in:
Chayim Refael Friedman
2021-04-18 05:09:20 +03:00
parent 19fc1f333f
commit 6c287e1504
2 changed files with 23 additions and 5 deletions

View File

@@ -27,9 +27,8 @@ pub(crate) fn format_docs(src: &str) -> String {
in_code_block ^= true;
if in_code_block {
is_rust = header
.split(',')
.all(|sub| RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUST_SPECIFIC.contains(&sub.trim()));
is_rust =
header.split(',').all(|sub| is_rust_specific_code_block_attribute(sub.trim()));
if is_rust {
line = "```rust";
@@ -42,6 +41,13 @@ pub(crate) fn format_docs(src: &str) -> String {
processed_lines.join("\n")
}
fn is_rust_specific_code_block_attribute(attr: &str) -> bool {
if RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUST_SPECIFIC.contains(&attr) {
return true;
}
attr.starts_with('E') && attr.len() == 5 && attr[1..].parse::<u32>().is_ok()
}
fn code_line_ignored_by_rustdoc(line: &str) -> bool {
let trimmed = line.trim();
trimmed == "#" || trimmed.starts_with("# ") || trimmed.starts_with("#\t")
@@ -81,6 +87,12 @@ mod tests {
assert_eq!(format_docs(comment), "```rust\nlet z = 55;\n```");
}
#[test]
fn test_format_docs_handles_error_codes() {
let comment = "```compile_fail,E0641\nlet b = 0 as *const _;\n```";
assert_eq!(format_docs(comment), "```rust\nlet b = 0 as *const _;\n```");
}
#[test]
fn test_format_docs_skips_comments_in_rust_block() {
let comment =