fix: handle inner ignore attribute on stdin

This commit is contained in:
Caleb Cartwright 2022-06-04 10:08:33 -05:00 committed by Caleb Cartwright
parent 79515f17ed
commit 64f1f57a48
2 changed files with 45 additions and 7 deletions

View File

@ -40,12 +40,10 @@ pub(crate) fn format_input_inner(
rustc_span::create_session_if_not_set_then(self.config.edition().into(), |_| { rustc_span::create_session_if_not_set_then(self.config.edition().into(), |_| {
if self.config.disable_all_formatting() { if self.config.disable_all_formatting() {
// When the input is from stdin, echo back the input. // When the input is from stdin, echo back the input.
if let Input::Text(ref buf) = input { return match input {
if let Err(e) = io::stdout().write_all(buf.as_bytes()) { Input::Text(ref buf) => echo_back_stdin(buf),
return Err(From::from(e)); _ => Ok(FormatReport::new()),
} };
}
return Ok(FormatReport::new());
} }
let config = &self.config.clone(); let config = &self.config.clone();
@ -94,6 +92,13 @@ fn should_skip_module<T: FormatHandler>(
false false
} }
fn echo_back_stdin(input: &str) -> Result<FormatReport, ErrorKind> {
if let Err(e) = io::stdout().write_all(input.as_bytes()) {
return Err(From::from(e));
}
Ok(FormatReport::new())
}
// Format an entire crate (or subset of the module tree). // Format an entire crate (or subset of the module tree).
fn format_project<T: FormatHandler>( fn format_project<T: FormatHandler>(
input: Input, input: Input,
@ -136,7 +141,8 @@ fn format_project<T: FormatHandler>(
.visit_crate(&krate)? .visit_crate(&krate)?
.into_iter() .into_iter()
.filter(|(path, module)| { .filter(|(path, module)| {
!should_skip_module(config, &context, input_is_stdin, &main_file, path, module) input_is_stdin
|| !should_skip_module(config, &context, input_is_stdin, &main_file, path, module)
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -146,6 +152,14 @@ fn format_project<T: FormatHandler>(
context.parse_session.set_silent_emitter(); context.parse_session.set_silent_emitter();
for (path, module) in files { for (path, module) in files {
if input_is_stdin && contains_skip(module.attrs()) {
return echo_back_stdin(
context
.parse_session
.snippet_provider(module.span)
.entire_snippet(),
);
}
should_emit_verbose(input_is_stdin, config, || println!("Formatting {}", path)); should_emit_verbose(input_is_stdin, config, || println!("Formatting {}", path));
context.format_file(path, &module, is_macro_def)?; context.format_file(path, &module, is_macro_def)?;
} }

View File

@ -578,6 +578,30 @@ fn stdin_generated_files_issue_5172() {
); );
} }
#[test]
fn stdin_handles_mod_inner_ignore_attr() {
// see https://github.com/rust-lang/rustfmt/issues/5368
init_log();
let input = String::from("#![rustfmt::skip]\n\nfn main() { }");
let mut child = Command::new(rustfmt().to_str().unwrap())
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.expect("failed to execute child");
{
let stdin = child.stdin.as_mut().expect("failed to get stdin");
stdin
.write_all(input.as_bytes())
.expect("failed to write stdin");
}
let output = child.wait_with_output().expect("failed to wait on child");
assert!(output.status.success());
assert!(output.stderr.is_empty());
assert_eq!(input, String::from_utf8(output.stdout).unwrap());
}
#[test] #[test]
fn format_lines_errors_are_reported() { fn format_lines_errors_are_reported() {
init_log(); init_log();