]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/disallowed_script_idents.rs
Store a `Symbol` instead of an `Ident` in `AssocItem`
[rust.git] / clippy_lints / src / disallowed_script_idents.rs
1 use clippy_utils::diagnostics::span_lint;
2 use rustc_ast::ast;
3 use rustc_data_structures::fx::FxHashSet;
4 use rustc_lint::{EarlyContext, EarlyLintPass, Level};
5 use rustc_session::{declare_tool_lint, impl_lint_pass};
6 use unicode_script::{Script, UnicodeScript};
7
8 declare_clippy_lint! {
9     /// ### What it does
10     /// Checks for usage of unicode scripts other than those explicitly allowed
11     /// by the lint config.
12     ///
13     /// This lint doesn't take into account non-text scripts such as `Unknown` and `Linear_A`.
14     /// It also ignores the `Common` script type.
15     /// While configuring, be sure to use official script name [aliases] from
16     /// [the list of supported scripts][supported_scripts].
17     ///
18     /// See also: [`non_ascii_idents`].
19     ///
20     /// [aliases]: http://www.unicode.org/reports/tr24/tr24-31.html#Script_Value_Aliases
21     /// [supported_scripts]: https://www.unicode.org/iso15924/iso15924-codes.html
22     ///
23     /// ### Why is this bad?
24     /// It may be not desired to have many different scripts for
25     /// identifiers in the codebase.
26     ///
27     /// Note that if you only want to allow plain English, you might want to use
28     /// built-in [`non_ascii_idents`] lint instead.
29     ///
30     /// [`non_ascii_idents`]: https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#non-ascii-idents
31     ///
32     /// ### Example
33     /// ```rust
34     /// // Assuming that `clippy.toml` contains the following line:
35     /// // allowed-locales = ["Latin", "Cyrillic"]
36     /// let counter = 10; // OK, latin is allowed.
37     /// let счётчик = 10; // OK, cyrillic is allowed.
38     /// let zähler = 10; // OK, it's still latin.
39     /// let カウンタ = 10; // Will spawn the lint.
40     /// ```
41     #[clippy::version = "1.55.0"]
42     pub DISALLOWED_SCRIPT_IDENTS,
43     restriction,
44     "usage of non-allowed Unicode scripts"
45 }
46
47 #[derive(Clone, Debug)]
48 pub struct DisallowedScriptIdents {
49     whitelist: FxHashSet<Script>,
50 }
51
52 impl DisallowedScriptIdents {
53     pub fn new(whitelist: &[String]) -> Self {
54         let whitelist = whitelist
55             .iter()
56             .map(String::as_str)
57             .filter_map(Script::from_full_name)
58             .collect();
59         Self { whitelist }
60     }
61 }
62
63 impl_lint_pass!(DisallowedScriptIdents => [DISALLOWED_SCRIPT_IDENTS]);
64
65 impl EarlyLintPass for DisallowedScriptIdents {
66     fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &ast::Crate) {
67         // Implementation is heavily inspired by the implementation of [`non_ascii_idents`] lint:
68         // https://github.com/rust-lang/rust/blob/master/compiler/rustc_lint/src/non_ascii_idents.rs
69
70         let check_disallowed_script_idents = cx.builder.lint_level(DISALLOWED_SCRIPT_IDENTS).0 != Level::Allow;
71         if !check_disallowed_script_idents {
72             return;
73         }
74
75         let symbols = cx.sess.parse_sess.symbol_gallery.symbols.lock();
76         // Sort by `Span` so that error messages make sense with respect to the
77         // order of identifier locations in the code.
78         let mut symbols: Vec<_> = symbols.iter().collect();
79         symbols.sort_unstable_by_key(|k| k.1);
80
81         for (symbol, &span) in &symbols {
82             // Note: `symbol.as_str()` is an expensive operation, thus should not be called
83             // more than once for a single symbol.
84             let symbol_str = symbol.as_str();
85             if symbol_str.is_ascii() {
86                 continue;
87             }
88
89             for c in symbol_str.chars() {
90                 // We want to iterate through all the scripts associated with this character
91                 // and check whether at least of one scripts is in the whitelist.
92                 let forbidden_script = c
93                     .script_extension()
94                     .iter()
95                     .find(|script| !self.whitelist.contains(script));
96                 if let Some(script) = forbidden_script {
97                     span_lint(
98                         cx,
99                         DISALLOWED_SCRIPT_IDENTS,
100                         span,
101                         &format!(
102                             "identifier `{}` has a Unicode script that is not allowed by configuration: {}",
103                             symbol_str,
104                             script.full_name()
105                         ),
106                     );
107                     // We don't want to spawn warning multiple times over a single identifier.
108                     break;
109                 }
110             }
111         }
112     }
113 }