Skip to content

Commit 821d04b

Browse files
committed
Do not report errors on skipped items or statements
1 parent 5e6bb3e commit 821d04b

File tree

3 files changed

+66
-20
lines changed

3 files changed

+66
-20
lines changed

src/lib.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ fn format_ast<F>(
300300
mut after_file: F,
301301
) -> Result<(FileMap, bool), io::Error>
302302
where
303-
F: FnMut(&str, &mut StringBuffer) -> Result<bool, io::Error>,
303+
F: FnMut(&str, &mut StringBuffer, &[(usize, usize)]) -> Result<bool, io::Error>,
304304
{
305305
let mut result = FileMap::new();
306306
// diff mode: check if any files are differing
@@ -343,7 +343,7 @@ where
343343
::utils::count_newlines(&format!("{}", visitor.buffer))
344344
);
345345

346-
has_diff |= match after_file(path_str, &mut visitor.buffer) {
346+
has_diff |= match after_file(path_str, &mut visitor.buffer, &visitor.skipped_range) {
347347
Ok(result) => result,
348348
Err(e) => {
349349
// Create a new error with path_str to help users see which files failed
@@ -358,10 +358,24 @@ where
358358
Ok((result, has_diff))
359359
}
360360

361+
/// Returns true if the line with the given line number was skipped by `#[rustfmt_skip]`.
362+
fn is_skipped_line(line_number: usize, skipped_range: &[(usize, usize)]) -> bool {
363+
skipped_range
364+
.iter()
365+
.any(|&(lo, hi)| lo <= line_number && line_number <= hi)
366+
}
367+
361368
// Formatting done on a char by char or line by line basis.
362369
// FIXME(#209) warn on bad license
363370
// FIXME(#20) other stuff for parity with make tidy
364-
fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &mut FormatReport) {
371+
fn format_lines(
372+
text: &mut StringBuffer,
373+
name: &str,
374+
skipped_range: &[(usize, usize)],
375+
config: &Config,
376+
report: &mut FormatReport,
377+
) {
378+
println!("skipped_range: {:?}", skipped_range);
365379
// Iterate over the chars in the file map.
366380
let mut trims = vec![];
367381
let mut last_wspace: Option<usize> = None;
@@ -403,6 +417,7 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m
403417

404418
// Check for any line width errors we couldn't correct.
405419
let report_error_on_line_overflow = config.error_on_line_overflow()
420+
&& !is_skipped_line(cur_line, skipped_range)
406421
&& (config.error_on_line_overflow_comments() || !is_comment);
407422
if report_error_on_line_overflow && line_len > config.max_width() {
408423
errors.push(FormattingError {
@@ -448,12 +463,14 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m
448463
}
449464

450465
for &(l, _, _, ref b) in &trims {
451-
errors.push(FormattingError {
452-
line: l,
453-
kind: ErrorKind::TrailingWhitespace,
454-
is_comment: false,
455-
line_buffer: b.clone(),
456-
});
466+
if !is_skipped_line(l, skipped_range) {
467+
errors.push(FormattingError {
468+
line: l,
469+
kind: ErrorKind::TrailingWhitespace,
470+
is_comment: false,
471+
line_buffer: b.clone(),
472+
});
473+
}
457474
}
458475

459476
report.file_error_map.insert(name.to_owned(), errors);
@@ -546,12 +563,12 @@ pub fn format_input<T: Write>(
546563
&mut parse_session,
547564
&main_file,
548565
config,
549-
|file_name, file| {
566+
|file_name, file, skipped_range| {
550567
// For some reason, the codemap does not include terminating
551568
// newlines so we must add one on for each file. This is sad.
552569
filemap::append_newline(file);
553570

554-
format_lines(file, file_name, config, &mut report);
571+
format_lines(file, file_name, skipped_range, config, &mut report);
555572

556573
if let Some(ref mut out) = out {
557574
return filemap::write_file(file, file_name, out, config);

src/spanned.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ implement_spanned!(ast::Field);
5454
implement_spanned!(ast::ForeignItem);
5555
implement_spanned!(ast::Item);
5656
implement_spanned!(ast::Local);
57+
implement_spanned!(ast::TraitItem);
58+
implement_spanned!(ast::ImplItem);
5759

5860
impl Spanned for ast::Stmt {
5961
fn span(&self) -> Span {

src/visitor.rs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub struct FmtVisitor<'a> {
8282
pub is_if_else_block: bool,
8383
pub snippet_provider: &'a SnippetProvider<'a>,
8484
pub line_number: usize,
85+
pub skipped_range: Vec<(usize, usize)>,
8586
}
8687

8788
impl<'b, 'a: 'b> FmtVisitor<'a> {
@@ -101,13 +102,17 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
101102
self.visit_item(item);
102103
}
103104
ast::StmtKind::Local(..) | ast::StmtKind::Expr(..) | ast::StmtKind::Semi(..) => {
104-
let rewrite = stmt.rewrite(&self.get_context(), self.shape());
105-
self.push_rewrite(stmt.span(), rewrite)
105+
if contains_skip(get_attrs_from_stmt(stmt)) {
106+
self.push_skipped_with_span(stmt.span());
107+
} else {
108+
let rewrite = stmt.rewrite(&self.get_context(), self.shape());
109+
self.push_rewrite(stmt.span(), rewrite)
110+
}
106111
}
107112
ast::StmtKind::Mac(ref mac) => {
108113
let (ref mac, _macro_style, ref attrs) = **mac;
109114
if self.visit_attrs(attrs, ast::AttrStyle::Outer) {
110-
self.push_rewrite(stmt.span(), None);
115+
self.push_skipped_with_span(stmt.span());
111116
} else {
112117
self.visit_mac(mac, None, MacroPosition::Statement);
113118
}
@@ -321,7 +326,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
321326
// Module is inline, in this case we treat modules like any
322327
// other item.
323328
if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
324-
self.push_rewrite(item.span, None);
329+
self.push_skipped_with_span(item.span());
325330
return;
326331
}
327332
} else if contains_skip(&item.attrs) {
@@ -349,7 +354,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
349354
}
350355
_ => {
351356
if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
352-
self.push_rewrite(item.span, None);
357+
self.push_skipped_with_span(item.span());
353358
return;
354359
}
355360
}
@@ -436,7 +441,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
436441
skip_out_of_file_lines_range_visitor!(self, ti.span);
437442

438443
if self.visit_attrs(&ti.attrs, ast::AttrStyle::Outer) {
439-
self.push_rewrite(ti.span, None);
444+
self.push_skipped_with_span(ti.span());
440445
return;
441446
}
442447

@@ -478,7 +483,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
478483
skip_out_of_file_lines_range_visitor!(self, ii.span);
479484

480485
if self.visit_attrs(&ii.attrs, ast::AttrStyle::Outer) {
481-
self.push_rewrite(ii.span, None);
486+
self.push_skipped_with_span(ii.span());
482487
return;
483488
}
484489

@@ -525,8 +530,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
525530
self.buffer.push_str(s);
526531
}
527532

528-
pub fn push_rewrite(&mut self, span: Span, rewrite: Option<String>) {
529-
self.format_missing_with_indent(source!(self, span).lo());
533+
fn push_rewrite_inner(&mut self, span: Span, rewrite: Option<String>) {
530534
if let Some(ref s) = rewrite {
531535
self.push_str(s);
532536
} else {
@@ -536,6 +540,19 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
536540
self.last_pos = source!(self, span).hi();
537541
}
538542

543+
pub fn push_rewrite(&mut self, span: Span, rewrite: Option<String>) {
544+
self.format_missing_with_indent(source!(self, span).lo());
545+
self.push_rewrite_inner(span, rewrite);
546+
}
547+
548+
pub fn push_skipped_with_span(&mut self, span: Span) {
549+
self.format_missing_with_indent(source!(self, span).lo());
550+
let lo = self.line_number + 1;
551+
self.push_rewrite_inner(span, None);
552+
let hi = self.line_number + 1;
553+
self.skipped_range.push((lo, hi));
554+
}
555+
539556
pub fn from_context(ctx: &'a RewriteContext) -> FmtVisitor<'a> {
540557
FmtVisitor::from_codemap(ctx.parse_session, ctx.config, ctx.snippet_provider)
541558
}
@@ -555,6 +572,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
555572
is_if_else_block: false,
556573
snippet_provider: snippet_provider,
557574
line_number: 0,
575+
skipped_range: vec![],
558576
}
559577
}
560578

@@ -1058,3 +1076,12 @@ pub fn rewrite_extern_crate(context: &RewriteContext, item: &ast::Item) -> Optio
10581076
String::from(&*Regex::new(r"\s;").unwrap().replace(no_whitespace, ";"))
10591077
})
10601078
}
1079+
1080+
fn get_attrs_from_stmt(stmt: &ast::Stmt) -> &[ast::Attribute] {
1081+
match stmt.node {
1082+
ast::StmtKind::Local(ref local) => &local.attrs,
1083+
ast::StmtKind::Item(ref item) => &item.attrs,
1084+
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => &expr.attrs,
1085+
ast::StmtKind::Mac(ref mac) => &mac.2,
1086+
}
1087+
}

0 commit comments

Comments
 (0)