]> git.lizzy.rs Git - rust.git/blob - src/librustc_lint/non_ascii_idents.rs
Rollup merge of #68097 - MikailBag:master, r=shepmaster
[rust.git] / src / librustc_lint / non_ascii_idents.rs
1 use crate::{EarlyContext, EarlyLintPass, LintContext};
2 use syntax::ast;
3
4 declare_lint! {
5     pub NON_ASCII_IDENTS,
6     Allow,
7     "detects non-ASCII identifiers"
8 }
9
10 declare_lint! {
11     pub UNCOMMON_CODEPOINTS,
12     Warn,
13     "detects uncommon Unicode codepoints in identifiers"
14 }
15
16 declare_lint_pass!(NonAsciiIdents => [NON_ASCII_IDENTS, UNCOMMON_CODEPOINTS]);
17
18 impl EarlyLintPass for NonAsciiIdents {
19     fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: ast::Ident) {
20         use unicode_security::GeneralSecurityProfile;
21         let name_str = ident.name.as_str();
22         if name_str.is_ascii() {
23             return;
24         }
25         cx.struct_span_lint(
26             NON_ASCII_IDENTS,
27             ident.span,
28             "identifier contains non-ASCII characters",
29         )
30         .emit();
31         if !name_str.chars().all(GeneralSecurityProfile::identifier_allowed) {
32             cx.struct_span_lint(
33                 UNCOMMON_CODEPOINTS,
34                 ident.span,
35                 "identifier contains uncommon Unicode codepoints",
36             )
37             .emit();
38         }
39     }
40 }