diff --git a/compiler/rustc_errors/src/markdown/parse.rs b/compiler/rustc_errors/src/markdown/parse.rs index e1b1b32cd3ef9..6512d9ce19974 100644 --- a/compiler/rustc_errors/src/markdown/parse.rs +++ b/compiler/rustc_errors/src/markdown/parse.rs @@ -220,7 +220,7 @@ fn parse_codeblock(buf: &[u8]) -> Parsed<'_> { let mut found = None; for idx in (0..working.len()).filter(|idx| working[*idx..].starts_with(&end_pat)) { let (eol_txt, rest) = parse_to_newline(&working[(idx + end_pat.len())..]); - if !eol_txt.iter().any(u8::is_ascii_whitespace) { + if eol_txt.iter().all(u8::is_ascii_whitespace) { found = Some((&working[..idx], rest)); break; } diff --git a/compiler/rustc_errors/src/markdown/tests/parse.rs b/compiler/rustc_errors/src/markdown/tests/parse.rs index bfcb3de16fa0e..807fda3211799 100644 --- a/compiler/rustc_errors/src/markdown/tests/parse.rs +++ b/compiler/rustc_errors/src/markdown/tests/parse.rs @@ -364,3 +364,16 @@ fn test_snake_case() { let res = entrypoint(SNAKE_CASE); assert_eq!(res, expected); } + +#[test] +fn test_codeblock_trailing_whitespace() { + let buf = "```rust\ncode\n``` \nrest"; + let (t, r) = parse_codeblock(buf.as_bytes()); + assert_eq!(t, MdTree::CodeBlock { txt: "code", lang: Some("rust") }); + assert_eq!(r, b"\nrest"); + + let buf = "```rust\ncode\n```abc\nrest"; + let (t, r) = parse_codeblock(buf.as_bytes()); + assert_eq!(t, MdTree::CodeBlock { txt: "code\n```abc\nrest", lang: Some("rust") }); + assert_eq!(r, b""); +}