From 1e593417b32728f5c48a286b3163f74a4dea48fc Mon Sep 17 00:00:00 2001 From: Pavan Date: Thu, 9 Apr 2026 18:44:06 +0530 Subject: [PATCH 1/2] Fix code block whitespace handling --- compiler/rustc_errors/src/markdown/parse.rs | 2 +- compiler/rustc_errors/src/markdown/tests/parse.rs | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) 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..1cfbd7db13bab 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""); +} From ef3a89c3468bf23934316fcc60a01eeb25ff88dd Mon Sep 17 00:00:00 2001 From: Pavan Date: Thu, 9 Apr 2026 19:59:04 +0530 Subject: [PATCH 2/2] Fix tabs in test file to satisfy tidy check --- compiler/rustc_errors/src/markdown/tests/parse.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_errors/src/markdown/tests/parse.rs b/compiler/rustc_errors/src/markdown/tests/parse.rs index 1cfbd7db13bab..807fda3211799 100644 --- a/compiler/rustc_errors/src/markdown/tests/parse.rs +++ b/compiler/rustc_errors/src/markdown/tests/parse.rs @@ -367,7 +367,7 @@ fn test_snake_case() { #[test] fn test_codeblock_trailing_whitespace() { - let buf = "```rust\ncode\n``` \nrest"; + 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");