1 use crate::utils::{is_allowed, snippet, span_help_and_lint};
3 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use rustc::{declare_lint_pass, declare_tool_lint};
5 use syntax::ast::LitKind;
6 use syntax::source_map::Span;
7 use unicode_normalization::UnicodeNormalization;
10 /// **What it does:** Checks for the Unicode zero-width space in the code.
12 /// **Why is this bad?** Having an invisible character in the code makes for all
13 /// sorts of April fools, but otherwise is very much frowned upon.
15 /// **Known problems:** None.
17 /// **Example:** You don't see it, but there may be a zero-width space
18 /// somewhere in this text.
21 "using a zero-width space in a string literal, which is confusing"
24 declare_clippy_lint! {
25 /// **What it does:** Checks for non-ASCII characters in string literals.
27 /// **Why is this bad?** Yeah, we know, the 90's called and wanted their charset
28 /// back. Even so, there still are editors and other programs out there that
29 /// don't work well with Unicode. So if the code is meant to be used
30 /// internationally, on multiple operating systems, or has other portability
31 /// requirements, activating this lint could be useful.
33 /// **Known problems:** None.
39 pub NON_ASCII_LITERAL,
41 "using any literal non-ASCII chars in a string literal instead of using the `\\u` escape"
44 declare_clippy_lint! {
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}"`.
58 "using a unicode literal not in NFC normal form (see [unicode tr15](http://www.unicode.org/reports/tr15/) for further information)"
61 declare_lint_pass!(Unicode => [ZERO_WIDTH_SPACE, NON_ASCII_LITERAL, UNICODE_NOT_NFC]);
63 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Unicode {
64 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
65 if let ExprKind::Lit(ref lit) = expr.node {
66 if let LitKind::Str(_, _) = lit.node {
67 check_str(cx, lit.span, expr.hir_id)
73 fn escape<T: Iterator<Item = char>>(s: T) -> String {
74 let mut result = String::new();
77 for d in c.escape_unicode() {
87 fn check_str(cx: &LateContext<'_, '_>, span: Span, id: HirId) {
88 let string = snippet(cx, span, "");
89 if string.contains('\u{200B}') {
94 "zero-width space detected",
96 "Consider replacing the string with:\n\"{}\"",
97 string.replace("\u{200B}", "\\u{200B}")
101 if string.chars().any(|c| c as u32 > 0x7F) {
106 "literal non-ASCII character detected",
108 "Consider replacing the string with:\n\"{}\"",
109 if is_allowed(cx, UNICODE_NOT_NFC, id) {
110 escape(string.chars())
117 if is_allowed(cx, NON_ASCII_LITERAL, id) && string.chars().zip(string.nfc()).any(|(a, b)| a != b) {
122 "non-nfc unicode sequence detected",
124 "Consider replacing the string with:\n\"{}\"",
125 string.nfc().collect::<String>()