]> git.lizzy.rs Git - rust.git/blob - src/librustc_lint/non_ascii_idents.rs
Auto merge of #68406 - andjo403:selfprofileLlvm, r=wesleywiser
[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(NON_ASCII_IDENTS, ident.span, |lint| {
26             lint.build("identifier contains non-ASCII characters").emit()
27         });
28         if !name_str.chars().all(GeneralSecurityProfile::identifier_allowed) {
29             cx.struct_span_lint(UNCOMMON_CODEPOINTS, ident.span, |lint| {
30                 lint.build("identifier contains uncommon Unicode codepoints").emit()
31             })
32         }
33     }
34 }