rust-lang / rust-lang/rustfmt

OR pattern in `match` arm makes `rustfmt` forget comment alignment

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

Nobody has claimed this yet.

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

Description

When reformatting the following example:

let error_code = match err {
	msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte
	msgs::DecodeError::UnknownRequiredFeature|
	msgs::DecodeError::InvalidValue|
	msgs::DecodeError::ShortRead => 0x4000 | 22, // invalid_onion_payload
	_ => 0x2000 | 2, // Should never happen
};

we receive this result:

let error_code = match err {
	msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte
	msgs::DecodeError::UnknownRequiredFeature
	| msgs::DecodeError::InvalidValue
	| msgs::DecodeError::ShortRead => 0x4000 | 22, // invalid_onion_payload
	_ => 0x2000 | 2,                                 // Should never happen
};

Note that the comment at the end of the second match arm is unaligned with the others. For the third match arm rustfmt even introduces many whitespaces to ensure it's aligned with the in the first match arm. However, it seems that rustfmt entirely forgets about this alignment in the second match arm, presumably due to it being an OR pattern

This seems to happen independently from any particular config option or rustfmt version, but currently I'm using

> rustfmt --version
rustfmt 1.7.0-stable (82e1608d 2023-12-21)

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 supplied match-arm example with rustfmt 1.7.0, then inspect rustfmt's match-pattern formatting logic and existing tests for OR patterns and trailing comment alignment. Done means the second arm's comment aligns consistently with the other arm comments and a regression test covers this example.

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.