From 8a5c4d1b76eaa667a71dfaeb1373bca36fda4e78 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 11 Feb 2024 10:40:31 -0800 Subject: [PATCH] Use write_str when args only consists of trailing comma --- impl/src/attr.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/impl/src/attr.rs b/impl/src/attr.rs index 0e30d29..269c69e 100644 --- a/impl/src/attr.rs +++ b/impl/src/attr.rs @@ -1,6 +1,7 @@ use proc_macro2::{Delimiter, Group, Span, TokenStream, TokenTree}; use quote::{format_ident, quote, ToTokens}; use std::collections::BTreeSet as Set; +use syn::parse::discouraged::Speculative; use syn::parse::ParseStream; use syn::{ braced, bracketed, parenthesized, token, Attribute, Error, Ident, Index, LitInt, LitStr, Meta, @@ -105,8 +106,18 @@ fn parse_error_attribute<'a>(attrs: &mut Attrs<'a>, attr: &'a Attribute) -> Resu } let fmt: LitStr = input.parse()?; - let args = parse_token_expr(input, false)?; + + let ahead = input.fork(); + ahead.parse::>()?; + let args = if ahead.is_empty() { + input.advance_to(&ahead); + TokenStream::new() + } else { + parse_token_expr(input, false)? + }; + let requires_fmt_machinery = !args.is_empty(); + let display = Display { original: attr, fmt,