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

rustfmt sometimes refuses to format_code_in_doc_comments #6014

Closed
stepancheg opened this issue Jan 10, 2024 · 2 comments
Closed

rustfmt sometimes refuses to format_code_in_doc_comments #6014

stepancheg opened this issue Jan 10, 2024 · 2 comments
Labels
a-comments only-with-option requires a non-default option value to reproduce p-low

Comments

@stepancheg
Copy link
Contributor

The minimal example I could find:

/// ```
/// use buck::types::TargetLabel;
///
///
/// use buck::types::TargetPattern;
///
///
///
///
///
///
/// assert!(!TargetPattern::new("foo//bar/...").is_specific_target());
/// ```
fn is_specific_target() {
}

/// ```
/// use buck::types::TargetLabel;
///
///
/// use buck::types::TargetPattern;
///
///
///
///
///
///
///
/// assert!(!
///     TargetPattern::new("foo//bar/...").matches(&TargetLabel::new("foo//moo/bar/baz:qux")),
/// );
/// ```
fn matches() {
}
rustfmt +nightly --config format_code_in_doc_comments=true

Outputs:

/// ```
/// use buck::types::TargetLabel;
///
/// use buck::types::TargetPattern;
///
/// assert!(!TargetPattern::new("foo//bar/...").is_specific_target());
/// ```
fn is_specific_target() {}

/// ```
/// use buck::types::TargetLabel;
///
///
/// use buck::types::TargetPattern;
///
///
///
///
///
///
///
/// assert!(!
///     TargetPattern::new("foo//bar/...").matches(&TargetLabel::new("foo//moo/bar/baz:qux")),
/// );
/// ```
fn matches() {}

So, first comment is reformatted, and the second is completely skipped. Something in that assert! prevents formatting.

rustfmt +nightly --version
rustfmt 1.7.0-nightly (ca663b06 2024-01-08)
@ytmimi
Copy link
Contributor

ytmimi commented Jan 10, 2024

This seems related to #5531. Specifically this makes me think of #5531 (comment).

For now, linking the tracking issue for format_code_in_doc_comments #3348

@ytmimi ytmimi added a-comments only-with-option requires a non-default option value to reproduce p-low labels Jan 10, 2024
@ytmimi
Copy link
Contributor

ytmimi commented Jan 10, 2024

I suspect this has the same underlying issue as #5531.

Taking a look at #5531 (comment). I've explained that rustfmt isn't taking the doc comment opener into account. Furthermore, when rewriting macros rustfmt doesn't remove trailing commas.

rustfmt would love to produce this output, but it's 101 characters long

/// assert!(!TargetPattern::new("foo//bar/...").matches(&TargetLabel::new("foo//moo/bar/baz:qux")),);

and because it exceeds the max_width rustfmt can't reformat the code block. If you remove the trailing comma:

/// ```
/// use buck::types::TargetLabel;
///
///
/// use buck::types::TargetPattern;
///
///
///
///
///
///
///
/// assert!(!
///     TargetPattern::new("foo//bar/...").matches(&TargetLabel::new("foo//moo/bar/baz:qux")) 
/// );
/// ```
fn matches() {}

You'll get:

/// ```
/// use buck::types::TargetLabel;
///
/// use buck::types::TargetPattern;
///
/// assert!(!TargetPattern::new("foo//bar/...").matches(&TargetLabel::new("foo//moo/bar/baz:qux")));
/// ```
fn matches() {}

Additionally, I got code formatting to work when I set doc_comment_code_block_width=99, which further leads me to believe that this is a width related issue similar to #5531.

@ytmimi ytmimi closed this as not planned Won't fix, can't repro, duplicate, stale Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-comments only-with-option requires a non-default option value to reproduce p-low
Projects
None yet
Development

No branches or pull requests

2 participants