Skip to content

Commit

Permalink
Do not touch module with #![rustfmt::skip] (#4297)
Browse files Browse the repository at this point in the history
  • Loading branch information
topecongiro authored Jul 3, 2020
2 parents cd7d3ff + 62758fd commit 5947aaf
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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::{
Expand Down Expand Up @@ -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)
});
Expand Down
14 changes: 8 additions & 6 deletions src/formatting/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -967,12 +967,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) {
Expand Down
16 changes: 13 additions & 3 deletions src/test/configuration_snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions tests/target/skip/skip-with-trailing-comma.rs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5947aaf

Please sign in to comment.