rust-lang / rust-lang/rustfmt

Off by one: return type on associated function/method wraps at exactly `max_width`

Open
#6,539 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

good first issue
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

The problem occurs in the following code, note that the longest line is exactly 100 columns:

pub trait Manager {
    fn attach_device(&self, seat_id: &str, sysfs_path: &str, interactive: bool) -> zbus::Result<()>;
}

If this is formatted with the 2021 edition, you get the following:

pub trait Manager {
    fn attach_device(&self, seat_id: &str, sysfs_path: &str, interactive: bool)
        -> zbus::Result<()>;
}

This isn't technically correct, since it shouldn't be broken at all, but at least the arrow is indented properly to make the association to the function clear.

With the 2024 edition, it formats like this:

pub trait Manager {
    fn attach_device(&self, seat_id: &str, sysfs_path: &str, interactive: bool)
    -> zbus::Result<()>;
}

So still breaks the line that doesn't need to be broken, but even worse than 2021 edition.

If the line is a single character longer, it's not broken at all:

pub trait Manager {
    fn attach_device(&self, seat_id: &str, sysfs_path: &str, interactiv: bool) -> zbus::Result<()>;
}

If it is one character longer, the arguments are broken vertically:

pub trait Manager {
    fn attach_device(
        &self,
        seat_id: &str,
        sysfs_path: &str,
        interactivee: bool,
    ) -> zbus::Result<()>;
}

Surely this can't be intentional? It seems like some sort of off-by-one error to me. I'd honestly be happier with having it broken apart vertically than that ugly 2024 formatting, even though it shouldn't require formatting at all.

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. Reproduce the supplied Rust examples at max_width 100 in the 2021 and 2024 editions, then trace the return-type break decision; done when the boundary no longer produces the incorrect 2024 indentation and regression coverage captures the examples.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.