diff --git a/src/formatting.rs b/src/formatting.rs index 69ada297f4e..fdd47e3bc99 100644 --- a/src/formatting.rs +++ b/src/formatting.rs @@ -2,7 +2,7 @@ use std::time::{Duration, Instant}; -use rustc_ast::ast; +use rustc_ast::{ast, attr::HasAttrs}; use rustc_span::symbol; pub(crate) use syntux::session::ParseSess; @@ -14,7 +14,7 @@ use crate::formatting::{ newline_style::apply_newline_style, report::NonFormattedRange, syntux::parser::{DirectoryOwnership, Parser, ParserError}, - utils::count_newlines, + utils::{contains_skip, count_newlines}, visitor::FmtVisitor, }; use crate::{ @@ -129,6 +129,10 @@ fn format_project( if (!operation_setting.recursive && path != &main_file) || should_ignore { continue; } + if contains_skip(module.attrs()) { + continue; + } + should_emit_verbose(input_is_stdin, operation_setting.verbosity, || { println!("Formatting {}", path) }); diff --git a/src/formatting/visitor.rs b/src/formatting/visitor.rs index ffe74e0d333..ec96bef1c55 100644 --- a/src/formatting/visitor.rs +++ b/src/formatting/visitor.rs @@ -959,12 +959,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { pub(crate) fn format_separate_mod(&mut self, m: &Module<'_>, end_pos: BytePos) { self.block_indent = Indent::empty(); - if self.visit_attrs(m.attrs(), ast::AttrStyle::Inner) { - self.push_skipped_with_span(m.attrs(), m.as_ref().inner, m.as_ref().inner); - } else { - self.walk_mod_items(m.as_ref()); - self.format_missing_with_indent(end_pos); - } + let skipped = self.visit_attrs(m.attrs(), ast::AttrStyle::Inner); + assert!( + !skipped, + "Skipping module must be handled before reaching this line.", + ); + + self.walk_mod_items(m.as_ref()); + self.format_missing_with_indent(end_pos); } pub(crate) fn skip_empty_lines(&mut self, end_pos: BytePos) { diff --git a/src/test/configuration_snippet.rs b/src/test/configuration_snippet.rs index 2caf6fe7766..6ce983f8e7b 100644 --- a/src/test/configuration_snippet.rs +++ b/src/test/configuration_snippet.rs @@ -190,9 +190,19 @@ impl ConfigCodeBlock { } let report = report.unwrap(); - let result = report.format_result().next().unwrap(); - let text = result.1.formatted_text(); - !self.formatted_has_diff(text) + let result = report.format_result().next(); + + match result { + Some(result) => { + let text = result.1.formatted_text(); + !self.formatted_has_diff(text) + } + None => { + // The format report may be empty if the code block contains `#![rustfmt::skip]`. + // In that case, we just return true. + true + } + } } // Extract a code block from the iterator. Behavior: diff --git a/tests/target/skip/skip-with-trailing-comma.rs b/tests/target/skip/skip-with-trailing-comma.rs new file mode 100644 index 00000000000..24ec0521a58 --- /dev/null +++ b/tests/target/skip/skip-with-trailing-comma.rs @@ -0,0 +1,10 @@ +#![rustfmt::skip] + +fn myfunc1() +{ + println!("hi"); // yes I want my comments here +} // keep this comment here too + +fn myfunc2() { + println!("bye"); // i want it that way +} // tell me why