Skip to content

fix: avoid leaking wrapper space from doc comment code block formatting#6851

Open
utakotoba wants to merge 4 commits intorust-lang:mainfrom
utakotoba:main
Open

fix: avoid leaking wrapper space from doc comment code block formatting#6851
utakotoba wants to merge 4 commits intorust-lang:mainfrom
utakotoba:main

Conversation

@utakotoba
Copy link
Copy Markdown

fixes #6832

Summary

Fixes a bug in doc comment code block formatting where rustfmt can introduce a trailing space and then report left behind trailing whitespace (if error_on_unformatted = true).

This happens with the following configuration:

format_code_in_doc_comments = true
fn_single_line = true

Fix

Handle the single-line wrapped case when extracting the formatted code block body so wrapper-added whitespace is not preserved in the returned snippet.

The current fix is narrowly scoped to catch only single-line functions in doc comments. A simpler solution would be to wrap the final result with remove_trailing_white_spaces(), but I don’t think it’s ideal since it would somehow impact more cases.

@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Apr 1, 2026
Comment thread src/lib.rs Outdated
Comment thread tests/target/issue-6832.rs Outdated
Comment thread src/lib.rs
@afurm
Copy link
Copy Markdown

afurm commented Apr 13, 2026

The fix handles the single-line wrapped case narrowly. Is there a scenario where format_code_block is called with a multi-line function inside a doc comment that also has fn_single_line = true? Would the same wrapper-space leak affect that case?

@utakotoba
Copy link
Copy Markdown
Author

The fix handles the single-line wrapped case narrowly. Is there a scenario where format_code_block is called with a multi-line function inside a doc comment that also has fn_single_line = true? Would the same wrapper-space leak affect that case?

Yes. format_code_block() is used for any Rust fenced code block in a doc comment, not just already-single-line snippets.

The leak depends on the final wrapped formatted shape, not the original input shape. So if a multi-line function in a doc comment is reformatted such that the dummy wrapper collapses to fn main() { ... }, the same wrapper-space leak can occur.

The current fix is applied at extraction time, so it also covers that case. If the wrapped output stays multi-line, strip_suffix(' ') is a no-op.

Co-authored-by: Matthew Hughes <matthewhughes934@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Trailing space on doc comment

4 participants