1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 use crate::rustc::hir::*;
11 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
12 use crate::rustc::{declare_tool_lint, lint_array};
13 use crate::syntax::ast::{LitKind, NodeId};
14 use crate::syntax::source_map::Span;
15 use crate::utils::{is_allowed, snippet, span_help_and_lint};
16 use unicode_normalization::UnicodeNormalization;
18 /// **What it does:** Checks for the Unicode zero-width space in the code.
20 /// **Why is this bad?** Having an invisible character in the code makes for all
21 /// sorts of April fools, but otherwise is very much frowned upon.
23 /// **Known problems:** None.
25 /// **Example:** You don't see it, but there may be a zero-width space
26 /// somewhere in this text.
27 declare_clippy_lint! {
30 "using a zero-width space in a string literal, which is confusing"
33 /// **What it does:** Checks for non-ASCII characters in string literals.
35 /// **Why is this bad?** Yeah, we know, the 90's called and wanted their charset
36 /// back. Even so, there still are editors and other programs out there that
37 /// don't work well with Unicode. So if the code is meant to be used
38 /// internationally, on multiple operating systems, or has other portability
39 /// requirements, activating this lint could be useful.
41 /// **Known problems:** None.
47 declare_clippy_lint! {
48 pub NON_ASCII_LITERAL,
50 "using any literal non-ASCII chars in a string literal instead of using the `\\u` escape"
53 /// **What it does:** Checks for string literals that contain Unicode in a form
54 /// that is not equal to its
55 /// [NFC-recomposition](http://www.unicode.org/reports/tr15/#Norm_Forms).
57 /// **Why is this bad?** If such a string is compared to another, the results
58 /// may be surprising.
60 /// **Known problems** None.
62 /// **Example:** You may not see it, but “à” and “à” aren't the same string. The
63 /// former when escaped is actually `"a\u{300}"` while the latter is `"\u{e0}"`.
64 declare_clippy_lint! {
67 "using a unicode literal not in NFC normal form (see [unicode tr15](http://www.unicode.org/reports/tr15/) for further information)"
70 #[derive(Copy, Clone)]
73 impl LintPass for Unicode {
74 fn get_lints(&self) -> LintArray {
75 lint_array!(ZERO_WIDTH_SPACE, NON_ASCII_LITERAL, UNICODE_NOT_NFC)
79 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Unicode {
80 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
81 if let ExprKind::Lit(ref lit) = expr.node {
82 if let LitKind::Str(_, _) = lit.node {
83 check_str(cx, lit.span, expr.id)
89 fn escape<T: Iterator<Item = char>>(s: T) -> String {
90 let mut result = String::new();
93 for d in c.escape_unicode() {
103 fn check_str(cx: &LateContext<'_, '_>, span: Span, id: NodeId) {
104 let string = snippet(cx, span, "");
105 if string.contains('\u{200B}') {
110 "zero-width space detected",
112 "Consider replacing the string with:\n\"{}\"",
113 string.replace("\u{200B}", "\\u{200B}")
117 if string.chars().any(|c| c as u32 > 0x7F) {
122 "literal non-ASCII character detected",
124 "Consider replacing the string with:\n\"{}\"",
125 if is_allowed(cx, UNICODE_NOT_NFC, id) {
126 escape(string.chars())
133 if is_allowed(cx, NON_ASCII_LITERAL, id) && string.chars().zip(string.nfc()).any(|(a, b)| a != b) {
138 "non-nfc unicode sequence detected",
140 "Consider replacing the string with:\n\"{}\"",
141 string.nfc().collect::<String>()