rust-lang / rust-lang/rustfmt

Undesired reformatting of comments applying to array elements.

Open
#6,497 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-comments S-duplicate
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

REPRO:

$ cat fmt-comment-trouble.rs
const RLIB_CRATE_TYPES: [&str; 2] = [
    // https://doc.rust-lang.org/nightly/reference/linkage.html#r-link.lib
    // says:
    //
    //     > The purpose of this generic lib option is to generate
    //     > the “compiler recommended” style of library.
    //
    // For us this means `Rlib`.
    "lib",
    // Naturally "rlib" maps to `LibType::Rlib`.
    "rlib",
];


lukasza2 in /usr/local/google/home/lukasza/scratch
$ rustfmt --version
rustfmt 1.8.0-nightly (3ea711f17e 2025-03-09)

lukasza2 in /usr/local/google/home/lukasza/scratch
$ rustfmt fmt-comment-trouble.rs
Warning: the `fn_args_layout` option is deprecated. Use `fn_params_layout`. instead
Warning: the `version` option is deprecated. Use `style_edition` instead.

lukasza2 in /usr/local/google/home/lukasza/scratch
$ cat fmt-comment-trouble.rs
const RLIB_CRATE_TYPES: [&str; 2] = [
    // https://doc.rust-lang.org/nightly/reference/linkage.html#r-link.lib
    // says:
    //
    //     > The purpose of this generic lib option is to generate
    //     > the “compiler recommended” style of library.
    //
    // For us this means `Rlib`.
    "lib", // Naturally "rlib" maps to `LibType::Rlib`.
    "rlib",
];

EXPECTED BEHAVIOR: // Naturally "rlib" maps to LibType::Rlib. stays below "lib" and above "rlibs".

ACTUAL BEHAVIOR: The comment is no longer on a separate line - the comment got transformed into a trailing comment that seems to apply to "lib" rather than to "rlib".

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 running rustfmt on fmt-comment-trouble.rs and compare the documented expected and actual output. Trace the comment handling for array elements from the rustfmt entry point; done means the comment remains on its own line between "lib" and "rlib" rather than becoming a trailing comment.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.