From 30ae8164e6fa04ce7b2c040f367ce086cc0010ef Mon Sep 17 00:00:00 2001 From: Ly Date: Thu, 2 Apr 2026 01:20:04 +0800 Subject: [PATCH 1/7] fix: avoid leaking wrapper space from doc comment code block formatting --- src/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index d3379a4564b..0a3c591fbbf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -382,6 +382,22 @@ fn format_code_block( // Remove wrapping main block formatted.unwrap_code_block(); + // If wrapper collapsed to a single line like `fn main() { stmt }`, + // extract the body directly so the wrapper's space before `}` + // does not leak into final snippet. + if let Some(body) = formatted + .snippet + .trim_end_matches('\n') + .strip_prefix(FN_MAIN_PREFIX.trim_end()) + .and_then(|s| s.strip_prefix(' ')) + .and_then(|s| s.strip_suffix(" }")) + { + return Some(FormattedSnippet { + snippet: body.to_owned(), + non_formatted_ranges: formatted.non_formatted_ranges, + }); + } + // Trim "fn main() {" on the first line and "}" on the last line, // then unindent the whole code block. let block_len = formatted From 215efee9602e897c33352f1c73d7c547a30aeb12 Mon Sep 17 00:00:00 2001 From: Ly Date: Thu, 2 Apr 2026 02:29:21 +0800 Subject: [PATCH 2/7] test: target only test case for issue #6832 --- tests/target/issue-6832.rs | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 tests/target/issue-6832.rs diff --git a/tests/target/issue-6832.rs b/tests/target/issue-6832.rs new file mode 100644 index 00000000000..530a7834dbc --- /dev/null +++ b/tests/target/issue-6832.rs @@ -0,0 +1,8 @@ +// rustfmt-format_code_in_doc_comments: true +// rustfmt-fn_single_line: true +// rustfmt-error_on_unformatted: true + +/// ```rust +/// let font = String::from("hello"); +/// ``` +fn main() {} From 8097bc550ecd566a06bafb563e78ee75d588a827 Mon Sep 17 00:00:00 2001 From: Ly Date: Wed, 8 Apr 2026 14:01:55 +0800 Subject: [PATCH 3/7] chore: unify whitespace handling in `block_len` calc --- src/lib.rs | 17 +---------------- tests/target/issue-6832.rs | 1 - 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0a3c591fbbf..bef953284c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -382,27 +382,12 @@ fn format_code_block( // Remove wrapping main block formatted.unwrap_code_block(); - // If wrapper collapsed to a single line like `fn main() { stmt }`, - // extract the body directly so the wrapper's space before `}` - // does not leak into final snippet. - if let Some(body) = formatted - .snippet - .trim_end_matches('\n') - .strip_prefix(FN_MAIN_PREFIX.trim_end()) - .and_then(|s| s.strip_prefix(' ')) - .and_then(|s| s.strip_suffix(" }")) - { - return Some(FormattedSnippet { - snippet: body.to_owned(), - non_formatted_ranges: formatted.non_formatted_ranges, - }); - } - // Trim "fn main() {" on the first line and "}" on the last line, // then unindent the whole code block. let block_len = formatted .snippet .rfind('}') + .map(|i| formatted.snippet[..i].strip_suffix(' ').map_or(i, str::len)) .unwrap_or_else(|| formatted.snippet.len()); // It's possible that `block_len < FN_MAIN_PREFIX.len()`. This can happen if the code block was diff --git a/tests/target/issue-6832.rs b/tests/target/issue-6832.rs index 530a7834dbc..cd7537aed4e 100644 --- a/tests/target/issue-6832.rs +++ b/tests/target/issue-6832.rs @@ -1,6 +1,5 @@ // rustfmt-format_code_in_doc_comments: true // rustfmt-fn_single_line: true -// rustfmt-error_on_unformatted: true /// ```rust /// let font = String::from("hello"); From 0bab52c1722e01f2b2db94507f2c9188fbd053e9 Mon Sep 17 00:00:00 2001 From: Ly Date: Thu, 16 Apr 2026 22:54:24 +0800 Subject: [PATCH 4/7] docs: description comment over fixed line Co-authored-by: Matthew Hughes --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index bef953284c5..aeb05584fff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -387,6 +387,7 @@ fn format_code_block( let block_len = formatted .snippet .rfind('}') + // trim and trailing whitespace we added, e.g. if we format to fn main { some_func(); } .map(|i| formatted.snippet[..i].strip_suffix(' ').map_or(i, str::len)) .unwrap_or_else(|| formatted.snippet.len()); From a58d8d8d582cb02439b143c629aaf6ceae769253 Mon Sep 17 00:00:00 2001 From: Ly Date: Wed, 22 Apr 2026 22:20:27 +0800 Subject: [PATCH 5/7] test: new testcase around multi-byte unicode [skip ci] --- tests/target/issue-6832.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/target/issue-6832.rs b/tests/target/issue-6832.rs index cd7537aed4e..3acf67dc22c 100644 --- a/tests/target/issue-6832.rs +++ b/tests/target/issue-6832.rs @@ -4,4 +4,9 @@ /// ```rust /// let font = String::from("hello"); /// ``` -fn main() {} +fn ascii_string() {} + +/// ```rust +/// let font = String::from("hello 👋😁"); +/// ``` +fn unicode_chars() {} From e0c4eaf4b7805f1be527daa2fae1cb31df757209 Mon Sep 17 00:00:00 2001 From: Ly Date: Wed, 22 Apr 2026 22:23:12 +0800 Subject: [PATCH 6/7] chore: use flexible whitespace pattern + update comments Co-authored-by: Yacin Tmimi --- src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index aeb05584fff..a00fd3ffbb8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -387,8 +387,9 @@ fn format_code_block( let block_len = formatted .snippet .rfind('}') - // trim and trailing whitespace we added, e.g. if we format to fn main { some_func(); } - .map(|i| formatted.snippet[..i].strip_suffix(' ').map_or(i, str::len)) + // trim whitespace when using `fn_single_line=true` + // + .map(|i| formatted.snippet[..i].strip_suffix(char::is_whitespace).map_or(i, str::len)) .unwrap_or_else(|| formatted.snippet.len()); // It's possible that `block_len < FN_MAIN_PREFIX.len()`. This can happen if the code block was From 548b66c19ffa310dabb96f3b1732b68105a6d7cf Mon Sep 17 00:00:00 2001 From: Ly Date: Wed, 22 Apr 2026 22:26:10 +0800 Subject: [PATCH 7/7] style: re-format code to make test happy --- src/lib.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a00fd3ffbb8..2b80c437bba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -388,8 +388,11 @@ fn format_code_block( .snippet .rfind('}') // trim whitespace when using `fn_single_line=true` - // - .map(|i| formatted.snippet[..i].strip_suffix(char::is_whitespace).map_or(i, str::len)) + .map(|i| { + formatted.snippet[..i] + .strip_suffix(char::is_whitespace) + .map_or(i, str::len) + }) .unwrap_or_else(|| formatted.snippet.len()); // It's possible that `block_len < FN_MAIN_PREFIX.len()`. This can happen if the code block was