proc_macro: Validate inputs to Punct::new and Ident::new

This commit is contained in:
Vadim Petrochenkov
2018-05-05 22:45:59 +03:00
parent f116ab6e6e
commit 780616ed74
12 changed files with 203 additions and 22 deletions

View File

@@ -1770,6 +1770,15 @@ fn ident_continue(c: Option<char>) -> 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::*;