Rustfmt: Needs two passes to correctly format.

Created on 17 Oct 2019  路  2Comments  路  Source: rust-lang/rustfmt

I'm not sure whether this is expected behaviour or not, but the following code:

fn main() {
    for _ in 0..1 {
        ;
    }
}

Is first reformatted as

fn main() {
    for _ in 0..1 {
    }
}

And only then as

fn main() {
    for _ in 0..1 {}
}

If I need to run rustfmt twice in this case, how am I sure that I have run it enough times in other cases? Do I need to repeat every rustfmt until the file is stable?

bug

Most helpful comment

Thanks @iago-lito - that's a bug (at least IMO)

All 2 comments

Thanks @iago-lito - that's a bug (at least IMO)

Here the single ; is treated as an empty statement, and in the first run, rustfmt misunderstands that the for block is not empty. In the second run, since the single ; gets removed and the block is empty, rustfmt squashes the block into a single line.

To fix this, we should filter out the empty statements from the block when checking its emptiness.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thomaseizinger picture thomaseizinger  路  3Comments

gnzlbg picture gnzlbg  路  3Comments

gnzlbg picture gnzlbg  路  3Comments

fulara picture fulara  路  4Comments

MoSal picture MoSal  路  5Comments