Check for None-delimited group in attribute value

This commit is contained in:
David Tolnay 2023-03-20 04:03:25 -07:00
parent c3d637f397
commit dd460f82a1
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 21 additions and 16 deletions

View File

@ -24,7 +24,7 @@ proc-macro = true
[dependencies] [dependencies]
proc-macro2 = "1.0" proc-macro2 = "1.0"
quote = "1.0" quote = "1.0"
syn = "2.0" syn = "2.0.3"
[dev-dependencies] [dev-dependencies]
serde = { version = "1.0", path = "../serde" } serde = { version = "1.0", path = "../serde" }

View File

@ -1381,21 +1381,26 @@ fn get_lit_str2(
meta_item_name: Symbol, meta_item_name: Symbol,
meta: &ParseNestedMeta, meta: &ParseNestedMeta,
) -> syn::Result<Option<syn::LitStr>> { ) -> syn::Result<Option<syn::LitStr>> {
match meta.value()?.parse()? { let expr: syn::Expr = meta.value()?.parse()?;
syn::Expr::Lit(syn::ExprLit { let mut value = &expr;
lit: syn::Lit::Str(lit), while let syn::Expr::Group(e) = value {
.. value = &e.expr;
}) => Ok(Some(lit)), }
expr => { if let syn::Expr::Lit(syn::ExprLit {
cx.error_spanned_by( lit: syn::Lit::Str(lit),
expr, ..
format!( }) = value
"expected serde {} attribute to be a string: `{} = \"...\"`", {
attr_name, meta_item_name Ok(Some(lit.clone()))
), } else {
); cx.error_spanned_by(
Ok(None) expr,
} format!(
"expected serde {} attribute to be a string: `{} = \"...\"`",
attr_name, meta_item_name
),
);
Ok(None)
} }
} }