]> git.lizzy.rs Git - rust.git/commitdiff
Return the original snippet if the attribute contains invalid syntax
authortopecongiro <seuchida@gmail.com>
Tue, 20 Feb 2018 05:48:46 +0000 (14:48 +0900)
committertopecongiro <seuchida@gmail.com>
Tue, 20 Feb 2018 05:48:46 +0000 (14:48 +0900)
This allows us to keep formatting the macro def with attributes that become
invalid syntax when the `$` is replaced with `z`, e.g. `#[doc = $expr]`.

rustfmt-core/src/visitor.rs
rustfmt-core/tests/source/macro_rules.rs
rustfmt-core/tests/target/macro_rules.rs

index 0921c189cd7837e7b8169f2a647ace4d4e54cecb..b3d3e3270e834be826ba1193f7aa219b0ead0a83 100644 (file)
@@ -813,9 +813,11 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
             }
             // 1 = `[`
             let shape = shape.offset_left(prefix.len() + 1)?;
-            self.meta()?
-                .rewrite(context, shape)
-                .map(|rw| format!("{}[{}]", prefix, rw))
+            Some(
+                self.meta()
+                    .and_then(|meta| meta.rewrite(context, shape))
+                    .map_or_else(|| snippet.to_owned(), |rw| format!("{}[{}]", prefix, rw)),
+            )
         }
     }
 }
index 4900574c4eb9bd1796e95765ca24d3957bf92b52..33465d207e2d6be2e4b0b61291bb7914e3d0a67e 100644 (file)
@@ -77,3 +77,11 @@ macro_rules! m (
 macro_rules! m [
     () => ()
 ];
+
+// #2470
+macro foo($type_name: ident, $docs: expr) {
+    #[allow(non_camel_case_types)]
+    #[doc=$docs]
+    #[derive(Debug, Clone, Copy)]
+    pub struct $type_name;
+}
index e6f779d8a03c3827d26f2b3fd203168546ddb7af..3f93124137e2a350ee490172fde8860a416a1f97 100644 (file)
@@ -68,3 +68,11 @@ macro_rules! m (
 macro_rules! m [
     () => ()
 ];
+
+// #2470
+macro foo($type_name: ident, $docs: expr) {
+    #[allow(non_camel_case_types)]
+    #[doc=$docs]
+    #[derive(Debug, Clone, Copy)]
+    pub struct $type_name;
+}