Skip to content

Commit 68b43b2

Browse files
authored
Merge pull request #2250 from topecongiro/rustfmt-skip-no-warning-on-items
Do not report errors on skipped items or statements
2 parents db5d6dd + adc3b12 commit 68b43b2

File tree

6 files changed

+122
-66
lines changed

6 files changed

+122
-66
lines changed

src/imports.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ impl<'a> FmtVisitor<'a> {
285285
}
286286
Some(ref s) => {
287287
self.format_missing_with_indent(source!(self, span).lo());
288-
self.buffer.push_str(s);
288+
self.push_str(s);
289289
self.last_pos = source!(self, span).hi();
290290
}
291291
None => {

src/items.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,12 @@ impl<'a> FnSig<'a> {
240240

241241
impl<'a> FmtVisitor<'a> {
242242
fn format_item(&mut self, item: Item) {
243-
self.buffer.push_str(&item.abi);
243+
self.push_str(&item.abi);
244244

245245
let snippet = self.snippet(item.span);
246246
let brace_pos = snippet.find_uncommented("{").unwrap();
247247

248-
self.buffer.push_str("{");
248+
self.push_str("{");
249249
if !item.body.is_empty() || contains_comment(&snippet[brace_pos..]) {
250250
// FIXME: this skips comments between the extern keyword and the opening
251251
// brace.
@@ -255,9 +255,8 @@ impl<'a> FmtVisitor<'a> {
255255
if item.body.is_empty() {
256256
self.format_missing_no_indent(item.span.hi() - BytePos(1));
257257
self.block_indent = self.block_indent.block_unindent(self.config);
258-
259-
self.buffer
260-
.push_str(&self.block_indent.to_string(self.config));
258+
let indent_str = self.block_indent.to_string(self.config);
259+
self.push_str(&indent_str);
261260
} else {
262261
for item in &item.body {
263262
self.format_body_element(item);
@@ -268,7 +267,7 @@ impl<'a> FmtVisitor<'a> {
268267
}
269268
}
270269

271-
self.buffer.push_str("}");
270+
self.push_str("}");
272271
self.last_pos = item.span.hi();
273272
}
274273

@@ -423,7 +422,7 @@ impl<'a> FmtVisitor<'a> {
423422
span: Span,
424423
) {
425424
let enum_header = format_header("enum ", ident, vis);
426-
self.buffer.push_str(&enum_header);
425+
self.push_str(&enum_header);
427426

428427
let enum_snippet = self.snippet(span);
429428
let brace_pos = enum_snippet.find_uncommented("{").unwrap();
@@ -441,23 +440,23 @@ impl<'a> FmtVisitor<'a> {
441440
mk_sp(span.lo(), body_start),
442441
last_line_width(&enum_header),
443442
).unwrap();
444-
self.buffer.push_str(&generics_str);
443+
self.push_str(&generics_str);
445444

446445
self.last_pos = body_start;
447446

448447
self.block_indent = self.block_indent.block_indent(self.config);
449448
let variant_list = self.format_variant_list(enum_def, body_start, span.hi() - BytePos(1));
450449
match variant_list {
451-
Some(ref body_str) => self.buffer.push_str(body_str),
450+
Some(ref body_str) => self.push_str(body_str),
452451
None => self.format_missing_no_indent(span.hi() - BytePos(1)),
453452
}
454453
self.block_indent = self.block_indent.block_unindent(self.config);
455454

456455
if variant_list.is_some() || contains_comment(&enum_snippet[brace_pos..]) {
457-
self.buffer
458-
.push_str(&self.block_indent.to_string(self.config));
456+
let indent_str = self.block_indent.to_string(self.config);
457+
self.push_str(&indent_str);
459458
}
460-
self.buffer.push_str("}");
459+
self.push_str("}");
461460
self.last_pos = span.hi();
462461
}
463462

src/lib.rs

+32-11
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
@@ -338,7 +338,12 @@ where
338338
visitor.format_separate_mod(module, &*filemap);
339339
};
340340

341-
has_diff |= match after_file(path_str, &mut visitor.buffer) {
341+
assert_eq!(
342+
visitor.line_number,
343+
::utils::count_newlines(&format!("{}", visitor.buffer))
344+
);
345+
346+
has_diff |= match after_file(path_str, &mut visitor.buffer, &visitor.skipped_range) {
342347
Ok(result) => result,
343348
Err(e) => {
344349
// Create a new error with path_str to help users see which files failed
@@ -353,10 +358,23 @@ where
353358
Ok((result, has_diff))
354359
}
355360

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+
356368
// Formatting done on a char by char or line by line basis.
357369
// FIXME(#209) warn on bad license
358370
// FIXME(#20) other stuff for parity with make tidy
359-
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+
) {
360378
// Iterate over the chars in the file map.
361379
let mut trims = vec![];
362380
let mut last_wspace: Option<usize> = None;
@@ -398,6 +416,7 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m
398416

399417
// Check for any line width errors we couldn't correct.
400418
let report_error_on_line_overflow = config.error_on_line_overflow()
419+
&& !is_skipped_line(cur_line, skipped_range)
401420
&& (config.error_on_line_overflow_comments() || !is_comment);
402421
if report_error_on_line_overflow && line_len > config.max_width() {
403422
errors.push(FormattingError {
@@ -443,12 +462,14 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m
443462
}
444463

445464
for &(l, _, _, ref b) in &trims {
446-
errors.push(FormattingError {
447-
line: l,
448-
kind: ErrorKind::TrailingWhitespace,
449-
is_comment: false,
450-
line_buffer: b.clone(),
451-
});
465+
if !is_skipped_line(l, skipped_range) {
466+
errors.push(FormattingError {
467+
line: l,
468+
kind: ErrorKind::TrailingWhitespace,
469+
is_comment: false,
470+
line_buffer: b.clone(),
471+
});
472+
}
452473
}
453474

454475
report.file_error_map.insert(name.to_owned(), errors);
@@ -541,12 +562,12 @@ pub fn format_input<T: Write>(
541562
&mut parse_session,
542563
&main_file,
543564
config,
544-
|file_name, file| {
565+
|file_name, file, skipped_range| {
545566
// For some reason, the codemap does not include terminating
546567
// newlines so we must add one on for each file. This is sad.
547568
filemap::append_newline(file);
548569

549-
format_lines(file, file_name, config, &mut report);
570+
format_lines(file, file_name, skipped_range, config, &mut report);
550571

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

src/missed_spans.rs

+17-19
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,25 @@ impl<'a> FmtVisitor<'a> {
2828
// TODO these format_missing methods are ugly. Refactor and add unit tests
2929
// for the central whitespace stripping loop.
3030
pub fn format_missing(&mut self, end: BytePos) {
31-
self.format_missing_inner(end, |this, last_snippet, _| {
32-
this.buffer.push_str(last_snippet)
33-
})
31+
self.format_missing_inner(end, |this, last_snippet, _| this.push_str(last_snippet))
3432
}
3533

3634
pub fn format_missing_with_indent(&mut self, end: BytePos) {
3735
let config = self.config;
3836
self.format_missing_inner(end, |this, last_snippet, snippet| {
39-
this.buffer.push_str(last_snippet.trim_right());
37+
this.push_str(last_snippet.trim_right());
4038
if last_snippet == snippet && !this.output_at_start() {
4139
// No new lines in the snippet.
42-
this.buffer.push_str("\n");
40+
this.push_str("\n");
4341
}
4442
let indent = this.block_indent.to_string(config);
45-
this.buffer.push_str(&indent);
43+
this.push_str(&indent);
4644
})
4745
}
4846

4947
pub fn format_missing_no_indent(&mut self, end: BytePos) {
5048
self.format_missing_inner(end, |this, last_snippet, _| {
51-
this.buffer.push_str(last_snippet.trim_right());
49+
this.push_str(last_snippet.trim_right());
5250
})
5351
}
5452

@@ -97,7 +95,7 @@ impl<'a> FmtVisitor<'a> {
9795
newline_count = newline_lower_bound;
9896
}
9997
let blank_lines: String = repeat('\n').take(newline_count).collect();
100-
self.buffer.push_str(&blank_lines);
98+
self.push_str(&blank_lines);
10199
}
102100

103101
fn write_snippet<F>(&mut self, span: Span, process_last_snippet: F)
@@ -154,12 +152,12 @@ impl<'a> FmtVisitor<'a> {
154152
if status.rewrite_next_comment {
155153
if fix_indent {
156154
if let Some('{') = last_char {
157-
self.buffer.push_str("\n");
155+
self.push_str("\n");
158156
}
159-
self.buffer
160-
.push_str(&self.block_indent.to_string(self.config));
157+
let indent_str = self.block_indent.to_string(self.config);
158+
self.push_str(&indent_str);
161159
} else {
162-
self.buffer.push_str(" ");
160+
self.push_str(" ");
163161
}
164162

165163
let comment_width = ::std::cmp::min(
@@ -170,7 +168,7 @@ impl<'a> FmtVisitor<'a> {
170168
let comment_shape = Shape::legacy(comment_width, comment_indent);
171169
let comment_str = rewrite_comment(subslice, false, comment_shape, self.config)
172170
.unwrap_or_else(|| String::from(subslice));
173-
self.buffer.push_str(&comment_str);
171+
self.push_str(&comment_str);
174172

175173
status.last_wspace = None;
176174
status.line_start = offset + subslice.len();
@@ -183,13 +181,13 @@ impl<'a> FmtVisitor<'a> {
183181
.any(|s| s.len() >= 2 && &s[0..2] == "/*")
184182
{
185183
// Add a newline after line comments
186-
self.buffer.push_str("\n");
184+
self.push_str("\n");
187185
}
188186
} else if status.line_start <= snippet.len() {
189187
// For other comments add a newline if there isn't one at the end already
190188
match snippet[status.line_start..].chars().next() {
191189
Some('\n') | Some('\r') => (),
192-
_ => self.buffer.push_str("\n"),
190+
_ => self.push_str("\n"),
193191
}
194192
}
195193

@@ -277,10 +275,10 @@ impl<'a> FmtVisitor<'a> {
277275
}
278276

279277
if let Some(lw) = status.last_wspace {
280-
self.buffer.push_str(&snippet[status.line_start..lw]);
281-
self.buffer.push_str("\n");
278+
self.push_str(&snippet[status.line_start..lw]);
279+
self.push_str("\n");
282280
} else {
283-
self.buffer.push_str(&snippet[status.line_start..i + 1]);
281+
self.push_str(&snippet[status.line_start..i + 1]);
284282
}
285283

286284
status.cur_line += 1;
@@ -306,7 +304,7 @@ impl<'a> FmtVisitor<'a> {
306304

307305
let remaining = snippet[status.line_start..subslice.len() + offset].trim();
308306
if !remaining.is_empty() {
309-
self.buffer.push_str(remaining);
307+
self.push_str(remaining);
310308
status.line_start = subslice.len() + offset;
311309
status.rewrite_next_comment = true;
312310
}

src/spanned.rs

+2
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 {

0 commit comments

Comments
 (0)