3 use syntax::ast::{LitKind, NodeId};
4 use syntax::codemap::Span;
5 use unicode_normalization::UnicodeNormalization;
6 use crate::utils::{is_allowed, 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
16 /// somewhere in this text.
17 declare_clippy_lint! {
20 "using a zero-width space in a string literal, which is confusing"
23 /// **What it does:** Checks for non-ASCII characters in string literals.
25 /// **Why is this bad?** Yeah, we know, the 90's called and wanted their charset
26 /// back. Even so, there still are editors and other programs out there that
27 /// don't work well with Unicode. So if the code is meant to be used
28 /// internationally, on multiple operating systems, or has other portability
29 /// requirements, activating this lint could be useful.
31 /// **Known problems:** None.
37 declare_clippy_lint! {
38 pub NON_ASCII_LITERAL,
40 "using any literal non-ASCII chars in a string literal instead of \
41 using the `\\u` escape"
44 /// **What it does:** Checks for string literals that contain Unicode in a form
45 /// that is not equal to its
46 /// [NFC-recomposition](http://www.unicode.org/reports/tr15/#Norm_Forms).
48 /// **Why is this bad?** If such a string is compared to another, the results
49 /// may be surprising.
51 /// **Known problems** None.
53 /// **Example:** You may not see it, but “à” and “à” aren't the same string. The
54 /// former when escaped is actually `"a\u{300}"` while the latter is `"\u{e0}"`.
55 declare_clippy_lint! {
58 "using a unicode literal not in NFC normal form (see \
59 [unicode tr15](http://www.unicode.org/reports/tr15/) for further information)"
63 #[derive(Copy, Clone)]
66 impl LintPass for Unicode {
67 fn get_lints(&self) -> LintArray {
68 lint_array!(ZERO_WIDTH_SPACE, NON_ASCII_LITERAL, UNICODE_NOT_NFC)
72 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Unicode {
73 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
74 if let ExprLit(ref lit) = expr.node {
75 if let LitKind::Str(_, _) = lit.node {
76 check_str(cx, lit.span, expr.id)
82 fn escape<T: Iterator<Item = char>>(s: T) -> String {
83 let mut result = String::new();
86 for d in c.escape_unicode() {
96 fn check_str(cx: &LateContext, span: Span, id: NodeId) {
97 let string = snippet(cx, span, "");
98 if string.contains('\u{200B}') {
103 "zero-width space detected",
105 "Consider replacing the string with:\n\"{}\"",
106 string.replace("\u{200B}", "\\u{200B}")
110 if string.chars().any(|c| c as u32 > 0x7F) {
115 "literal non-ASCII character detected",
117 "Consider replacing the string with:\n\"{}\"",
118 if is_allowed(cx, UNICODE_NOT_NFC, id) {
119 escape(string.chars())
126 if is_allowed(cx, NON_ASCII_LITERAL, id) && string.chars().zip(string.nfc()).any(|(a, b)| a != b) {
131 "non-nfc unicode sequence detected",
132 &format!("Consider replacing the string with:\n\"{}\"", string.nfc().collect::<String>()),