allow duplicate idents in format_args_f (#515)

This commit is contained in:
Demonthos authored and GitHub committed 2022-08-10 22:45:56 -07:00
1 parent c801d4402e
commit baf1807338
2 files changed
+26 -12

No files matched your search

+6
View File
@@ -23,4 +23,10 @@ fn formatting_compiles() {
format_args_f!("{x.borrow():?}").to_string(),
format!("{:?}", x.borrow())
);
// allows duplicate format args
assert_eq!(
format_args_f!("{x:?} {x:?}").to_string(),
format!("{:?} {:?}", x, x)
);
}
+20 -12
View File
@@ -1,4 +1,4 @@
use std::str::FromStr;
use std::{collections::HashSet, str::FromStr};
use proc_macro2::{Span, TokenStream};
@@ -48,17 +48,25 @@ pub fn format_args_f_impl(input: IfmtInput) -> Result<TokenStream> {
}
});
let named_args = input.segments.iter().filter_map(|seg| {
if let Segment::Formatted {
segment: FormattedSegment::Ident(ident),
..
} = seg
{
Some(quote! {#ident = #ident})
} else {
None
}
});
// remove duplicate idents
let named_args_idents: HashSet<_> = input
.segments
.iter()
.filter_map(|seg| {
if let Segment::Formatted {
segment: FormattedSegment::Ident(ident),
..
} = seg
{
Some(ident)
} else {
None
}
})
.collect();
let named_args = named_args_idents
.iter()
.map(|ident| quote!(#ident = #ident));
Ok(quote! {
format_args!(