1 use crate::utils::{is_allowed, snippet, span_help_and_lint};
3 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use rustc::{declare_tool_lint, lint_array};
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 #[derive(Copy, Clone)]
64 impl LintPass for Unicode {
65 fn get_lints(&self) -> LintArray {
66 lint_array!(ZERO_WIDTH_SPACE, NON_ASCII_LITERAL, UNICODE_NOT_NFC)
69 fn name(&self) -> &'static str {
74 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Unicode {
75 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
76 if let ExprKind::Lit(ref lit) = expr.node {
77 if let LitKind::Str(_, _) = lit.node {
78 check_str(cx, lit.span, expr.hir_id)
84 fn escape<T: Iterator<Item = char>>(s: T) -> String {
85 let mut result = String::new();
88 for d in c.escape_unicode() {
98 fn check_str(cx: &LateContext<'_, '_>, span: Span, id: HirId) {
99 let string = snippet(cx, span, "");
100 if string.contains('\u{200B}') {
105 "zero-width space detected",
107 "Consider replacing the string with:\n\"{}\"",
108 string.replace("\u{200B}", "\\u{200B}")
112 if string.chars().any(|c| c as u32 > 0x7F) {
117 "literal non-ASCII character detected",
119 "Consider replacing the string with:\n\"{}\"",
120 if is_allowed(cx, UNICODE_NOT_NFC, id) {
121 escape(string.chars())
128 if is_allowed(cx, NON_ASCII_LITERAL, id) && string.chars().zip(string.nfc()).any(|(a, b)| a != b) {
133 "non-nfc unicode sequence detected",
135 "Consider replacing the string with:\n\"{}\"",
136 string.nfc().collect::<String>()