Skip to content

Commit fb5fea6

Browse files
jugglerchristopecongiro
authored andcommitted
Fix --check -l with stdin. (#3910)
* Fix some possible panics when using `--check` with stdin. One case which doesn't work is when there are only line ending fixes; with stdin rustfmt is unable to detect the difference as it stores the input with Unix line endings. * Add test for `rustfmt --check -l` with stdin.
1 parent c1ada05 commit fb5fea6

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/emitter/diff.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl Emitter for DiffEmitter {
2828

2929
if has_diff {
3030
if self.config.print_misformatted_file_names() {
31-
writeln!(output, "{}", ensure_real_path(filename).display())?;
31+
writeln!(output, "{}", filename)?;
3232
} else {
3333
print_diff(
3434
mismatch,
@@ -40,8 +40,7 @@ impl Emitter for DiffEmitter {
4040
// This occurs when the only difference between the original and formatted values
4141
// is the newline style. This happens because The make_diff function compares the
4242
// original and formatted values line by line, independent of line endings.
43-
let file_path = ensure_real_path(filename);
44-
writeln!(output, "Incorrect newline style in {}", file_path.display())?;
43+
writeln!(output, "Incorrect newline style in {}", filename)?;
4544
return Ok(EmitterResult { has_diff: true });
4645
}
4746

src/test/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,3 +958,29 @@ fn verify_check_works_with_stdin() {
958958
.expect("Failed to wait on rustfmt child");
959959
assert!(output.status.success());
960960
}
961+
962+
#[test]
963+
fn verify_check_l_works_with_stdin() {
964+
init_log();
965+
966+
let mut child = Command::new(rustfmt().to_str().unwrap())
967+
.arg("--check")
968+
.arg("-l")
969+
.stdin(Stdio::piped())
970+
.stdout(Stdio::piped())
971+
.stderr(Stdio::piped())
972+
.spawn()
973+
.expect("run with check option failed");
974+
975+
{
976+
let stdin = child.stdin.as_mut().expect("Failed to open stdin");
977+
stdin
978+
.write_all("fn main()\n{}\n".as_bytes())
979+
.expect("Failed to write to rustfmt --check");
980+
}
981+
let output = child
982+
.wait_with_output()
983+
.expect("Failed to wait on rustfmt child");
984+
assert!(output.status.success());
985+
assert_eq!(std::str::from_utf8(&output.stdout).unwrap(), "stdin\n");
986+
}

0 commit comments

Comments
 (0)