mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-05 05:04:24 +00:00
Do not report errors on skipped items or statements
This commit is contained in:
parent
5e6bb3edb0
commit
821d04b2a4
39
src/lib.rs
39
src/lib.rs
@ -300,7 +300,7 @@ fn format_ast<F>(
|
||||
mut after_file: F,
|
||||
) -> Result<(FileMap, bool), io::Error>
|
||||
where
|
||||
F: FnMut(&str, &mut StringBuffer) -> Result<bool, io::Error>,
|
||||
F: FnMut(&str, &mut StringBuffer, &[(usize, usize)]) -> Result<bool, io::Error>,
|
||||
{
|
||||
let mut result = FileMap::new();
|
||||
// diff mode: check if any files are differing
|
||||
@ -343,7 +343,7 @@ where
|
||||
::utils::count_newlines(&format!("{}", visitor.buffer))
|
||||
);
|
||||
|
||||
has_diff |= match after_file(path_str, &mut visitor.buffer) {
|
||||
has_diff |= match after_file(path_str, &mut visitor.buffer, &visitor.skipped_range) {
|
||||
Ok(result) => result,
|
||||
Err(e) => {
|
||||
// Create a new error with path_str to help users see which files failed
|
||||
@ -358,10 +358,24 @@ where
|
||||
Ok((result, has_diff))
|
||||
}
|
||||
|
||||
/// Returns true if the line with the given line number was skipped by `#[rustfmt_skip]`.
|
||||
fn is_skipped_line(line_number: usize, skipped_range: &[(usize, usize)]) -> bool {
|
||||
skipped_range
|
||||
.iter()
|
||||
.any(|&(lo, hi)| lo <= line_number && line_number <= hi)
|
||||
}
|
||||
|
||||
// Formatting done on a char by char or line by line basis.
|
||||
// FIXME(#209) warn on bad license
|
||||
// FIXME(#20) other stuff for parity with make tidy
|
||||
fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &mut FormatReport) {
|
||||
fn format_lines(
|
||||
text: &mut StringBuffer,
|
||||
name: &str,
|
||||
skipped_range: &[(usize, usize)],
|
||||
config: &Config,
|
||||
report: &mut FormatReport,
|
||||
) {
|
||||
println!("skipped_range: {:?}", skipped_range);
|
||||
// Iterate over the chars in the file map.
|
||||
let mut trims = vec![];
|
||||
let mut last_wspace: Option<usize> = None;
|
||||
@ -403,6 +417,7 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m
|
||||
|
||||
// Check for any line width errors we couldn't correct.
|
||||
let report_error_on_line_overflow = config.error_on_line_overflow()
|
||||
&& !is_skipped_line(cur_line, skipped_range)
|
||||
&& (config.error_on_line_overflow_comments() || !is_comment);
|
||||
if report_error_on_line_overflow && line_len > config.max_width() {
|
||||
errors.push(FormattingError {
|
||||
@ -448,12 +463,14 @@ fn format_lines(text: &mut StringBuffer, name: &str, config: &Config, report: &m
|
||||
}
|
||||
|
||||
for &(l, _, _, ref b) in &trims {
|
||||
errors.push(FormattingError {
|
||||
line: l,
|
||||
kind: ErrorKind::TrailingWhitespace,
|
||||
is_comment: false,
|
||||
line_buffer: b.clone(),
|
||||
});
|
||||
if !is_skipped_line(l, skipped_range) {
|
||||
errors.push(FormattingError {
|
||||
line: l,
|
||||
kind: ErrorKind::TrailingWhitespace,
|
||||
is_comment: false,
|
||||
line_buffer: b.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
report.file_error_map.insert(name.to_owned(), errors);
|
||||
@ -546,12 +563,12 @@ pub fn format_input<T: Write>(
|
||||
&mut parse_session,
|
||||
&main_file,
|
||||
config,
|
||||
|file_name, file| {
|
||||
|file_name, file, skipped_range| {
|
||||
// For some reason, the codemap does not include terminating
|
||||
// newlines so we must add one on for each file. This is sad.
|
||||
filemap::append_newline(file);
|
||||
|
||||
format_lines(file, file_name, config, &mut report);
|
||||
format_lines(file, file_name, skipped_range, config, &mut report);
|
||||
|
||||
if let Some(ref mut out) = out {
|
||||
return filemap::write_file(file, file_name, out, config);
|
||||
|
@ -54,6 +54,8 @@ implement_spanned!(ast::Field);
|
||||
implement_spanned!(ast::ForeignItem);
|
||||
implement_spanned!(ast::Item);
|
||||
implement_spanned!(ast::Local);
|
||||
implement_spanned!(ast::TraitItem);
|
||||
implement_spanned!(ast::ImplItem);
|
||||
|
||||
impl Spanned for ast::Stmt {
|
||||
fn span(&self) -> Span {
|
||||
|
@ -82,6 +82,7 @@ pub struct FmtVisitor<'a> {
|
||||
pub is_if_else_block: bool,
|
||||
pub snippet_provider: &'a SnippetProvider<'a>,
|
||||
pub line_number: usize,
|
||||
pub skipped_range: Vec<(usize, usize)>,
|
||||
}
|
||||
|
||||
impl<'b, 'a: 'b> FmtVisitor<'a> {
|
||||
@ -101,13 +102,17 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
||||
self.visit_item(item);
|
||||
}
|
||||
ast::StmtKind::Local(..) | ast::StmtKind::Expr(..) | ast::StmtKind::Semi(..) => {
|
||||
let rewrite = stmt.rewrite(&self.get_context(), self.shape());
|
||||
self.push_rewrite(stmt.span(), rewrite)
|
||||
if contains_skip(get_attrs_from_stmt(stmt)) {
|
||||
self.push_skipped_with_span(stmt.span());
|
||||
} else {
|
||||
let rewrite = stmt.rewrite(&self.get_context(), self.shape());
|
||||
self.push_rewrite(stmt.span(), rewrite)
|
||||
}
|
||||
}
|
||||
ast::StmtKind::Mac(ref mac) => {
|
||||
let (ref mac, _macro_style, ref attrs) = **mac;
|
||||
if self.visit_attrs(attrs, ast::AttrStyle::Outer) {
|
||||
self.push_rewrite(stmt.span(), None);
|
||||
self.push_skipped_with_span(stmt.span());
|
||||
} else {
|
||||
self.visit_mac(mac, None, MacroPosition::Statement);
|
||||
}
|
||||
@ -321,7 +326,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
||||
// Module is inline, in this case we treat modules like any
|
||||
// other item.
|
||||
if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
|
||||
self.push_rewrite(item.span, None);
|
||||
self.push_skipped_with_span(item.span());
|
||||
return;
|
||||
}
|
||||
} else if contains_skip(&item.attrs) {
|
||||
@ -349,7 +354,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
||||
}
|
||||
_ => {
|
||||
if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
|
||||
self.push_rewrite(item.span, None);
|
||||
self.push_skipped_with_span(item.span());
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -436,7 +441,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
||||
skip_out_of_file_lines_range_visitor!(self, ti.span);
|
||||
|
||||
if self.visit_attrs(&ti.attrs, ast::AttrStyle::Outer) {
|
||||
self.push_rewrite(ti.span, None);
|
||||
self.push_skipped_with_span(ti.span());
|
||||
return;
|
||||
}
|
||||
|
||||
@ -478,7 +483,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
||||
skip_out_of_file_lines_range_visitor!(self, ii.span);
|
||||
|
||||
if self.visit_attrs(&ii.attrs, ast::AttrStyle::Outer) {
|
||||
self.push_rewrite(ii.span, None);
|
||||
self.push_skipped_with_span(ii.span());
|
||||
return;
|
||||
}
|
||||
|
||||
@ -525,8 +530,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
||||
self.buffer.push_str(s);
|
||||
}
|
||||
|
||||
pub fn push_rewrite(&mut self, span: Span, rewrite: Option<String>) {
|
||||
self.format_missing_with_indent(source!(self, span).lo());
|
||||
fn push_rewrite_inner(&mut self, span: Span, rewrite: Option<String>) {
|
||||
if let Some(ref s) = rewrite {
|
||||
self.push_str(s);
|
||||
} else {
|
||||
@ -536,6 +540,19 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
||||
self.last_pos = source!(self, span).hi();
|
||||
}
|
||||
|
||||
pub fn push_rewrite(&mut self, span: Span, rewrite: Option<String>) {
|
||||
self.format_missing_with_indent(source!(self, span).lo());
|
||||
self.push_rewrite_inner(span, rewrite);
|
||||
}
|
||||
|
||||
pub fn push_skipped_with_span(&mut self, span: Span) {
|
||||
self.format_missing_with_indent(source!(self, span).lo());
|
||||
let lo = self.line_number + 1;
|
||||
self.push_rewrite_inner(span, None);
|
||||
let hi = self.line_number + 1;
|
||||
self.skipped_range.push((lo, hi));
|
||||
}
|
||||
|
||||
pub fn from_context(ctx: &'a RewriteContext) -> FmtVisitor<'a> {
|
||||
FmtVisitor::from_codemap(ctx.parse_session, ctx.config, ctx.snippet_provider)
|
||||
}
|
||||
@ -555,6 +572,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
||||
is_if_else_block: false,
|
||||
snippet_provider: snippet_provider,
|
||||
line_number: 0,
|
||||
skipped_range: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
@ -1058,3 +1076,12 @@ pub fn rewrite_extern_crate(context: &RewriteContext, item: &ast::Item) -> Optio
|
||||
String::from(&*Regex::new(r"\s;").unwrap().replace(no_whitespace, ";"))
|
||||
})
|
||||
}
|
||||
|
||||
fn get_attrs_from_stmt(stmt: &ast::Stmt) -> &[ast::Attribute] {
|
||||
match stmt.node {
|
||||
ast::StmtKind::Local(ref local) => &local.attrs,
|
||||
ast::StmtKind::Item(ref item) => &item.attrs,
|
||||
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => &expr.attrs,
|
||||
ast::StmtKind::Mac(ref mac) => &mac.2,
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user