1 use clippy_utils::diagnostics::span_lint;
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};
10 /// Checks for usage of unicode scripts other than those explicitly allowed
11 /// by the lint config.
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].
18 /// See also: [`non_ascii_idents`].
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
23 /// ### Why is this bad?
24 /// It may be not desired to have many different scripts for
25 /// identifiers in the codebase.
27 /// Note that if you only want to allow plain English, you might want to use
28 /// built-in [`non_ascii_idents`] lint instead.
30 /// [`non_ascii_idents`]: https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#non-ascii-idents
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.
41 #[clippy::version = "1.55.0"]
42 pub DISALLOWED_SCRIPT_IDENTS,
44 "usage of non-allowed Unicode scripts"
47 #[derive(Clone, Debug)]
48 pub struct DisallowedScriptIdents {
49 whitelist: FxHashSet<Script>,
52 impl DisallowedScriptIdents {
53 pub fn new(whitelist: &[String]) -> Self {
54 let whitelist = whitelist
57 .filter_map(Script::from_full_name)
63 impl_lint_pass!(DisallowedScriptIdents => [DISALLOWED_SCRIPT_IDENTS]);
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
70 let check_disallowed_script_idents = cx.builder.lint_level(DISALLOWED_SCRIPT_IDENTS).0 != Level::Allow;
71 if !check_disallowed_script_idents {
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);
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() {
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
95 .find(|script| !self.whitelist.contains(script));
96 if let Some(script) = forbidden_script {
99 DISALLOWED_SCRIPT_IDENTS,
102 "identifier `{}` has a Unicode script that is not allowed by configuration: {}",
107 // We don't want to spawn warning multiple times over a single identifier.