1
Fork 0

Refactor disallowed_script_idents: Simplify script checking loop.

This commit is contained in:
Jason Newcomb 2024-06-09 22:19:35 -04:00
parent d2400a49a4
commit ac939ad3a1

View file

@ -82,30 +82,25 @@ impl EarlyLintPass for DisallowedScriptIdents {
// Note: `symbol.as_str()` is an expensive operation, thus should not be called // Note: `symbol.as_str()` is an expensive operation, thus should not be called
// more than once for a single symbol. // more than once for a single symbol.
let symbol_str = symbol.as_str(); let symbol_str = symbol.as_str();
if symbol_str.is_ascii() {
continue;
}
for c in symbol_str.chars() { // Check if any character in the symbol is not part of any allowed script.
// We want to iterate through all the scripts associated with this character // Fast path for ascii-only idents.
// and check whether at least of one scripts is in the whitelist. if !symbol_str.is_ascii()
let forbidden_script = c && let Some(script) = symbol_str.chars().find_map(|c| {
.script_extension() c.script_extension()
.iter() .iter()
.find(|script| !self.whitelist.contains(script)); .find(|script| !self.whitelist.contains(script))
if let Some(script) = forbidden_script { })
span_lint( {
cx, span_lint(
DISALLOWED_SCRIPT_IDENTS, cx,
span, DISALLOWED_SCRIPT_IDENTS,
format!( span,
"identifier `{symbol_str}` has a Unicode script that is not allowed by configuration: {}", format!(
script.full_name() "identifier `{symbol_str}` has a Unicode script that is not allowed by configuration: {}",
), script.full_name()
); ),
// We don't want to spawn warning multiple times over a single identifier. );
break;
}
} }
} }
} }