rust-lang / rust-lang/rustfmt

Off-by-one error wrapping comments on struct field

Open
#6,180 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-comments only-with-option UO-wrap_comments
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

% rustfmt --version
rustfmt 1.7.0-nightly (8679004 2024-05-23)

Content of src/lib.rs:

/*
0         1         2         3         4         5         6         7         8         9
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
*/

// This line has 100 characters ...................................................................9
pub struct Foo {
    // This line has 100 characters ...............................................................9
    pub foo: u8,
}

pub mod foo {
    // This line has 100 characters ...............................................................9
    pub fn foo() {}
}

Content of rustfmt.toml:

comment_width = 100
wrap_comments = true

Expected formatting is that nothing changes. The file is already correctly formatted.

Actual formatting is the following (the line on the foo field of the Foo struct is wrapped):

/*
0         1         2         3         4         5         6         7         8         9
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
*/

// This line has 100 characters ...................................................................9
pub struct Foo {
    // This line has 100 characters
    // ...............................................................9
    pub foo: u8,
}

pub mod foo {
    // This line has 100 characters ...............................................................9
    pub fn foo() {}
}

This is probably a minimum reproduction example of https://github.com/rust-lang/rustfmt/issues/4929, indicating that this is an old issue, and probably not a regression but an off-by-one since the introduction of this feature.

Also I observed that comment_width seems to be capped by max_width although this doesn't seem to be documented. Using comment_width = 200 produces the same off-by-one behavior.

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

Reproduce the issue with the provided src/lib.rs and rustfmt.toml using rustfmt 1.7.0-nightly, then inspect the comment-wrapping path responsible for struct fields. Done means the 100-character field comment remains unchanged, with coverage for the comment_width and max_width interaction.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.