2 use rustc::{declare_lint, lint_array};
4 use syntax::ast::{LitKind, NodeId};
5 use syntax::codemap::Span;
6 use unicode_normalization::UnicodeNormalization;
7 use crate::utils::{is_allowed, snippet, span_help_and_lint};
9 /// **What it does:** Checks for the Unicode zero-width space in the code.
11 /// **Why is this bad?** Having an invisible character in the code makes for all
12 /// sorts of April fools, but otherwise is very much frowned upon.
14 /// **Known problems:** None.
16 /// **Example:** You don't see it, but there may be a zero-width space
17 /// somewhere in this text.
18 declare_clippy_lint! {
21 "using a zero-width space in a string literal, which is confusing"
24 /// **What it does:** Checks for non-ASCII characters in string literals.
26 /// **Why is this bad?** Yeah, we know, the 90's called and wanted their charset
27 /// back. Even so, there still are editors and other programs out there that
28 /// don't work well with Unicode. So if the code is meant to be used
29 /// internationally, on multiple operating systems, or has other portability
30 /// requirements, activating this lint could be useful.
32 /// **Known problems:** None.
38 declare_clippy_lint! {
39 pub NON_ASCII_LITERAL,
41 "using any literal non-ASCII chars in a string literal instead of \
42 using the `\\u` escape"
45 /// **What it does:** Checks for string literals that contain Unicode in a form
46 /// that is not equal to its
47 /// [NFC-recomposition](http://www.unicode.org/reports/tr15/#Norm_Forms).
49 /// **Why is this bad?** If such a string is compared to another, the results
50 /// may be surprising.
52 /// **Known problems** None.
54 /// **Example:** You may not see it, but “à” and “à” aren't the same string. The
55 /// former when escaped is actually `"a\u{300}"` while the latter is `"\u{e0}"`.
56 declare_clippy_lint! {
59 "using a unicode literal not in NFC normal form (see \
60 [unicode tr15](http://www.unicode.org/reports/tr15/) for further information)"
64 #[derive(Copy, Clone)]
67 impl LintPass for Unicode {
68 fn get_lints(&self) -> LintArray {
69 lint_array!(ZERO_WIDTH_SPACE, NON_ASCII_LITERAL, UNICODE_NOT_NFC)
73 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Unicode {
74 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
75 if let ExprKind::Lit(ref lit) = expr.node {
76 if let LitKind::Str(_, _) = lit.node {
77 check_str(cx, lit.span, expr.id)
83 fn escape<T: Iterator<Item = char>>(s: T) -> String {
84 let mut result = String::new();
87 for d in c.escape_unicode() {
97 fn check_str(cx: &LateContext, span: Span, id: NodeId) {
98 let string = snippet(cx, span, "");
99 if string.contains('\u{200B}') {
104 "zero-width space detected",
106 "Consider replacing the string with:\n\"{}\"",
107 string.replace("\u{200B}", "\\u{200B}")
111 if string.chars().any(|c| c as u32 > 0x7F) {
116 "literal non-ASCII character detected",
118 "Consider replacing the string with:\n\"{}\"",
119 if is_allowed(cx, UNICODE_NOT_NFC, id) {
120 escape(string.chars())
127 if is_allowed(cx, NON_ASCII_LITERAL, id) && string.chars().zip(string.nfc()).any(|(a, b)| a != b) {
132 "non-nfc unicode sequence detected",
133 &format!("Consider replacing the string with:\n\"{}\"", string.nfc().collect::<String>()),