proc-macro2 icon indicating copy to clipboard operation
proc-macro2 copied to clipboard

Ident construction example is incorrect in the presence of raw identifiers

Open sfackler opened this issue 4 years ago • 1 comments

The documentation for Ident has an example of how to make an ident with a name derived from another: https://docs.rs/proc-macro2/1.0.32/proc_macro2/struct.Ident.html#examples by just interpolating with format!(). However, it appears that if the original ident was raw, the output is invalid and unparseable: https://github.com/sfackler/rust-postgres/issues/839.

Is there any way of handling this properly other than something like format!("__{}", ident.to_string().strip_prefix("r#"))?

sfackler avatar Nov 23 '21 12:11 sfackler

https://docs.rs/quote/1/quote/macro.format_ident.html is the best option: format_ident!("__{}", ident)

dtolnay avatar Nov 23 '21 16:11 dtolnay