rust-lang / rust-lang/rustfmt

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

Open
#5,364 12 comments 1 reaction 1 assignee View on GitHub

@carsonRadtke is already working on this.

Since Jun 23, 2022.

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

Description

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)

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.