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