rust-lang / rust-lang/rustfmt

Comments in match partially aligned when using `hard_tabs`

Open
#6,796 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

When using the hard_tabs option the last two comments are aligned, but none of the previous ones:

fn year_path(year: u16) -> Option<&'static str> {
	Some(match year {
		2020.. => "Current", // https://www.indianamap.org/datasets/INMap::indiana-current-imagery/about
		2016.. => "2016_2019", // https://www.indianamap.org/datasets/INMap::indiana-2016-2019-imagery/about
		2011.. => "2011_2013", // https://www.indianamap.org/datasets/INMap::indiana-imagery-2011-2013/about
		2005.. => "2005",      // https://www.indianamap.org/datasets/INMap::indiana-2005-imagery/about
		_ => return None,
	})
}

Adding a character in "2011_2013" causes the comment in the line below to also move one space to the right. Adding two more characters to the string however causes this "bond" to break.

Disabling the hard_tabs option causes the formatting to be more consistent again:

fn year_path(year: u16) -> Option<&'static str> {
    Some(match year {
        2020.. => "Current", // https://www.indianamap.org/datasets/INMap::indiana-current-imagery/about
        2016.. => "2016_2019", // https://www.indianamap.org/datasets/INMap::indiana-2016-2019-imagery/about
        2011.. => "2011_2013", // https://www.indianamap.org/datasets/INMap::indiana-imagery-2011-2013/about
        2005.. => "2005", // https://www.indianamap.org/datasets/INMap::indiana-2005-imagery/about
        _ => return None,
    })
}

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

No source file or test is named. Start by reproducing the provided Rust snippet with the hard_tabs option, then trace rustfmt's comment-alignment behavior for match arms. Done means comment alignment remains consistent when string lengths change, with a regression test covering the example.

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
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.