3 use syntax::ast::LitKind;
4 use syntax::codemap::Span;
5 use unicode_normalization::UnicodeNormalization;
6 use utils::{snippet, span_help_and_lint};
8 /// **What it does:** Checks for the Unicode zero-width space in the code.
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.
13 /// **Known problems:** None.
15 /// **Example:** You don't see it, but there may be a zero-width space somewhere in this text.
17 pub ZERO_WIDTH_SPACE, Deny,
18 "using a zero-width space in a string literal, which is confusing"
21 /// **What it does:** Checks for non-ASCII characters in string literals.
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.
29 /// **Known problems:** None.
36 pub NON_ASCII_LITERAL, Allow,
37 "using any literal non-ASCII chars in a string literal; suggests \
38 using the `\\u` escape instead"
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).
45 /// **Why is this bad?** If such a string is compared to another, the results
46 /// may be surprising.
48 /// **Known problems** None.
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}"`.
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)"
59 #[derive(Copy, Clone)]
62 impl LintPass for Unicode {
63 fn get_lints(&self) -> LintArray {
64 lint_array!(ZERO_WIDTH_SPACE, NON_ASCII_LITERAL, UNICODE_NOT_NFC)
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)
78 fn escape<T: Iterator<Item = char>>(s: T) -> String {
79 let mut result = String::new();
82 for d in c.escape_unicode() {
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,
98 "zero-width space detected",
99 &format!("Consider replacing the string with:\n\"{}\"",
100 string.replace("\u{200B}", "\\u{200B}")));
102 if string.chars().any(|c| c as u32 > 0x7F) {
103 span_help_and_lint(cx,
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())
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,
118 "non-nfc unicode sequence detected",
119 &format!("Consider replacing the string with:\n\"{}\"", string.nfc().collect::<String>()));