Skip to content

Commit 34263cd

Browse files
jugglerchriscalebcartwright
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 93b7de5 commit 34263cd

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

Diff for: src/emitter/diff.rs

+2-3
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

Diff for: src/test/mod.rs

+26
Original file line numberDiff line numberDiff line change
@@ -989,3 +989,29 @@ fn verify_check_works_with_stdin() {
989989
.expect("Failed to wait on rustfmt child");
990990
assert!(output.status.success());
991991
}
992+
993+
#[test]
994+
fn verify_check_l_works_with_stdin() {
995+
init_log();
996+
997+
let mut child = Command::new(rustfmt().to_str().unwrap())
998+
.arg("--check")
999+
.arg("-l")
1000+
.stdin(Stdio::piped())
1001+
.stdout(Stdio::piped())
1002+
.stderr(Stdio::piped())
1003+
.spawn()
1004+
.expect("run with check option failed");
1005+
1006+
{
1007+
let stdin = child.stdin.as_mut().expect("Failed to open stdin");
1008+
stdin
1009+
.write_all("fn main()\n{}\n".as_bytes())
1010+
.expect("Failed to write to rustfmt --check");
1011+
}
1012+
let output = child
1013+
.wait_with_output()
1014+
.expect("Failed to wait on rustfmt child");
1015+
assert!(output.status.success());
1016+
assert_eq!(std::str::from_utf8(&output.stdout).unwrap(), "stdin\n");
1017+
}

0 commit comments

Comments
 (0)