Skip to content

Commit 64f1f57

Browse files
fix: handle inner ignore attribute on stdin
1 parent 79515f1 commit 64f1f57

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

Diff for: src/formatting.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,10 @@ impl<'b, T: Write + 'b> Session<'b, T> {
4040
rustc_span::create_session_if_not_set_then(self.config.edition().into(), |_| {
4141
if self.config.disable_all_formatting() {
4242
// When the input is from stdin, echo back the input.
43-
if let Input::Text(ref buf) = input {
44-
if let Err(e) = io::stdout().write_all(buf.as_bytes()) {
45-
return Err(From::from(e));
46-
}
47-
}
48-
return Ok(FormatReport::new());
43+
return match input {
44+
Input::Text(ref buf) => echo_back_stdin(buf),
45+
_ => Ok(FormatReport::new()),
46+
};
4947
}
5048

5149
let config = &self.config.clone();
@@ -94,6 +92,13 @@ fn should_skip_module<T: FormatHandler>(
9492
false
9593
}
9694

95+
fn echo_back_stdin(input: &str) -> Result<FormatReport, ErrorKind> {
96+
if let Err(e) = io::stdout().write_all(input.as_bytes()) {
97+
return Err(From::from(e));
98+
}
99+
Ok(FormatReport::new())
100+
}
101+
97102
// Format an entire crate (or subset of the module tree).
98103
fn format_project<T: FormatHandler>(
99104
input: Input,
@@ -136,7 +141,8 @@ fn format_project<T: FormatHandler>(
136141
.visit_crate(&krate)?
137142
.into_iter()
138143
.filter(|(path, module)| {
139-
!should_skip_module(config, &context, input_is_stdin, &main_file, path, module)
144+
input_is_stdin
145+
|| !should_skip_module(config, &context, input_is_stdin, &main_file, path, module)
140146
})
141147
.collect::<Vec<_>>();
142148

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

148154
for (path, module) in files {
155+
if input_is_stdin && contains_skip(module.attrs()) {
156+
return echo_back_stdin(
157+
context
158+
.parse_session
159+
.snippet_provider(module.span)
160+
.entire_snippet(),
161+
);
162+
}
149163
should_emit_verbose(input_is_stdin, config, || println!("Formatting {}", path));
150164
context.format_file(path, &module, is_macro_def)?;
151165
}

Diff for: src/test/mod.rs

+24
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,30 @@ fn stdin_generated_files_issue_5172() {
578578
);
579579
}
580580

581+
#[test]
582+
fn stdin_handles_mod_inner_ignore_attr() {
583+
// see https://github.com/rust-lang/rustfmt/issues/5368
584+
init_log();
585+
let input = String::from("#![rustfmt::skip]\n\nfn main() { }");
586+
let mut child = Command::new(rustfmt().to_str().unwrap())
587+
.stdin(Stdio::piped())
588+
.stdout(Stdio::piped())
589+
.spawn()
590+
.expect("failed to execute child");
591+
592+
{
593+
let stdin = child.stdin.as_mut().expect("failed to get stdin");
594+
stdin
595+
.write_all(input.as_bytes())
596+
.expect("failed to write stdin");
597+
}
598+
599+
let output = child.wait_with_output().expect("failed to wait on child");
600+
assert!(output.status.success());
601+
assert!(output.stderr.is_empty());
602+
assert_eq!(input, String::from_utf8(output.stdout).unwrap());
603+
}
604+
581605
#[test]
582606
fn format_lines_errors_are_reported() {
583607
init_log();

0 commit comments

Comments
 (0)