Skip to content

Commit

Permalink
Merge pull request #332 from dtolnay/validate
Browse files Browse the repository at this point in the history
Move raw ident validate into validate_ident
  • Loading branch information
dtolnay committed Jun 20, 2022
2 parents 8b3a2dd + 868a8ea commit 83ed7b8
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/fallback.rs
Expand Up @@ -638,11 +638,7 @@ pub(crate) struct Ident {

impl Ident {
fn _new(string: &str, raw: bool, span: Span) -> Self {
validate_ident(string);

if raw && !Self::can_be_raw(string) {
panic!("`{}` cannot be a raw identifier", string);
}
validate_ident(string, raw);

Ident {
sym: string.to_owned(),
Expand All @@ -651,13 +647,6 @@ impl Ident {
}
}

fn can_be_raw(string: &str) -> bool {
match string {
"" | "_" | "super" | "self" | "Self" | "crate" | "$crate" | "{{root}}" => false,
_ => true,
}
}

pub fn new(string: &str, span: Span) -> Self {
Ident::_new(string, false, span)
}
Expand All @@ -683,7 +672,7 @@ pub(crate) fn is_ident_continue(c: char) -> bool {
unicode_ident::is_xid_continue(c)
}

fn validate_ident(string: &str) {
fn validate_ident(string: &str, raw: bool) {
let validate = string;
if validate.is_empty() {
panic!("Ident is not allowed to be empty; use Option<Ident>");
Expand All @@ -710,6 +699,15 @@ fn validate_ident(string: &str) {
if !ident_ok(validate) {
panic!("{:?} is not a valid Ident", string);
}

if raw {
match string {
"" | "_" | "super" | "self" | "Self" | "crate" | "$crate" | "{{root}}" => {
panic!("`{}` cannot be a raw identifier", string);
}
_ => {}
}
}
}

impl PartialEq for Ident {
Expand Down

0 comments on commit 83ed7b8

Please sign in to comment.