From: Nick Cameron Date: Fri, 20 Jul 2018 02:42:48 +0000 (+1200) Subject: Trigger an internal error if we skip formatting due to a lost comment X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;ds=sidebyside;h=b085113cbe77b558624bbd2acb098956f5d6f266;p=rust.git Trigger an internal error if we skip formatting due to a lost comment --- diff --git a/src/comment.rs b/src/comment.rs index 53e496bf457..3d55f6560e3 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -20,6 +20,7 @@ use shape::{Indent, Shape}; use string::{rewrite_string, StringFormat}; use utils::{count_newlines, first_line_width, last_line_width}; +use {ErrorKind, FormattingError}; fn is_custom_comment(comment: &str) -> bool { if !comment.starts_with("//") { @@ -1124,7 +1125,17 @@ pub fn recover_comment_removed( ) -> Option { let snippet = context.snippet(span); if snippet != new && changed_comment_content(snippet, &new) { - // We missed some comments. Keep the original text. + // We missed some comments. Warn and keep the original text. + if context.config.error_on_unformatted() { + context.report.append( + context.codemap.span_to_filename(span).into(), + vec![FormattingError::from_span( + &span, + &context.codemap, + ErrorKind::LostComment, + )], + ); + } Some(snippet.to_owned()) } else { Some(new) diff --git a/src/lib.rs b/src/lib.rs index b856c66dd71..820134c2085 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -140,8 +140,20 @@ pub enum ErrorKind { ParseError, /// The user mandated a version and the current version of Rustfmt does not /// satisfy that requirement. - #[fail(display = "Version mismatch")] + #[fail(display = "version mismatch")] VersionMismatch, + /// If we had formatted the given node, then we would have lost a comment. + #[fail(display = "not formatted because a comment would be lost")] + LostComment, +} + +impl ErrorKind { + fn is_comment(&self) -> bool { + match self { + ErrorKind::LostComment => true, + _ => false, + } + } } impl From for ErrorKind { @@ -162,8 +174,8 @@ impl FormattingError { fn from_span(span: &Span, codemap: &CodeMap, kind: ErrorKind) -> FormattingError { FormattingError { line: codemap.lookup_char_pos(span.lo()).line, + is_comment: kind.is_comment(), kind, - is_comment: false, is_string: false, line_buffer: codemap .span_to_lines(*span) @@ -181,7 +193,8 @@ fn msg_prefix(&self) -> &str { ErrorKind::LineOverflow(..) | ErrorKind::TrailingWhitespace | ErrorKind::IoError(_) - | ErrorKind::ParseError => "internal error:", + | ErrorKind::ParseError + | ErrorKind::LostComment => "internal error:", ErrorKind::LicenseCheck | ErrorKind::BadAttr | ErrorKind::VersionMismatch => "error:", ErrorKind::BadIssue(_) | ErrorKind::DeprecatedAttr => "warning:", } @@ -200,7 +213,10 @@ fn msg_suffix(&self) -> &str { fn format_len(&self) -> (usize, usize) { match self.kind { ErrorKind::LineOverflow(found, max) => (max, found - max), - ErrorKind::TrailingWhitespace | ErrorKind::DeprecatedAttr | ErrorKind::BadAttr => { + ErrorKind::TrailingWhitespace + | ErrorKind::DeprecatedAttr + | ErrorKind::BadAttr + | ErrorKind::LostComment => { let trailing_ws_start = self .line_buffer .rfind(|c: char| !c.is_whitespace()) @@ -501,7 +517,7 @@ fn should_report_error( is_string: bool, error_kind: &ErrorKind, ) -> bool { - let allow_error_report = if char_kind.is_comment() || is_string { + let allow_error_report = if char_kind.is_comment() || is_string || error_kind.is_comment() { config.error_on_unformatted() } else { true @@ -509,7 +525,7 @@ fn should_report_error( match error_kind { ErrorKind::LineOverflow(..) => config.error_on_line_overflow() && allow_error_report, - ErrorKind::TrailingWhitespace => allow_error_report, + ErrorKind::TrailingWhitespace | ErrorKind::LostComment => allow_error_report, _ => true, } }