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.
19 "using a zero-width space in a string literal, which is confusing"
22 /// **What it does:** Checks for non-ASCII characters in string literals.
24 /// **Why is this bad?** Yeah, we know, the 90's called and wanted their charset
25 /// back. Even so, there still are editors and other programs out there that
26 /// don't work well with Unicode. So if the code is meant to be used
27 /// internationally, on multiple operating systems, or has other portability
28 /// requirements, activating this lint could be useful.
30 /// **Known problems:** None.
37 pub NON_ASCII_LITERAL,
39 "using any literal non-ASCII chars in a string literal instead of \
40 using the `\\u` escape"
43 /// **What it does:** Checks for string literals that contain Unicode in a form
44 /// that is not equal to its
45 /// [NFC-recomposition](http://www.unicode.org/reports/tr15/#Norm_Forms).
47 /// **Why is this bad?** If such a string is compared to another, the results
48 /// may be surprising.
50 /// **Known problems** None.
52 /// **Example:** You may not see it, but “à” and “à” aren't the same string. The
53 /// former when escaped is actually `"a\u{300}"` while the latter is `"\u{e0}"`.
57 "using a unicode literal not in NFC normal form (see \
58 [unicode tr15](http://www.unicode.org/reports/tr15/) for further information)"
62 #[derive(Copy, Clone)]
65 impl LintPass for Unicode {
66 fn get_lints(&self) -> LintArray {
67 lint_array!(ZERO_WIDTH_SPACE, NON_ASCII_LITERAL, UNICODE_NOT_NFC)
71 impl LateLintPass for Unicode {
72 fn check_expr<'a, 'tcx: 'a>(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
73 if let ExprLit(ref lit) = expr.node {
74 if let LitKind::Str(_, _) = lit.node {
75 check_str(cx, lit.span)
81 fn escape<T: Iterator<Item = char>>(s: T) -> String {
82 let mut result = String::new();
85 for d in c.escape_unicode() {
95 fn check_str(cx: &LateContext, span: Span) {
96 let string = snippet(cx, span, "");
97 if string.contains('\u{200B}') {
98 span_help_and_lint(cx,
101 "zero-width space detected",
102 &format!("Consider replacing the string with:\n\"{}\"",
103 string.replace("\u{200B}", "\\u{200B}")));
105 if string.chars().any(|c| c as u32 > 0x7F) {
106 span_help_and_lint(cx,
109 "literal non-ASCII character detected",
110 &format!("Consider replacing the string with:\n\"{}\"",
111 if cx.current_level(UNICODE_NOT_NFC) == Level::Allow {
112 escape(string.chars())
117 if cx.current_level(NON_ASCII_LITERAL) == Level::Allow && string.chars().zip(string.nfc()).any(|(a, b)| a != b) {
118 span_help_and_lint(cx,
121 "non-nfc unicode sequence detected",
122 &format!("Consider replacing the string with:\n\"{}\"", string.nfc().collect::<String>()));