rust-lang / rust-lang/rustfmt

Trailing whitespace followed by long unbreakable line causes error[internal]: left behind trailing whitespace

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

Nobody has claimed this yet.

C-feature-request I-trailing-whitespace P-low
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Input code
fn f() {
    match x {
        0 => S {
            
        },
        _ => AAAAAAAAA,
    }
}

(Note, the 4th line contains 12 spaces, which you can see if you select the text.)

Actual result
$ rustfmt file.rs --config max_width=20
error[internal]: left behind trailing whitespace
 --> /usr/local/google/home/mkember/a.rs:4:4:1
  |
4 |
  | ^^^^^^^^^^^^
  |

warning: rustfmt has failed to format. See previous 1 errors. 
$ echo $?
1
Expected result

Here are some possibilities, from best to worst (IMO):

  1. Successfully delete the trailing whitespace. I'm not sure why the long line that follows prevents it from doing so.
  2. Print a warning that it wasn't able to remove trailing whitespace, but still succeed (exit status 0).
  3. Provide an option that makes such errors non-fatal (I though error_on_unformatted=false would, but it does not).

If you're running rustfmt manually on your own code this isn't such a big deal. But in my case I am running it automatically on Rust bindings code generated by a program. I want to fail the whole program is rustfmt fails on a real problem, like invalid Rust syntax. I don't want it to fail just because it wasn't able to format optimally. One solution (at least for this example) is to make max_width very large, but that makes everything else less readable. The whole point of running rustfmt is so I can generate ugly code without worrying about formatting.

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 issue using the Rust snippet in file.rs and run rustfmt with max_width=20. Trace the trailing-whitespace handling that produces the internal error, then verify that this input removes the whitespace or otherwise completes without an inappropriate formatting failure.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.