From 5d3edf9d7e57f8a3080b12f96ab7f1f6e491061e Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 31 Oct 2024 14:25:18 -0700 Subject: [PATCH] Improve error on malformed format attribute --- impl/src/attr.rs | 12 ++++++++---- tests/ui/concat-display.stderr | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/impl/src/attr.rs b/impl/src/attr.rs index e0ac02b1..e59b5191 100644 --- a/impl/src/attr.rs +++ b/impl/src/attr.rs @@ -91,7 +91,11 @@ fn parse_error_attribute<'a>(attrs: &mut Attrs<'a>, attr: &'a Attribute) -> Resu syn::custom_keyword!(transparent); attr.parse_args_with(|input: ParseStream| { - if let Some(kw) = input.parse::>()? { + let lookahead = input.lookahead1(); + let fmt = if lookahead.peek(LitStr) { + input.parse::()? + } else if lookahead.peek(transparent) { + let kw: transparent = input.parse()?; if attrs.transparent.is_some() { return Err(Error::new_spanned( attr, @@ -103,9 +107,9 @@ fn parse_error_attribute<'a>(attrs: &mut Attrs<'a>, attr: &'a Attribute) -> Resu span: kw.span, }); return Ok(()); - } - - let fmt: LitStr = input.parse()?; + } else { + return Err(lookahead.error()); + }; let ahead = input.fork(); ahead.parse::>()?; diff --git a/tests/ui/concat-display.stderr b/tests/ui/concat-display.stderr index dbecd69f..d92e635a 100644 --- a/tests/ui/concat-display.stderr +++ b/tests/ui/concat-display.stderr @@ -1,4 +1,4 @@ -error: expected string literal +error: expected string literal or `transparent` --> tests/ui/concat-display.rs:8:17 | 8 | #[error(concat!("invalid ", $what))]