diff --git a/src/lib.rs b/src/lib.rs index d3379a4564b..2b80c437bba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -387,6 +387,12 @@ fn format_code_block( let block_len = formatted .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) + }) .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 new file mode 100644 index 00000000000..3acf67dc22c --- /dev/null +++ b/tests/target/issue-6832.rs @@ -0,0 +1,12 @@ +// rustfmt-format_code_in_doc_comments: true +// rustfmt-fn_single_line: true + +/// ```rust +/// let font = String::from("hello"); +/// ``` +fn ascii_string() {} + +/// ```rust +/// let font = String::from("hello 👋😁"); +/// ``` +fn unicode_chars() {}