Skip to content

rustfmt fails on an empty file; --check's diff doesn't result in properly formatted file #5364

Open
@roy-work

Description

@roy-work

Generate an empty file, and run rustfmt --check on it:

$ touch zero_lines.rs
$ rustfmt --check zero_lines.rs
Diff in /Users/roy/code/random/test-crate/src/zero_lines.rs at line 1:
+
+
$

So, this is interesting; to me, an empty file is inherently correctly formatted (what else would you put in the file?). But the diff emitted by rustfmt here indicates that, instead of just an empty file, that it would like a file with two blank lines. But if you give it what it wants:

$ printf '\n\n' > two_lines.rs
$ rustfmt --check two_lines.rs
Diff in /Users/roy/code/random/test-crate/src/two_lines.rs at line 1:

-

$

… the file is still not valid, and … the diff is nonsensical. This diff indicates a 3 line file, where the second line is removed, which doesn't match our input.

What rustfmt --check seems to want, is:

$ printf '\n' > one_line.rs
$ rustfmt --check one_line.rs
$

A file with one line in it. Which is fairly arbitrary (why have that blank line?).

This is in a crate whose lib.rs is empty, as it's just a placeholder for bin/a.rs, bin/b.rs, etc. stuff.

In practice, I'm going to slap a docstring into this particular lib.rs to hack around this.

rustfmt 1.4.37-stable (59eed8a2 2021-11-01)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions