Return the original snippet if the attribute contains invalid syntax

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]`.
This commit is contained in:
topecongiro 2018-02-20 14:48:46 +09:00
parent be79f5e433
commit b4c85d1bcb
3 changed files with 21 additions and 3 deletions

View 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)),
)
}
}
}

View 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;
}

View 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;
}