]> git.lizzy.rs Git - rust.git/commitdiff
Trigger an internal error if we skip formatting due to a lost comment
authorNick Cameron <ncameron@mozilla.com>
Fri, 20 Jul 2018 02:42:48 +0000 (14:42 +1200)
committerNick Cameron <ncameron@mozilla.com>
Fri, 20 Jul 2018 04:05:19 +0000 (16:05 +1200)
src/comment.rs
src/lib.rs

index 53e496bf45748be45f6dbf368a954b7fc903b53c..3d55f6560e3e3a6ab9f5b58a5ef41504a071b916 100644 (file)
@@ -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<String> {
     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)
index b856c66dd71e1eaa2b6bea0b625bd375f6c8573d..820134c2085d4e6efaf50ddfefbf03282d4a2f47 100644 (file)
@@ -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<io::Error> 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,
     }
 }