rust-lang / rust-lang/rustfmt

`/// # #![rustfmt::skip]` behaves weird

Open
#6,098 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-comments A-rustfmt::skip C-bug only-with-option UO-format_code_in_doc_comments
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

I just wanted to keep specific formatting in my list. However it wasn't so easy...

skip visible in the HTML

Source code
//! ```rust
//! #![rustfmt::skip]
//! let _ = [
//!     1, 2, 3,
//!     4, 5,
//! ];
//! ```
Rendered doc
#![rustfmt::skip]
let _ = [
    1, 2, 3,
    4, 5,
];

This works, but I want to hide the formatting attribute

Hiding the skip

Before save:

//! ```rust
//! # #![rustfmt::skip]
//! let _ = [
//!     1, 2, 3,
//!     4, 5,
//! ];
//! ```

On save the list is formatted:

//! ```rust
//! # #![rustfmt::skip]
//! let _ = [1, 2, 3, 4, 5];
//! ```

Another way to hide skip with a block

Source code
//! ```rust
//! # #![rustfmt::skip] {
//! let _ = [
//!     1, 2, 3,
//!     4, 5
//! ];
//! }
//! ```

No difference on save, but its a new scope so now this variable is not avaliable to unformatted code.

Rendered HTML
let _ = [
    1, 2, 3,
    4, 5
];
}

Contains trailing }. If you hide the closing brace rustfmt will format the list.

Using nofmt::pls!{}

Source code
//! ```rust
//! let _ = nofmt::pls! {[
//!     1, 2, 3,
//!     4, 5
//! ]};
//! ```
Rendered doc
let _ = nofmt::pls! {[
    1, 2, 3,
    4, 5
]};

Hiding nofmt::pls!{}

Source code
//! ```rust
//! let _ =
//! # nofmt::pls! {
//! [
//!     1, 2, 3,
//!     4, 5
//! ];
//! # }
//! ```

No change on save.

Rendered doc
let _ =
[
    1, 2, 3,
    4, 5
];

But this just looks weird, I want to try to put the open bracket on the same line as =. I don't want the doc to look completely unformatted, just a little bit in a subtle way so that my list is more readable.

Making nofmt::pls!{} more hidden

Source code
//! ```rust
//! let _ = [
//! # nofmt::pls! {
//!     1, 2, 3,
//!     4, 5
//! # }
//! ];
//! ```

On save:

//! ```rust
//! let _ = [
//! # nofmt::pls! {
//!     1, 2, 3, 4, 5, //#### # }
//! ];
//! ```

Ouch... I think this is realted to #6025

Versions

rustfmt 1.7.0-nightly (3246e79 2024-02-19)
cargo 1.78.0-nightly (7b7af3077 2024-02-17

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the documented examples with the reported rustfmt nightly version and compare the source and rendered documentation cases. Trace how hidden attributes, blocks, and nofmt::pls! are processed; done means the intended formatting and hiding behavior is covered consistently by regression tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.