Skip to content

Commit 7910dc9

Browse files
committed
Reimplement lint using EarlyLintPass::check_crate
1 parent d08545c commit 7910dc9

File tree

2 files changed

+39
-27
lines changed

2 files changed

+39
-27
lines changed

clippy_lints/src/disallowed_script_idents.rs

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint;
2+
use rustc_ast::ast;
23
use rustc_data_structures::fx::FxHashSet;
3-
use rustc_hir::{Pat, PatKind};
4-
use rustc_lint::{LateContext, LateLintPass};
4+
use rustc_lint::{EarlyContext, EarlyLintPass, Level};
55
use rustc_session::{declare_tool_lint, impl_lint_pass};
66
use unicode_script::{Script, UnicodeScript};
77

@@ -59,34 +59,46 @@ impl DisallowedScriptIdents {
5959

6060
impl_lint_pass!(DisallowedScriptIdents => [DISALLOWED_SCRIPT_IDENTS]);
6161

62-
impl<'tcx> LateLintPass<'tcx> for DisallowedScriptIdents {
63-
fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
64-
let ident = if let PatKind::Binding(.., ident, _) = pat.kind {
65-
ident
66-
} else {
67-
return;
68-
};
62+
impl EarlyLintPass for DisallowedScriptIdents {
63+
fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &ast::Crate) {
64+
// Implementation is heavily inspired by the implementation of [`non_ascii_idents`] lint:
65+
// https://github.com/rust-lang/rust/blob/master/compiler/rustc_lint/src/non_ascii_idents.rs
66+
67+
let check_disallowed_script_idents = cx.builder.lint_level(DISALLOWED_SCRIPT_IDENTS).0 != Level::Allow;
6968

70-
if ident.as_str().is_ascii() {
71-
// Fully-ASCII identifiers are valid by default, no need to analyze them further.
69+
if !check_disallowed_script_idents {
7270
return;
7371
}
7472

75-
for symbol in ident.name.as_str().chars() {
76-
let script = symbol.script();
77-
if !self.whitelist.contains(&script) {
78-
span_lint(
79-
cx,
80-
DISALLOWED_SCRIPT_IDENTS,
81-
ident.span,
82-
&format!(
83-
"identifier `{}` has a Unicode script that is not allowed by configuration: {}",
84-
ident.name,
85-
script.full_name()
86-
),
87-
);
88-
// We don't want to spawn warning multiple times over a single identifier.
89-
break;
73+
let symbols = cx.sess.parse_sess.symbol_gallery.symbols.lock();
74+
75+
// Sort by `Span` so that error messages make sense with respect to the
76+
// order of identifier locations in the code.
77+
let mut symbols: Vec<_> = symbols.iter().collect();
78+
symbols.sort_unstable_by_key(|k| k.1);
79+
80+
for (symbol, &span) in &symbols {
81+
let symbol_str = symbol.as_str();
82+
if symbol_str.is_ascii() {
83+
continue;
84+
}
85+
86+
for c in symbol_str.chars() {
87+
let script = c.script();
88+
if !self.whitelist.contains(&script) {
89+
span_lint(
90+
cx,
91+
DISALLOWED_SCRIPT_IDENTS,
92+
span,
93+
&format!(
94+
"identifier `{}` has a Unicode script that is not allowed by configuration: {}",
95+
symbol_str,
96+
script.full_name()
97+
),
98+
);
99+
// We don't want to spawn warning multiple times over a single identifier.
100+
break;
101+
}
90102
}
91103
}
92104
}

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2086,7 +2086,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
20862086
let import_renames = conf.enforced_import_renames.clone();
20872087
store.register_late_pass(move || box missing_enforced_import_rename::ImportRename::new(import_renames.clone()));
20882088
let scripts = conf.allowed_scripts.clone();
2089-
store.register_late_pass(move || box disallowed_script_idents::DisallowedScriptIdents::new(scripts.clone()));
2089+
store.register_early_pass(move || box disallowed_script_idents::DisallowedScriptIdents::new(scripts.clone()));
20902090
}
20912091

20922092
#[rustfmt::skip]

0 commit comments

Comments
 (0)