1 use crate::{EarlyContext, EarlyLintPass, LintContext};
3 use rustc_data_structures::fx::FxHashMap;
4 use rustc_errors::fluent;
5 use rustc_span::symbol::Symbol;
8 /// The `non_ascii_idents` lint detects non-ASCII identifiers.
12 /// ```rust,compile_fail
13 /// # #![allow(unused)]
14 /// #![deny(non_ascii_idents)]
24 /// This lint allows projects that wish to retain the limit of only using
25 /// ASCII characters to switch this lint to "forbid" (for example to ease
26 /// collaboration or for security reasons).
27 /// See [RFC 2457] for more details.
29 /// [RFC 2457]: https://github.com/rust-lang/rfcs/blob/master/text/2457-non-ascii-idents.md
32 "detects non-ASCII identifiers",
37 /// The `uncommon_codepoints` lint detects uncommon Unicode codepoints in
43 /// # #![allow(unused)]
44 /// const µ: f64 = 0.000001;
51 /// This lint warns about using characters which are not commonly used, and may
52 /// cause visual confusion.
54 /// This lint is triggered by identifiers that contain a codepoint that is
55 /// not part of the set of "Allowed" codepoints as described by [Unicode®
56 /// Technical Standard #39 Unicode Security Mechanisms Section 3.1 General
57 /// Security Profile for Identifiers][TR39Allowed].
59 /// Note that the set of uncommon codepoints may change over time. Beware
60 /// that if you "forbid" this lint that existing code may fail in the
63 /// [TR39Allowed]: https://www.unicode.org/reports/tr39/#General_Security_Profile
64 pub UNCOMMON_CODEPOINTS,
66 "detects uncommon Unicode codepoints in identifiers",
71 /// The `confusable_idents` lint detects visually confusable pairs between
77 /// // Latin Capital Letter E With Caron
78 /// pub const Ě: i32 = 1;
79 /// // Latin Capital Letter E With Breve
80 /// pub const Ĕ: i32 = 2;
87 /// This lint warns when different identifiers may appear visually similar,
88 /// which can cause confusion.
90 /// The confusable detection algorithm is based on [Unicode® Technical
91 /// Standard #39 Unicode Security Mechanisms Section 4 Confusable
92 /// Detection][TR39Confusable]. For every distinct identifier X execute
93 /// the function `skeleton(X)`. If there exist two distinct identifiers X
94 /// and Y in the same crate where `skeleton(X) = skeleton(Y)` report it.
95 /// The compiler uses the same mechanism to check if an identifier is too
96 /// similar to a keyword.
98 /// Note that the set of confusable characters may change over time.
99 /// Beware that if you "forbid" this lint that existing code may fail in
102 /// [TR39Confusable]: https://www.unicode.org/reports/tr39/#Confusable_Detection
103 pub CONFUSABLE_IDENTS,
105 "detects visually confusable pairs between identifiers",
110 /// The `mixed_script_confusables` lint detects visually confusable
111 /// characters in identifiers between different [scripts].
113 /// [scripts]: https://en.wikipedia.org/wiki/Script_(Unicode)
118 /// // The Japanese katakana character エ can be confused with the Han character 工.
119 /// const エ: &'static str = "アイウ";
126 /// This lint warns when characters between different scripts may appear
127 /// visually similar, which can cause confusion.
129 /// If the crate contains other identifiers in the same script that have
130 /// non-confusable characters, then this lint will *not* be issued. For
131 /// example, if the example given above has another identifier with
132 /// katakana characters (such as `let カタカナ = 123;`), then this indicates
133 /// that you are intentionally using katakana, and it will not warn about
136 /// Note that the set of confusable characters may change over time.
137 /// Beware that if you "forbid" this lint that existing code may fail in
139 pub MIXED_SCRIPT_CONFUSABLES,
141 "detects Unicode scripts whose mixed script confusables codepoints are solely used",
145 declare_lint_pass!(NonAsciiIdents => [NON_ASCII_IDENTS, UNCOMMON_CODEPOINTS, CONFUSABLE_IDENTS, MIXED_SCRIPT_CONFUSABLES]);
147 impl EarlyLintPass for NonAsciiIdents {
148 fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &ast::Crate) {
149 use rustc_session::lint::Level;
150 use rustc_span::Span;
151 use std::collections::BTreeMap;
152 use unicode_security::GeneralSecurityProfile;
154 let check_non_ascii_idents = cx.builder.lint_level(NON_ASCII_IDENTS).0 != Level::Allow;
155 let check_uncommon_codepoints =
156 cx.builder.lint_level(UNCOMMON_CODEPOINTS).0 != Level::Allow;
157 let check_confusable_idents = cx.builder.lint_level(CONFUSABLE_IDENTS).0 != Level::Allow;
158 let check_mixed_script_confusables =
159 cx.builder.lint_level(MIXED_SCRIPT_CONFUSABLES).0 != Level::Allow;
161 if !check_non_ascii_idents
162 && !check_uncommon_codepoints
163 && !check_confusable_idents
164 && !check_mixed_script_confusables
169 let mut has_non_ascii_idents = false;
170 let symbols = cx.sess().parse_sess.symbol_gallery.symbols.lock();
172 // Sort by `Span` so that error messages make sense with respect to the
173 // order of identifier locations in the code.
174 let mut symbols: Vec<_> = symbols.iter().collect();
175 symbols.sort_by_key(|k| k.1);
177 for (symbol, &sp) in symbols.iter() {
178 let symbol_str = symbol.as_str();
179 if symbol_str.is_ascii() {
182 has_non_ascii_idents = true;
186 fluent::lint_identifier_non_ascii_char,
189 if check_uncommon_codepoints
190 && !symbol_str.chars().all(GeneralSecurityProfile::identifier_allowed)
195 fluent::lint_identifier_uncommon_codepoints,
201 if has_non_ascii_idents && check_confusable_idents {
202 let mut skeleton_map: FxHashMap<Symbol, (Symbol, Span, bool)> =
203 FxHashMap::with_capacity_and_hasher(symbols.len(), Default::default());
204 let mut skeleton_buf = String::new();
206 for (&symbol, &sp) in symbols.iter() {
207 use unicode_security::confusable_detection::skeleton;
209 let symbol_str = symbol.as_str();
210 let is_ascii = symbol_str.is_ascii();
212 // Get the skeleton as a `Symbol`.
213 skeleton_buf.clear();
214 skeleton_buf.extend(skeleton(&symbol_str));
215 let skeleton_sym = if *symbol_str == *skeleton_buf {
218 Symbol::intern(&skeleton_buf)
223 .and_modify(|(existing_symbol, existing_span, existing_is_ascii)| {
224 if !*existing_is_ascii || !is_ascii {
228 fluent::lint_confusable_identifier_pair,
230 lint.set_arg("existing_sym", *existing_symbol)
231 .set_arg("sym", symbol)
232 .span_label(*existing_span, fluent::label)
236 if *existing_is_ascii && !is_ascii {
237 *existing_symbol = symbol;
239 *existing_is_ascii = is_ascii;
242 .or_insert((symbol, sp, is_ascii));
246 if has_non_ascii_idents && check_mixed_script_confusables {
247 use unicode_security::is_potential_mixed_script_confusable_char;
248 use unicode_security::mixed_script::AugmentedScriptSet;
251 enum ScriptSetUsage {
252 Suspicious(Vec<char>, Span),
256 let mut script_states: FxHashMap<AugmentedScriptSet, ScriptSetUsage> =
257 FxHashMap::default();
258 let latin_augmented_script_set = AugmentedScriptSet::for_char('A');
259 script_states.insert(latin_augmented_script_set, ScriptSetUsage::Verified);
261 let mut has_suspicous = false;
262 for (symbol, &sp) in symbols.iter() {
263 let symbol_str = symbol.as_str();
264 for ch in symbol_str.chars() {
266 // all ascii characters are covered by exception.
269 if !GeneralSecurityProfile::identifier_allowed(ch) {
270 // this character is covered by `uncommon_codepoints` lint.
273 let augmented_script_set = AugmentedScriptSet::for_char(ch);
275 .entry(augmented_script_set)
276 .and_modify(|existing_state| {
277 if let ScriptSetUsage::Suspicious(ch_list, _) = existing_state {
278 if is_potential_mixed_script_confusable_char(ch) {
281 *existing_state = ScriptSetUsage::Verified;
286 if !is_potential_mixed_script_confusable_char(ch) {
287 ScriptSetUsage::Verified
289 has_suspicous = true;
290 ScriptSetUsage::Suspicious(vec![ch], sp)
297 let verified_augmented_script_sets = script_states
299 .flat_map(|(k, v)| match v {
300 ScriptSetUsage::Verified => Some(*k),
303 .collect::<Vec<_>>();
305 // we're sorting the output here.
306 let mut lint_reports: BTreeMap<(Span, Vec<char>), AugmentedScriptSet> =
309 'outerloop: for (augment_script_set, usage) in script_states {
310 let ScriptSetUsage::Suspicious(mut ch_list, sp) = usage else { continue };
312 if augment_script_set.is_all() {
316 for existing in verified_augmented_script_sets.iter() {
317 if existing.is_all() {
320 let mut intersect = *existing;
321 intersect.intersect_with(augment_script_set);
322 if !intersect.is_empty() && !intersect.is_all() {
327 // We sort primitive chars here and can use unstable sort
328 ch_list.sort_unstable();
330 lint_reports.insert((sp, ch_list), augment_script_set);
333 for ((sp, ch_list), script_set) in lint_reports {
335 MIXED_SCRIPT_CONFUSABLES,
337 fluent::lint_mixed_script_confusables,
339 let mut includes = String::new();
340 for (idx, ch) in ch_list.into_iter().enumerate() {
344 let char_info = format!("'{}' (U+{:04X})", ch, ch as u32);
345 includes += &char_info;
347 lint.set_arg("set", script_set.to_string())
348 .set_arg("includes", includes)
349 .note(fluent::includes_note)