Strange bug with `cargo fmt`

Hi! I've been having some strange issues with rust's formatter recently, specifically in one file. When I add an extra line, I get an error "left behind trailing whitespace." However, it still won't format the code when I remove that line. When I remove lines 27-173 it starts formatting correctly. The code compiles just fine as well.

I suspect it would be helpful to post the exact error message.

The strangest thing is there aren't any messages, that's what really got my head scratching.

On further investigation, I've also reproduced it by copying the code in the file into a new rust project, no fmt settings. When removing those lines, it formats the code.

To reproduce:

cargo new bugtest
cd bugtest

Replace contents of main.rs with contents of m_0000_add_polyphonic_prop.rs:

cargo fmt --all

No errors, no formatting.

Remove lines 26-181. Now it formats.

Oops, I should also post system info:

cargo fmt --version
# rustfmt 1.5.1-stable
uname -r
# 6.0.9-arch1-1

I've been able to reduce it to this, I'll keep hacking away to see if I can get a smaller example though:

pub fn main() {
    let foo: Vec<()> = vec![];

    foo
        .iter()
        .for_each(|_| {
                            let x = "a really really really really really really really really really long line of text";
        });
}

It will reformat correctly inside of a normal block, but when it's in a closure block it stops formatting.

Never mind, looks like it's an open issue: Gives up on chains if any line is too long. · Issue #3863 · rust-lang/rustfmt · GitHub

2 Likes