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:** This lint 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 sorts of April
11 /// 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:** This lint checks for non-ASCII characters in string literals.
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.
28 /// **Known problems:** None
35 pub NON_ASCII_LITERAL, Allow,
36 "using any literal non-ASCII chars in a string literal; suggests \
37 using the `\\u` escape instead"
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).
43 /// **Why is this bad?** If such a string is compared to another, the results may be surprising.
45 /// **Known problems** None
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}"`.
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)"
56 #[derive(Copy, Clone)]
59 impl LintPass for Unicode {
60 fn get_lints(&self) -> LintArray {
61 lint_array!(ZERO_WIDTH_SPACE, NON_ASCII_LITERAL, UNICODE_NOT_NFC)
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)
75 fn escape<T: Iterator<Item = char>>(s: T) -> String {
76 let mut result = String::new();
79 for d in c.escape_unicode() {
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,
95 "zero-width space detected",
96 &format!("Consider replacing the string with:\n\"{}\"",
97 string.replace("\u{200B}", "\\u{200B}")));
99 if string.chars().any(|c| c as u32 > 0x7F) {
100 span_help_and_lint(cx,
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())
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,
115 "non-nfc unicode sequence detected",
116 &format!("Consider replacing the string with:\n\"{}\"", string.nfc().collect::<String>()));