Skip to content

Commit 5493fa1

Browse files
jugglerchriskaryon
authored andcommitted
Fix --check -l with stdin. (rust-lang#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 80287e1 commit 5493fa1

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
@@ -992,3 +992,29 @@ fn verify_check_works_with_stdin() {
992992
.expect("Failed to wait on rustfmt child");
993993
assert!(output.status.success());
994994
}
995+
996+
#[test]
997+
fn verify_check_l_works_with_stdin() {
998+
init_log();
999+
1000+
let mut child = Command::new(rustfmt().to_str().unwrap())
1001+
.arg("--check")
1002+
.arg("-l")
1003+
.stdin(Stdio::piped())
1004+
.stdout(Stdio::piped())
1005+
.stderr(Stdio::piped())
1006+
.spawn()
1007+
.expect("run with check option failed");
1008+
1009+
{
1010+
let stdin = child.stdin.as_mut().expect("Failed to open stdin");
1011+
stdin
1012+
.write_all("fn main()\n{}\n".as_bytes())
1013+
.expect("Failed to write to rustfmt --check");
1014+
}
1015+
let output = child
1016+
.wait_with_output()
1017+
.expect("Failed to wait on rustfmt child");
1018+
assert!(output.status.success());
1019+
assert_eq!(std::str::from_utf8(&output.stdout).unwrap(), "stdin\n");
1020+
}

0 commit comments

Comments
 (0)