1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::is_lint_allowed;
3 use clippy_utils::source::snippet;
4 use rustc_ast::ast::LitKind;
5 use rustc_errors::Applicability;
6 use rustc_hir::{Expr, ExprKind, HirId};
7 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
9 use rustc_span::source_map::Span;
10 use unicode_normalization::UnicodeNormalization;
12 declare_clippy_lint! {
14 /// Checks for invisible Unicode characters in the code.
16 /// ### Why is this bad?
17 /// Having an invisible character in the code makes for all
18 /// sorts of April fools, but otherwise is very much frowned upon.
21 /// You don't see it, but there may be a zero-width space or soft hyphen
22 /// somewhere in this text.
23 #[clippy::version = "1.49.0"]
24 pub INVISIBLE_CHARACTERS,
26 "using an invisible character in a string literal, which is confusing"
29 declare_clippy_lint! {
31 /// Checks for non-ASCII characters in string and char literals.
33 /// ### Why is this bad?
34 /// Yeah, we know, the 90's called and wanted their charset
35 /// back. Even so, there still are editors and other programs out there that
36 /// don't work well with Unicode. So if the code is meant to be used
37 /// internationally, on multiple operating systems, or has other portability
38 /// requirements, activating this lint could be useful.
42 /// let x = String::from("€");
47 /// let x = String::from("\u{20ac}");
49 #[clippy::version = "pre 1.29.0"]
50 pub NON_ASCII_LITERAL,
52 "using any literal non-ASCII chars in a string literal instead of using the `\\u` escape"
55 declare_clippy_lint! {
57 /// Checks for string literals that contain Unicode in a form
58 /// that is not equal to its
59 /// [NFC-recomposition](http://www.unicode.org/reports/tr15/#Norm_Forms).
61 /// ### Why is this bad?
62 /// If such a string is compared to another, the results
63 /// may be surprising.
66 /// You may not see it, but "à"" and "à"" aren't the same string. The
67 /// former when escaped is actually `"a\u{300}"` while the latter is `"\u{e0}"`.
68 #[clippy::version = "pre 1.29.0"]
71 "using a Unicode literal not in NFC normal form (see [Unicode tr15](http://www.unicode.org/reports/tr15/) for further information)"
74 declare_lint_pass!(Unicode => [INVISIBLE_CHARACTERS, NON_ASCII_LITERAL, UNICODE_NOT_NFC]);
76 impl LateLintPass<'_> for Unicode {
77 fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
78 if let ExprKind::Lit(ref lit) = expr.kind {
79 if let LitKind::Str(_, _) | LitKind::Char(_) = lit.node {
80 check_str(cx, lit.span, expr.hir_id);
86 fn escape<T: Iterator<Item = char>>(s: T) -> String {
87 let mut result = String::new();
90 for d in c.escape_unicode() {
100 fn check_str(cx: &LateContext<'_>, span: Span, id: HirId) {
101 let string = snippet(cx, span, "");
102 if string.chars().any(|c| ['\u{200B}', '\u{ad}', '\u{2060}'].contains(&c)) {
105 INVISIBLE_CHARACTERS,
107 "invisible character detected",
108 "consider replacing the string with",
110 .replace('\u{200B}', "\\u{200B}")
111 .replace('\u{ad}', "\\u{AD}")
112 .replace('\u{2060}', "\\u{2060}"),
113 Applicability::MachineApplicable,
116 if string.chars().any(|c| c as u32 > 0x7F) {
121 "literal non-ASCII character detected",
122 "consider replacing the string with",
123 if is_lint_allowed(cx, UNICODE_NOT_NFC, id) {
124 escape(string.chars())
128 Applicability::MachineApplicable,
131 if is_lint_allowed(cx, NON_ASCII_LITERAL, id) && string.chars().zip(string.nfc()).any(|(a, b)| a != b) {
136 "non-NFC Unicode sequence detected",
137 "consider replacing the string with",
138 string.nfc().collect::<String>(),
139 Applicability::MachineApplicable,