]> git.lizzy.rs Git - rust.git/blobdiff - src/comment.rs
Fix parsing '#'-hiding of rustdoc
[rust.git] / src / comment.rs
index 53e496bf45748be45f6dbf368a954b7fc903b53c..5958349c6ad6869fb32a4e7f4f3a5160395907ea 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("//") {
@@ -347,8 +348,7 @@ fn rewrite_comment_inner(
             }
 
             line
-        })
-        .map(|s| left_trim_comment_line(s, &style))
+        }).map(|s| left_trim_comment_line(s, &style))
         .map(|(line, has_leading_whitespace)| {
             if orig.starts_with("/*") && line_breaks == 0 {
                 (
@@ -500,7 +500,7 @@ fn rewrite_comment_inner(
 const RUSTFMT_CUSTOM_COMMENT_PREFIX: &str = "//#### ";
 
 fn hide_sharp_behind_comment<'a>(s: &'a str) -> Cow<'a, str> {
-    if s.trim_left().starts_with('#') {
+    if s.trim_left().starts_with("# ") {
         Cow::from(format!("{}{}", RUSTFMT_CUSTOM_COMMENT_PREFIX, s))
     } else {
         Cow::from(s)
@@ -516,8 +516,7 @@ fn trim_custom_comment_prefix(s: &str) -> String {
             } else {
                 line
             }
-        })
-        .collect::<Vec<_>>()
+        }).collect::<Vec<_>>()
         .join("\n")
 }
 
@@ -605,8 +604,7 @@ fn light_rewrite_comment(
             };
             // Preserve markdown's double-space line break syntax in doc comment.
             trim_right_unless_two_whitespaces(left_trimmed, is_doc_comment)
-        })
-        .collect();
+        }).collect();
     Some(lines.join(&format!("\n{}", offset.to_string(config))))
 }
 
@@ -1124,7 +1122,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)
@@ -1330,8 +1338,7 @@ fn uncommented(text: &str) -> String {
             .filter_map(|(s, c)| match s {
                 FullCodeCharKind::Normal | FullCodeCharKind::InString => Some(c),
                 _ => None,
-            })
-            .collect()
+            }).collect()
     }
 
     #[test]