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