From 98ea26129e9efb4198760e4504261e5dbbad658e Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 7 Oct 2024 16:47:45 +0200 Subject: [PATCH] Check valid punctuation character in Punct::new Valid punctuation characters generated by: use proc_macro::{Punct, Spacing, TokenStream}; use std::panic; #[proc_macro] pub fn punct_chars(_input: TokenStream) -> TokenStream { for ch in '\0'..=char::MAX { if let Ok(_) = panic::catch_unwind(|| { let _ = Punct::new(ch, Spacing::Alone); }) { eprintln!("{:?}", ch); } } TokenStream::new() } --- src/lib.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0bce18e..ed38122 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -835,10 +835,16 @@ impl Punct { /// The returned `Punct` will have the default span of `Span::call_site()` /// which can be further configured with the `set_span` method below. pub fn new(ch: char, spacing: Spacing) -> Self { - Punct { - ch, - spacing, - span: Span::call_site(), + if let '!' | '#' | '$' | '%' | '&' | '\'' | '*' | '+' | ',' | '-' | '.' | '/' | ':' | ';' + | '<' | '=' | '>' | '?' | '@' | '^' | '|' | '~' = ch + { + Punct { + ch, + spacing, + span: Span::call_site(), + } + } else { + panic!("unsupported proc macro punctuation character {:?}", ch); } }