rust-lang / rust-lang/rustfmt

Off-by-two error wrapping line with use_small_heuristics Max

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

Nobody has claimed this yet.

C-bug SO-use_small_heuristics
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

% rustfmt --version
rustfmt 1.7.1-nightly (fda509e 2024-06-25)

Content of src/main.rs:

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

fn main() {
    // This line has 100 characters ...............................................................9
    let connection = candidate.connect(Duration::from_secs(1)).context("connecting to the device")?;
    // This line has 99 characters ...............................................................8
    let connection = candidate.connect(Duration::from_secs(1)).context("connecting to the devic")?;
    // This line has 98 characters ..............................................................7
    let connection = candidate.connect(Duration::from_secs(1)).context("connecting to the devi")?;
}

Content of rustfmt.toml:

comment_width = 100
use_small_heuristics = "Max"

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

Actual formatting is the following (lines with 100 and 99 characters are wrapped):

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

fn main() {
    // This line has 100 characters ...............................................................9
    let connection =
        candidate.connect(Duration::from_secs(1)).context("connecting to the device")?;
    // This line has 99 characters ...............................................................8
    let connection =
        candidate.connect(Duration::from_secs(1)).context("connecting to the devic")?;
    // This line has 98 characters ..............................................................7
    let connection = candidate.connect(Duration::from_secs(1)).context("connecting to the devi")?;
}

Maybe it's related to #6180? Or maybe it's how use_small_heuristics = Max is supposed to work since the documentation doesn't say anything.

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 with the reproducer in src/main.rs and rustfmt.toml, then run rustfmt 1.7.1-nightly using use_small_heuristics = "Max" and comment_width = 100. Compare the wrapping of the 100-, 99-, and 98-character lines with the expected output, and verify that the off-by-two behavior is corrected without changing the 98-character case.

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.