2 ConfusableIdentifierPair, IdentifierNonAsciiChar, IdentifierUncommonCodepoints,
3 MixedScriptConfusables,
5 use crate::{EarlyContext, EarlyLintPass, LintContext};
7 use rustc_data_structures::fx::FxHashMap;
8 use rustc_span::symbol::Symbol;
11 /// The `non_ascii_idents` lint detects non-ASCII identifiers.
15 /// ```rust,compile_fail
16 /// # #![allow(unused)]
17 /// #![deny(non_ascii_idents)]
27 /// This lint allows projects that wish to retain the limit of only using
28 /// ASCII characters to switch this lint to "forbid" (for example to ease
29 /// collaboration or for security reasons).
30 /// See [RFC 2457] for more details.
32 /// [RFC 2457]: https://github.com/rust-lang/rfcs/blob/master/text/2457-non-ascii-idents.md
35 "detects non-ASCII identifiers",
40 /// The `uncommon_codepoints` lint detects uncommon Unicode codepoints in
46 /// # #![allow(unused)]
47 /// const µ: f64 = 0.000001;
54 /// This lint warns about using characters which are not commonly used, and may
55 /// cause visual confusion.
57 /// This lint is triggered by identifiers that contain a codepoint that is
58 /// not part of the set of "Allowed" codepoints as described by [Unicode®
59 /// Technical Standard #39 Unicode Security Mechanisms Section 3.1 General
60 /// Security Profile for Identifiers][TR39Allowed].
62 /// Note that the set of uncommon codepoints may change over time. Beware
63 /// that if you "forbid" this lint that existing code may fail in the
66 /// [TR39Allowed]: https://www.unicode.org/reports/tr39/#General_Security_Profile
67 pub UNCOMMON_CODEPOINTS,
69 "detects uncommon Unicode codepoints in identifiers",
74 /// The `confusable_idents` lint detects visually confusable pairs between
80 /// // Latin Capital Letter E With Caron
81 /// pub const Ě: i32 = 1;
82 /// // Latin Capital Letter E With Breve
83 /// pub const Ĕ: i32 = 2;
90 /// This lint warns when different identifiers may appear visually similar,
91 /// which can cause confusion.
93 /// The confusable detection algorithm is based on [Unicode® Technical
94 /// Standard #39 Unicode Security Mechanisms Section 4 Confusable
95 /// Detection][TR39Confusable]. For every distinct identifier X execute
96 /// the function `skeleton(X)`. If there exist two distinct identifiers X
97 /// and Y in the same crate where `skeleton(X) = skeleton(Y)` report it.
98 /// The compiler uses the same mechanism to check if an identifier is too
99 /// similar to a keyword.
101 /// Note that the set of confusable characters may change over time.
102 /// Beware that if you "forbid" this lint that existing code may fail in
105 /// [TR39Confusable]: https://www.unicode.org/reports/tr39/#Confusable_Detection
106 pub CONFUSABLE_IDENTS,
108 "detects visually confusable pairs between identifiers",
113 /// The `mixed_script_confusables` lint detects visually confusable
114 /// characters in identifiers between different [scripts].
116 /// [scripts]: https://en.wikipedia.org/wiki/Script_(Unicode)
121 /// // The Japanese katakana character エ can be confused with the Han character 工.
122 /// const エ: &'static str = "アイウ";
129 /// This lint warns when characters between different scripts may appear
130 /// visually similar, which can cause confusion.
132 /// If the crate contains other identifiers in the same script that have
133 /// non-confusable characters, then this lint will *not* be issued. For
134 /// example, if the example given above has another identifier with
135 /// katakana characters (such as `let カタカナ = 123;`), then this indicates
136 /// that you are intentionally using katakana, and it will not warn about
139 /// Note that the set of confusable characters may change over time.
140 /// Beware that if you "forbid" this lint that existing code may fail in
142 pub MIXED_SCRIPT_CONFUSABLES,
144 "detects Unicode scripts whose mixed script confusables codepoints are solely used",
148 declare_lint_pass!(NonAsciiIdents => [NON_ASCII_IDENTS, UNCOMMON_CODEPOINTS, CONFUSABLE_IDENTS, MIXED_SCRIPT_CONFUSABLES]);
150 impl EarlyLintPass for NonAsciiIdents {
151 fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &ast::Crate) {
152 use rustc_session::lint::Level;
153 use rustc_span::Span;
154 use std::collections::BTreeMap;
155 use unicode_security::GeneralSecurityProfile;
157 let check_non_ascii_idents = cx.builder.lint_level(NON_ASCII_IDENTS).0 != Level::Allow;
158 let check_uncommon_codepoints =
159 cx.builder.lint_level(UNCOMMON_CODEPOINTS).0 != Level::Allow;
160 let check_confusable_idents = cx.builder.lint_level(CONFUSABLE_IDENTS).0 != Level::Allow;
161 let check_mixed_script_confusables =
162 cx.builder.lint_level(MIXED_SCRIPT_CONFUSABLES).0 != Level::Allow;
164 if !check_non_ascii_idents
165 && !check_uncommon_codepoints
166 && !check_confusable_idents
167 && !check_mixed_script_confusables
172 let mut has_non_ascii_idents = false;
173 let symbols = cx.sess().parse_sess.symbol_gallery.symbols.lock();
175 // Sort by `Span` so that error messages make sense with respect to the
176 // order of identifier locations in the code.
177 let mut symbols: Vec<_> = symbols.iter().collect();
178 symbols.sort_by_key(|k| k.1);
180 for (symbol, &sp) in symbols.iter() {
181 let symbol_str = symbol.as_str();
182 if symbol_str.is_ascii() {
185 has_non_ascii_idents = true;
186 cx.emit_spanned_lint(NON_ASCII_IDENTS, sp, IdentifierNonAsciiChar);
187 if check_uncommon_codepoints
188 && !symbol_str.chars().all(GeneralSecurityProfile::identifier_allowed)
190 cx.emit_spanned_lint(UNCOMMON_CODEPOINTS, sp, IdentifierUncommonCodepoints);
194 if has_non_ascii_idents && check_confusable_idents {
195 let mut skeleton_map: FxHashMap<Symbol, (Symbol, Span, bool)> =
196 FxHashMap::with_capacity_and_hasher(symbols.len(), Default::default());
197 let mut skeleton_buf = String::new();
199 for (&symbol, &sp) in symbols.iter() {
200 use unicode_security::confusable_detection::skeleton;
202 let symbol_str = symbol.as_str();
203 let is_ascii = symbol_str.is_ascii();
205 // Get the skeleton as a `Symbol`.
206 skeleton_buf.clear();
207 skeleton_buf.extend(skeleton(&symbol_str));
208 let skeleton_sym = if *symbol_str == *skeleton_buf {
211 Symbol::intern(&skeleton_buf)
216 .and_modify(|(existing_symbol, existing_span, existing_is_ascii)| {
217 if !*existing_is_ascii || !is_ascii {
218 cx.emit_spanned_lint(
221 ConfusableIdentifierPair {
222 existing_sym: *existing_symbol,
224 label: *existing_span,
228 if *existing_is_ascii && !is_ascii {
229 *existing_symbol = symbol;
231 *existing_is_ascii = is_ascii;
234 .or_insert((symbol, sp, is_ascii));
238 if has_non_ascii_idents && check_mixed_script_confusables {
239 use unicode_security::is_potential_mixed_script_confusable_char;
240 use unicode_security::mixed_script::AugmentedScriptSet;
243 enum ScriptSetUsage {
244 Suspicious(Vec<char>, Span),
248 let mut script_states: FxHashMap<AugmentedScriptSet, ScriptSetUsage> =
249 FxHashMap::default();
250 let latin_augmented_script_set = AugmentedScriptSet::for_char('A');
251 script_states.insert(latin_augmented_script_set, ScriptSetUsage::Verified);
253 let mut has_suspicous = false;
254 for (symbol, &sp) in symbols.iter() {
255 let symbol_str = symbol.as_str();
256 for ch in symbol_str.chars() {
258 // all ascii characters are covered by exception.
261 if !GeneralSecurityProfile::identifier_allowed(ch) {
262 // this character is covered by `uncommon_codepoints` lint.
265 let augmented_script_set = AugmentedScriptSet::for_char(ch);
267 .entry(augmented_script_set)
268 .and_modify(|existing_state| {
269 if let ScriptSetUsage::Suspicious(ch_list, _) = existing_state {
270 if is_potential_mixed_script_confusable_char(ch) {
273 *existing_state = ScriptSetUsage::Verified;
278 if !is_potential_mixed_script_confusable_char(ch) {
279 ScriptSetUsage::Verified
281 has_suspicous = true;
282 ScriptSetUsage::Suspicious(vec![ch], sp)
289 let verified_augmented_script_sets = script_states
291 .flat_map(|(k, v)| match v {
292 ScriptSetUsage::Verified => Some(*k),
295 .collect::<Vec<_>>();
297 // we're sorting the output here.
298 let mut lint_reports: BTreeMap<(Span, Vec<char>), AugmentedScriptSet> =
301 'outerloop: for (augment_script_set, usage) in script_states {
302 let ScriptSetUsage::Suspicious(mut ch_list, sp) = usage else { continue };
304 if augment_script_set.is_all() {
308 for existing in verified_augmented_script_sets.iter() {
309 if existing.is_all() {
312 let mut intersect = *existing;
313 intersect.intersect_with(augment_script_set);
314 if !intersect.is_empty() && !intersect.is_all() {
319 // We sort primitive chars here and can use unstable sort
320 ch_list.sort_unstable();
322 lint_reports.insert((sp, ch_list), augment_script_set);
325 for ((sp, ch_list), script_set) in lint_reports {
326 let mut includes = String::new();
327 for (idx, ch) in ch_list.into_iter().enumerate() {
331 let char_info = format!("'{}' (U+{:04X})", ch, ch as u32);
332 includes += &char_info;
334 cx.emit_spanned_lint(
335 MIXED_SCRIPT_CONFUSABLES,
337 MixedScriptConfusables { set: script_set.to_string(), includes },