Skip to content

Commit 5947aaf

Browse files
authored
Do not touch module with #![rustfmt::skip] (#4297)
2 parents cd7d3ff + 62758fd commit 5947aaf

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

Diff for: src/formatting.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::time::{Duration, Instant};
44

5-
use rustc_ast::ast;
5+
use rustc_ast::{ast, attr::HasAttrs};
66
use rustc_span::symbol;
77

88
pub(crate) use syntux::session::ParseSess;
@@ -14,7 +14,7 @@ use crate::formatting::{
1414
newline_style::apply_newline_style,
1515
report::NonFormattedRange,
1616
syntux::parser::{DirectoryOwnership, Parser, ParserError},
17-
utils::count_newlines,
17+
utils::{contains_skip, count_newlines},
1818
visitor::FmtVisitor,
1919
};
2020
use crate::{
@@ -129,6 +129,10 @@ fn format_project(
129129
if (!operation_setting.recursive && path != &main_file) || should_ignore {
130130
continue;
131131
}
132+
if contains_skip(module.attrs()) {
133+
continue;
134+
}
135+
132136
should_emit_verbose(input_is_stdin, operation_setting.verbosity, || {
133137
println!("Formatting {}", path)
134138
});

Diff for: src/formatting/visitor.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -967,12 +967,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
967967

968968
pub(crate) fn format_separate_mod(&mut self, m: &Module<'_>, end_pos: BytePos) {
969969
self.block_indent = Indent::empty();
970-
if self.visit_attrs(m.attrs(), ast::AttrStyle::Inner) {
971-
self.push_skipped_with_span(m.attrs(), m.as_ref().inner, m.as_ref().inner);
972-
} else {
973-
self.walk_mod_items(m.as_ref());
974-
self.format_missing_with_indent(end_pos);
975-
}
970+
let skipped = self.visit_attrs(m.attrs(), ast::AttrStyle::Inner);
971+
assert!(
972+
!skipped,
973+
"Skipping module must be handled before reaching this line.",
974+
);
975+
976+
self.walk_mod_items(m.as_ref());
977+
self.format_missing_with_indent(end_pos);
976978
}
977979

978980
pub(crate) fn skip_empty_lines(&mut self, end_pos: BytePos) {

Diff for: src/test/configuration_snippet.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,19 @@ impl ConfigCodeBlock {
190190
}
191191

192192
let report = report.unwrap();
193-
let result = report.format_result().next().unwrap();
194-
let text = result.1.formatted_text();
195-
!self.formatted_has_diff(text)
193+
let result = report.format_result().next();
194+
195+
match result {
196+
Some(result) => {
197+
let text = result.1.formatted_text();
198+
!self.formatted_has_diff(text)
199+
}
200+
None => {
201+
// The format report may be empty if the code block contains `#![rustfmt::skip]`.
202+
// In that case, we just return true.
203+
true
204+
}
205+
}
196206
}
197207

198208
// Extract a code block from the iterator. Behavior:

Diff for: tests/target/skip/skip-with-trailing-comma.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![rustfmt::skip]
2+
3+
fn myfunc1()
4+
{
5+
println!("hi"); // yes I want my comments here
6+
} // keep this comment here too
7+
8+
fn myfunc2() {
9+
println!("bye"); // i want it that way
10+
} // tell me why

0 commit comments

Comments
 (0)