Skip to content

Commit 5d3edf9

Browse files
committed
Improve error on malformed format attribute
1 parent 003a89f commit 5d3edf9

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

impl/src/attr.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ fn parse_error_attribute<'a>(attrs: &mut Attrs<'a>, attr: &'a Attribute) -> Resu
9191
syn::custom_keyword!(transparent);
9292

9393
attr.parse_args_with(|input: ParseStream| {
94-
if let Some(kw) = input.parse::<Option<transparent>>()? {
94+
let lookahead = input.lookahead1();
95+
let fmt = if lookahead.peek(LitStr) {
96+
input.parse::<LitStr>()?
97+
} else if lookahead.peek(transparent) {
98+
let kw: transparent = input.parse()?;
9599
if attrs.transparent.is_some() {
96100
return Err(Error::new_spanned(
97101
attr,
@@ -103,9 +107,9 @@ fn parse_error_attribute<'a>(attrs: &mut Attrs<'a>, attr: &'a Attribute) -> Resu
103107
span: kw.span,
104108
});
105109
return Ok(());
106-
}
107-
108-
let fmt: LitStr = input.parse()?;
110+
} else {
111+
return Err(lookahead.error());
112+
};
109113

110114
let ahead = input.fork();
111115
ahead.parse::<Option<Token![,]>>()?;

tests/ui/concat-display.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: expected string literal
1+
error: expected string literal or `transparent`
22
--> tests/ui/concat-display.rs:8:17
33
|
44
8 | #[error(concat!("invalid ", $what))]

0 commit comments

Comments
 (0)