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

Do not touch module with #![rustfmt::skip] #4297

Merged
merged 3 commits into from
Jul 3, 2020
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
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 @@ -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) {
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