Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix handling of inner ignore attributes on stdin #5369

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ impl<'b, T: Write + 'b> Session<'b, T> {
rustc_span::create_session_if_not_set_then(self.config.edition().into(), |_| {
if self.config.disable_all_formatting() {
// When the input is from stdin, echo back the input.
if let Input::Text(ref buf) = input {
if let Err(e) = io::stdout().write_all(buf.as_bytes()) {
return Err(From::from(e));
}
}
return Ok(FormatReport::new());
return match input {
Input::Text(ref buf) => echo_back_stdin(buf),
_ => Ok(FormatReport::new()),
};
}

let config = &self.config.clone();
Expand Down Expand Up @@ -94,6 +92,13 @@ fn should_skip_module<T: FormatHandler>(
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).
fn format_project<T: FormatHandler>(
input: Input,
Expand Down Expand Up @@ -136,7 +141,8 @@ fn format_project<T: FormatHandler>(
.visit_crate(&krate)?
.into_iter()
.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<_>>();

Expand All @@ -146,6 +152,14 @@ fn format_project<T: FormatHandler>(
context.parse_session.set_silent_emitter();

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));
context.format_file(path, &module, is_macro_def)?;
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
fn format_lines_errors_are_reported() {
init_log();
Expand Down