Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to skip formatting in doc comments #5623

Open
AaronKutch opened this issue Dec 1, 2022 · 2 comments · May be fixed by #5636
Open

Trying to skip formatting in doc comments #5623

AaronKutch opened this issue Dec 1, 2022 · 2 comments · May be fixed by #5636
Labels
a-comments e-trailing whitespace error[internal]: left behind trailing whitespace only-with-option requires a non-default option value to reproduce

Comments

@AaronKutch
Copy link

AaronKutch commented Dec 1, 2022

In a recent nightly, rustfmt with format_code_in_doc_comments = true started working and fixed some stuff. However, there was a place where I wanted to skip the formatting. This code:

/// ```
/// #![rustfmt::skip]
/// use core::primitive;
///
/// let y = 0;
/// ```
fn main() {}

will result in

error[internal]: left behind trailing whitespace
  --> \\?\C:\Users\aaron\Documents\github\testbin\src\main.rs:11:11:4
   |
11 | ///
   |    ^^^^^

I presume the above behavior is a bug.
Independently, is there a way of skipping doc formatting without #![rustfmt::skip] appearing? I feel like I've seen some tag you can attach to the triple backticks, but it is not documented prominently.

@ytmimi
Copy link
Contributor

ytmimi commented Dec 2, 2022

Thanks for reaching out!

You're right, it would definitely be useful to document under what conditions we'll actually run rustfmt on your doc comments. We should probably update the docs for format_code_in_doc_comments to include that information.

That being said, before running rustfmt on the code in your doc comments we first check to see if there are any attributes that would indicate that it's not rust code. We support the rustdoc attributes, and we'll only run rustfmt on the code block if it's annotated with one of "rust", "should_panic", "no_run", "edition2015", "edition2018", "edition2021", or if there are no attributes since we assume that the code block contains rust code in that case.

We'll ignore the code block if it contains any other kind of attribute. For example, "ignore", "compile_fail", or "text".

For some immediate relief to your issue you could write something like this:

/// ```ignore
/// use core::primitive;
///
/// let y = 0;
/// ```
fn main() {}

or even something like this:

/// ```some_random_attribute_so_rustfmt_ignores_the_code
/// use core::primitive;
///
/// let y = 0;
/// ```
fn main() {}

@ytmimi ytmimi added a-comments e-trailing whitespace error[internal]: left behind trailing whitespace only-with-option requires a non-default option value to reproduce labels Dec 2, 2022
@ytmimi
Copy link
Contributor

ytmimi commented Dec 2, 2022

There's definitely an issue when calling crate::format_code_block, as it's adding extra whitespace:

rustfmt/src/comment.rs

Lines 737 to 743 in ee2bed9

if let Some(s) =
crate::format_code_block(&self.code_block_buffer, &config, false)
{
trim_custom_comment_prefix(&s.snippet)
} else {
trim_custom_comment_prefix(&self.code_block_buffer)
}

linking tracking issue for format_code_in_doc_comments #3348

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-comments e-trailing whitespace error[internal]: left behind trailing whitespace only-with-option requires a non-default option value to reproduce
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants