rust-lang / rust-lang/rustfmt

Comment indentation/alignment changed in Rust 1.81

Open
#6,351 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

When I updated to Rust 1.81 I noticed that the comment indentation changed. Not sure if this is an intentional breakage or a bug.

I have the following code (formatted with Rust 1.80.1, rustfmt 1.7.0):

enum Enum12 {
	Fn,
	NotEquals,
	Backslash,
}

fn parse_symbol2(ch: char) -> Enum12 {
	match ch {
		'=' => Enum12::Fn,
		'\u{2260}' => Enum12::NotEquals,       // unicode not equal to symbol
		'\\' | '\u{3bb}' => Enum12::Backslash, // lambda symbol
		_ => todo!(),
	}
}

.rustfmt.toml:

hard_tabs = true

When I updated to Rust 1.81 (rustfmt 1.7.1) I got this diff:

diff --git a/src/lib.rs b/src/lib.rs
index 634b281..edc0fca 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -7,7 +7,7 @@ enum Enum12 {
 fn parse_symbol2(ch: char) -> Enum12 {
        match ch {
                '=' => Enum12::Fn,
-               '\u{2260}' => Enum12::NotEquals,       // unicode not equal to symbol
+               '\u{2260}' => Enum12::NotEquals, // unicode not equal to symbol
                '\\' | '\u{3bb}' => Enum12::Backslash, // lambda symbol
                _ => todo!(),
        }

Strangely the bug seems to be very dependent on the length of some of my identifiers. If I change the enum name to Enum or E the formatting difference goes away. Are comments like this meant to be aligned or not?

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 formatting difference using the provided src/lib.rs example and .rustfmt.toml with Rust 1.80.1 and Rust 1.81. Compare how the inline comments are aligned for the different identifier lengths. Done means the intended alignment behavior is established and the regression is covered by a project test.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.