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