]> git.lizzy.rs Git - rust.git/blobdiff - src/comment.rs
deps: bump rustc-ap to v672
[rust.git] / src / comment.rs
index 232d3745e59c074dd6a360b272e2d5d509676894..1da62d176817171b46b433b06978f6c6fc2fcd74 100644 (file)
@@ -3,7 +3,7 @@
 use std::{self, borrow::Cow, iter};
 
 use itertools::{multipeek, MultiPeek};
-use syntax::source_map::Span;
+use rustc_span::Span;
 
 use crate::config::Config;
 use crate::rewrite::RewriteContext;
@@ -91,8 +91,9 @@ pub(crate) fn closer(&self) -> &'a str {
             | CommentStyle::TripleSlash
             | CommentStyle::Custom(..)
             | CommentStyle::Doc => "",
-            CommentStyle::DoubleBullet => " **/",
-            CommentStyle::SingleBullet | CommentStyle::Exclamation => " */",
+            CommentStyle::SingleBullet | CommentStyle::DoubleBullet | CommentStyle::Exclamation => {
+                " */"
+            }
         }
     }
 
@@ -101,8 +102,9 @@ pub(crate) fn line_start(&self) -> &'a str {
             CommentStyle::DoubleSlash => "// ",
             CommentStyle::TripleSlash => "/// ",
             CommentStyle::Doc => "//! ",
-            CommentStyle::SingleBullet | CommentStyle::Exclamation => " * ",
-            CommentStyle::DoubleBullet => " ** ",
+            CommentStyle::SingleBullet | CommentStyle::DoubleBullet | CommentStyle::Exclamation => {
+                " * "
+            }
             CommentStyle::Custom(opener) => opener,
         }
     }
@@ -112,7 +114,7 @@ pub(crate) fn to_str_tuplet(&self) -> (&'a str, &'a str, &'a str) {
     }
 }
 
-fn comment_style(orig: &str, normalize_comments: bool) -> CommentStyle<'_> {
+pub(crate) fn comment_style(orig: &str, normalize_comments: bool) -> CommentStyle<'_> {
     if !normalize_comments {
         if orig.starts_with("/**") && !orig.starts_with("/**/") {
             CommentStyle::DoubleBullet
@@ -527,7 +529,6 @@ fn new(
             .checked_sub(closer.len() + opener.len())
             .unwrap_or(1);
         let indent_str = shape.indent.to_string_with_newline(config).to_string();
-        let fmt_indent = shape.indent + (opener.len() - line_start.len());
 
         let mut cr = CommentRewrite {
             result: String::with_capacity(orig.len() * 2),
@@ -538,14 +539,14 @@ fn new(
             comment_line_separator: format!("{}{}", indent_str, line_start),
             max_width,
             indent_str,
-            fmt_indent,
+            fmt_indent: shape.indent,
 
             fmt: StringFormat {
                 opener: "",
                 closer: "",
                 line_start,
                 line_end: "",
-                shape: Shape::legacy(max_width, fmt_indent),
+                shape: Shape::legacy(max_width, shape.indent),
                 trim_end: true,
                 config,
             },
@@ -823,7 +824,8 @@ fn rewrite_comment_inner(
 const RUSTFMT_CUSTOM_COMMENT_PREFIX: &str = "//#### ";
 
 fn hide_sharp_behind_comment(s: &str) -> Cow<'_, str> {
-    if s.trim_start().starts_with("# ") {
+    let s_trimmed = s.trim();
+    if s_trimmed.starts_with("# ") || s_trimmed == "#" {
         Cow::from(format!("{}{}", RUSTFMT_CUSTOM_COMMENT_PREFIX, s))
     } else {
         Cow::from(s)
@@ -859,7 +861,9 @@ pub(crate) fn rewrite_missing_comment(
 ) -> Option<String> {
     let missing_snippet = context.snippet(span);
     let trimmed_snippet = missing_snippet.trim();
-    if !trimmed_snippet.is_empty() {
+    // check the span starts with a comment
+    let pos = trimmed_snippet.find('/');
+    if !trimmed_snippet.is_empty() && pos.is_some() {
         rewrite_comment(trimmed_snippet, false, shape, context.config)
     } else {
         Some(String::new())
@@ -880,7 +884,7 @@ pub(crate) fn recover_missing_comment_in_span(
         Some(String::new())
     } else {
         let missing_snippet = context.snippet(span);
-        let pos = missing_snippet.find('/').unwrap_or(0);
+        let pos = missing_snippet.find('/')?;
         // 1 = ` `
         let total_width = missing_comment.len() + used_width + 1;
         let force_new_line_before_comment =
@@ -1553,10 +1557,10 @@ pub(crate) fn recover_comment_removed(
         // We missed some comments. Warn and keep the original text.
         if context.config.error_on_unformatted() {
             context.report.append(
-                context.source_map.span_to_filename(span).into(),
+                context.parse_sess.span_to_filename(span),
                 vec![FormattingError::from_span(
                     span,
-                    &context.source_map,
+                    &context.parse_sess,
                     ErrorKind::LostComment,
                 )],
             );