Skip to content

Commit

Permalink
Improve error on malformed format attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 31, 2024
1 parent 003a89f commit 5d3edf9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 8 additions & 4 deletions impl/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Option<transparent>>()? {
let lookahead = input.lookahead1();
let fmt = if lookahead.peek(LitStr) {
input.parse::<LitStr>()?
} else if lookahead.peek(transparent) {
let kw: transparent = input.parse()?;
if attrs.transparent.is_some() {
return Err(Error::new_spanned(
attr,
Expand All @@ -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::<Option<Token![,]>>()?;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/concat-display.stderr
Original file line number Diff line number Diff line change
@@ -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))]
Expand Down

0 comments on commit 5d3edf9

Please sign in to comment.