2020-01-05 10:07:26 +01:00
|
|
|
use rustc::lint::{EarlyContext, EarlyLintPass, LintContext};
|
2019-06-15 20:22:07 -07:00
|
|
|
use syntax::ast;
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub NON_ASCII_IDENTS,
|
|
|
|
Allow,
|
|
|
|
"detects non-ASCII identifiers"
|
|
|
|
}
|
|
|
|
|
2020-01-02 20:02:22 +08:00
|
|
|
declare_lint! {
|
|
|
|
pub UNCOMMON_CODEPOINTS,
|
|
|
|
Warn,
|
|
|
|
"detects uncommon Unicode codepoints in identifiers"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(NonAsciiIdents => [NON_ASCII_IDENTS, UNCOMMON_CODEPOINTS]);
|
2019-06-15 20:22:07 -07:00
|
|
|
|
|
|
|
impl EarlyLintPass for NonAsciiIdents {
|
|
|
|
fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: ast::Ident) {
|
2020-01-02 20:02:22 +08:00
|
|
|
use unicode_security::GeneralSecurityProfile;
|
|
|
|
let name_str = ident.name.as_str();
|
|
|
|
if name_str.is_ascii() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cx.struct_span_lint(
|
|
|
|
NON_ASCII_IDENTS,
|
|
|
|
ident.span,
|
|
|
|
"identifier contains non-ASCII characters",
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
if !name_str.chars().all(GeneralSecurityProfile::identifier_allowed) {
|
2019-06-15 20:22:07 -07:00
|
|
|
cx.struct_span_lint(
|
2020-01-02 20:02:22 +08:00
|
|
|
UNCOMMON_CODEPOINTS,
|
2019-06-15 20:22:07 -07:00
|
|
|
ident.span,
|
2020-01-02 20:02:22 +08:00
|
|
|
"identifier contains uncommon Unicode codepoints",
|
2019-12-22 17:42:04 -05:00
|
|
|
)
|
|
|
|
.emit();
|
2019-06-15 20:22:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|