Skip to content

Commit 9b454a5

Browse files
Merge remote-tracking branch 'upstream/master' into subtree-sync-2022-01-23
2 parents 7913f13 + 5056f4c commit 9b454a5

File tree

23 files changed

+334
-126
lines changed

23 files changed

+334
-126
lines changed

src/bin/main.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,10 @@ pub enum OperationError {
7474
/// An io error during reading or writing.
7575
#[error("{0}")]
7676
IoError(IoError),
77-
/// Attempt to use --check with stdin, which isn't currently
78-
/// supported.
79-
#[error("The `--check` option is not supported with standard input.")]
80-
CheckWithStdin,
81-
/// Attempt to use --emit=json with stdin, which isn't currently
82-
/// supported.
83-
#[error("Using `--emit` other than stdout is not supported with standard input.")]
84-
EmitWithStdin,
77+
/// Attempt to use --emit with a mode which is not currently
78+
/// supported with stdandard input.
79+
#[error("Emit mode {0} not supported with standard output.")]
80+
StdinBadEmit(EmitMode),
8581
}
8682

8783
impl From<IoError> for OperationError {
@@ -255,15 +251,20 @@ fn format_string(input: String, options: GetOptsOptions) -> Result<i32> {
255251
let (mut config, _) = load_config(Some(Path::new(".")), Some(options.clone()))?;
256252

257253
if options.check {
258-
return Err(OperationError::CheckWithStdin.into());
259-
}
260-
if let Some(emit_mode) = options.emit_mode {
261-
if emit_mode != EmitMode::Stdout {
262-
return Err(OperationError::EmitWithStdin.into());
254+
config.set().emit_mode(EmitMode::Diff);
255+
} else {
256+
match options.emit_mode {
257+
// Emit modes which work with standard input
258+
// None means default, which is Stdout.
259+
None | Some(EmitMode::Stdout) | Some(EmitMode::Checkstyle) | Some(EmitMode::Json) => {}
260+
Some(emit_mode) => {
261+
return Err(OperationError::StdinBadEmit(emit_mode).into());
262+
}
263263
}
264+
config
265+
.set()
266+
.emit_mode(options.emit_mode.unwrap_or(EmitMode::Stdout));
264267
}
265-
// emit mode is always Stdout for Stdin.
266-
config.set().emit_mode(EmitMode::Stdout);
267268
config.set().verbose(Verbosity::Quiet);
268269

269270
// parse file_lines

src/cargo-fmt/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,7 @@ fn get_targets_root_only(
387387
.unwrap_or_default()
388388
== current_dir_manifest
389389
})
390-
.map(|p| p.targets)
391-
.flatten()
390+
.flat_map(|p| p.targets)
392391
.collect(),
393392
};
394393

src/config/file_lines.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl fmt::Display for FileName {
3939
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4040
match self {
4141
FileName::Real(p) => write!(f, "{}", p.to_str().unwrap()),
42-
FileName::Stdin => write!(f, "stdin"),
42+
FileName::Stdin => write!(f, "<stdin>"),
4343
}
4444
}
4545
}

src/emitter/checkstyle.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use self::xml::XmlEscaped;
22
use super::*;
33
use crate::rustfmt_diff::{make_diff, DiffLine, Mismatch};
44
use std::io::{self, Write};
5-
use std::path::Path;
65

76
mod xml;
87

@@ -30,7 +29,6 @@ impl Emitter for CheckstyleEmitter {
3029
}: FormattedFile<'_>,
3130
) -> Result<EmitterResult, io::Error> {
3231
const CONTEXT_SIZE: usize = 0;
33-
let filename = ensure_real_path(filename);
3432
let diff = make_diff(original_text, formatted_text, CONTEXT_SIZE);
3533
output_checkstyle_file(output, filename, diff)?;
3634
Ok(EmitterResult::default())
@@ -39,13 +37,13 @@ impl Emitter for CheckstyleEmitter {
3937

4038
pub(crate) fn output_checkstyle_file<T>(
4139
mut writer: T,
42-
filename: &Path,
40+
filename: &FileName,
4341
diff: Vec<Mismatch>,
4442
) -> Result<(), io::Error>
4543
where
4644
T: Write,
4745
{
48-
write!(writer, r#"<file name="{}">"#, filename.display())?;
46+
write!(writer, r#"<file name="{}">"#, filename)?;
4947
for mismatch in diff {
5048
let begin_line = mismatch.line_number;
5149
let mut current_line;
@@ -77,7 +75,11 @@ mod tests {
7775
fn emits_empty_record_on_file_with_no_mismatches() {
7876
let file_name = "src/well_formatted.rs";
7977
let mut writer = Vec::new();
80-
let _ = output_checkstyle_file(&mut writer, &PathBuf::from(file_name), vec![]);
78+
let _ = output_checkstyle_file(
79+
&mut writer,
80+
&FileName::Real(PathBuf::from(file_name)),
81+
vec![],
82+
);
8183
assert_eq!(
8284
&writer[..],
8385
format!(r#"<file name="{}"></file>"#, file_name).as_bytes()

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

0 commit comments

Comments
 (0)