]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/unicode.rs
Rollup merge of #90741 - mbartlett21:patch-4, r=dtolnay
[rust.git] / src / tools / clippy / clippy_lints / src / unicode.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::is_lint_allowed;
3 use clippy_utils::source::snippet;
4 use rustc_ast::ast::LitKind;
5 use rustc_errors::Applicability;
6 use rustc_hir::{Expr, ExprKind, HirId};
7 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
9 use rustc_span::source_map::Span;
10 use unicode_normalization::UnicodeNormalization;
11
12 declare_clippy_lint! {
13     /// ### What it does
14     /// Checks for invisible Unicode characters in the code.
15     ///
16     /// ### Why is this bad?
17     /// Having an invisible character in the code makes for all
18     /// sorts of April fools, but otherwise is very much frowned upon.
19     ///
20     /// ### Example
21     /// You don't see it, but there may be a zero-width space or soft hyphen
22     /// some­where in this text.
23     #[clippy::version = "1.49.0"]
24     pub INVISIBLE_CHARACTERS,
25     correctness,
26     "using an invisible character in a string literal, which is confusing"
27 }
28
29 declare_clippy_lint! {
30     /// ### What it does
31     /// Checks for non-ASCII characters in string and char literals.
32     ///
33     /// ### Why is this bad?
34     /// Yeah, we know, the 90's called and wanted their charset
35     /// back. Even so, there still are editors and other programs out there that
36     /// don't work well with Unicode. So if the code is meant to be used
37     /// internationally, on multiple operating systems, or has other portability
38     /// requirements, activating this lint could be useful.
39     ///
40     /// ### Example
41     /// ```rust
42     /// let x = String::from("€");
43     /// ```
44     /// Could be written as:
45     /// ```rust
46     /// let x = String::from("\u{20ac}");
47     /// ```
48     #[clippy::version = "pre 1.29.0"]
49     pub NON_ASCII_LITERAL,
50     restriction,
51     "using any literal non-ASCII chars in a string literal instead of using the `\\u` escape"
52 }
53
54 declare_clippy_lint! {
55     /// ### What it does
56     /// Checks for string literals that contain Unicode in a form
57     /// that is not equal to its
58     /// [NFC-recomposition](http://www.unicode.org/reports/tr15/#Norm_Forms).
59     ///
60     /// ### Why is this bad?
61     /// If such a string is compared to another, the results
62     /// may be surprising.
63     ///
64     /// ### Example
65     /// You may not see it, but "à"" and "à"" aren't the same string. The
66     /// former when escaped is actually `"a\u{300}"` while the latter is `"\u{e0}"`.
67     #[clippy::version = "pre 1.29.0"]
68     pub UNICODE_NOT_NFC,
69     pedantic,
70     "using a Unicode literal not in NFC normal form (see [Unicode tr15](http://www.unicode.org/reports/tr15/) for further information)"
71 }
72
73 declare_lint_pass!(Unicode => [INVISIBLE_CHARACTERS, NON_ASCII_LITERAL, UNICODE_NOT_NFC]);
74
75 impl LateLintPass<'_> for Unicode {
76     fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
77         if let ExprKind::Lit(ref lit) = expr.kind {
78             if let LitKind::Str(_, _) | LitKind::Char(_) = lit.node {
79                 check_str(cx, lit.span, expr.hir_id);
80             }
81         }
82     }
83 }
84
85 fn escape<T: Iterator<Item = char>>(s: T) -> String {
86     let mut result = String::new();
87     for c in s {
88         if c as u32 > 0x7F {
89             for d in c.escape_unicode() {
90                 result.push(d);
91             }
92         } else {
93             result.push(c);
94         }
95     }
96     result
97 }
98
99 fn check_str(cx: &LateContext<'_>, span: Span, id: HirId) {
100     let string = snippet(cx, span, "");
101     if string.chars().any(|c| ['\u{200B}', '\u{ad}', '\u{2060}'].contains(&c)) {
102         span_lint_and_sugg(
103             cx,
104             INVISIBLE_CHARACTERS,
105             span,
106             "invisible character detected",
107             "consider replacing the string with",
108             string
109                 .replace('\u{200B}', "\\u{200B}")
110                 .replace('\u{ad}', "\\u{AD}")
111                 .replace('\u{2060}', "\\u{2060}"),
112             Applicability::MachineApplicable,
113         );
114     }
115     if string.chars().any(|c| c as u32 > 0x7F) {
116         span_lint_and_sugg(
117             cx,
118             NON_ASCII_LITERAL,
119             span,
120             "literal non-ASCII character detected",
121             "consider replacing the string with",
122             if is_lint_allowed(cx, UNICODE_NOT_NFC, id) {
123                 escape(string.chars())
124             } else {
125                 escape(string.nfc())
126             },
127             Applicability::MachineApplicable,
128         );
129     }
130     if is_lint_allowed(cx, NON_ASCII_LITERAL, id) && string.chars().zip(string.nfc()).any(|(a, b)| a != b) {
131         span_lint_and_sugg(
132             cx,
133             UNICODE_NOT_NFC,
134             span,
135             "non-NFC Unicode sequence detected",
136             "consider replacing the string with",
137             string.nfc().collect::<String>(),
138             Applicability::MachineApplicable,
139         );
140     }
141 }