rust-lang / rust-lang/rustfmt

Change rewrite return type from Option to Return

Open
#3,420 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

In issues #3206 #3416 #3135, wrap_str is preventing some possibly good formatting because of long strings. The return type being an Option, the fact that the rewrite failed because of a long string is lost.
If that were returning a Return, then the failure reason could be passed up for the callee to handle as appropriate.

For example, it seems the rewrite in #3416 could allow the line to overflow (which anyway would be caught by the LineOverflow logic). However, there are cases where a better formatting is possible, e.g.:

// original
fn foo() {
    let suuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuum = xxxxxxx
        .map(|x| x + 5)
        .map(|x| x / 2)
        .fold(0, |acc, x| acc + x);
}

// formatted
fn foo() {
     let suuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuum =
         xxxxxxx.map(|x| x + 5)
                .map(|x| x / 2)
                .fold(0, |acc, x| acc + x);
}

Although in that example there is no long string, the current wrap_str logic makes that formatting possible.

With a Return as the return type, rewrite methods could be made more flexible to various kinds of errors.
However, that change would require some heavy refactoring I think.

What do you think about such a change ?

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 tracing wrap_str and the rewrite methods that currently return Option, then inspect the LineOverflow logic in formatting.rs. Determine how a Return type could propagate long-string failure reasons to callers without losing existing formatting behavior. Done means the refactor supports caller-specific handling of rewrite failures and preserves appropriate overflow handling.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.