]> git.lizzy.rs Git - rust.git/blobdiff - src/macros.rs
Merge pull request #3035 from topecongiro/issue-3006
[rust.git] / src / macros.rs
index 8eff489412b31771e325583dbc67999bda3d9f05..736333cf44dbe6b4a7c578aef8d398dbc27dcb4f 100644 (file)
@@ -69,7 +69,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
         visitor.block_indent = shape.indent;
         visitor.last_pos = self.span().lo();
         visitor.visit_item(self);
-        Some(visitor.buffer)
+        Some(visitor.buffer.to_owned())
     }
 }
 
@@ -406,7 +406,15 @@ pub fn rewrite_macro_def(
         ";",
         |branch| branch.span.lo(),
         |branch| branch.span.hi(),
-        |branch| branch.rewrite(context, arm_shape, multi_branch_style),
+        |branch| match branch.rewrite(context, arm_shape, multi_branch_style) {
+            Some(v) => Some(v),
+            // if the rewrite returned None because a macro could not be rewritten, then return the
+            // original body
+            None if *context.macro_rewrite_failure.borrow() == true => {
+                Some(context.snippet(branch.body).trim().to_string())
+            }
+            None => None,
+        },
         context.snippet_provider.span_after(span, "{"),
         span.hi(),
         false,
@@ -1118,6 +1126,7 @@ fn indent_macro_snippet(
             } else {
                 Some(get_prefix_space_width(context, &line))
             };
+
             let line = if veto_trim || (kind.is_string() && !line.ends_with('\\')) {
                 veto_trim = kind.is_string() && !line.ends_with('\\');
                 trimmed = false;
@@ -1126,7 +1135,12 @@ fn indent_macro_snippet(
                 line.trim().to_owned()
             };
             trimmed_lines.push((trimmed, line, prefix_space_width));
-            prefix_space_width
+
+            // when computing the minimum, do not consider lines within a string
+            match kind {
+                FullCodeCharKind::InString | FullCodeCharKind::EndString => None,
+                _ => prefix_space_width,
+            }
         }).min()?;
 
     Some(
@@ -1139,7 +1153,7 @@ fn indent_macro_snippet(
                         let new_indent_width = indent.width() + original_indent_width
                             .saturating_sub(min_prefix_space_width);
                         let new_indent = Indent::from_width(context.config, new_indent_width);
-                        format!("{}{}", new_indent.to_string(context.config), line.trim())
+                        format!("{}{}", new_indent.to_string(context.config), line)
                     }
                     None => String::new(),
                 },