From 780616ed74d22d96cb7464c2d558244bd665e39a Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 5 May 2018 22:45:59 +0300 Subject: proc_macro: Validate inputs to `Punct::new` and `Ident::new` --- src/libsyntax/parse/lexer/mod.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/lexer/mod.rs b/src/libsyntax/parse/lexer/mod.rs index 22a0261d8c6..4da7b8e93d9 100644 --- a/src/libsyntax/parse/lexer/mod.rs +++ b/src/libsyntax/parse/lexer/mod.rs @@ -1770,6 +1770,15 @@ fn ident_continue(c: Option) -> bool { (c > '\x7f' && c.is_xid_continue()) } +// The string is a valid identifier or a lifetime identifier. +pub fn is_valid_ident(s: &str) -> bool { + let mut chars = s.chars(); + match chars.next() { + Some('\'') => ident_start(chars.next()) && chars.all(|ch| ident_continue(Some(ch))), + ch => ident_start(ch) && chars.all(|ch| ident_continue(Some(ch))) + } +} + #[cfg(test)] mod tests { use super::*; -- cgit 1.4.1-3-g733a5