]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/unicode.rs
Auto merge of #3946 - rchaser53:issue-3920, r=flip1995
[rust.git] / clippy_lints / src / unicode.rs
index 83f9713b59e737cfec0e2235f9a242c835f869b2..4d9e2f18650ea3ca5dc9a8276cbe4a8b5d9b4101 100644 (file)
@@ -1,66 +1,63 @@
-use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
-use crate::rustc::{declare_tool_lint, lint_array};
-use crate::rustc::hir::*;
-use crate::syntax::ast::{LitKind, NodeId};
-use crate::syntax::source_map::Span;
-use unicode_normalization::UnicodeNormalization;
 use crate::utils::{is_allowed, snippet, span_help_and_lint};
+use rustc::hir::*;
+use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
+use rustc::{declare_tool_lint, lint_array};
+use syntax::ast::LitKind;
+use syntax::source_map::Span;
+use unicode_normalization::UnicodeNormalization;
 
-/// **What it does:** Checks for the Unicode zero-width space in the code.
-///
-/// **Why is this bad?** Having an invisible character in the code makes for all
-/// sorts of April fools, but otherwise is very much frowned upon.
-///
-/// **Known problems:** None.
-///
-/// **Example:** You don't see it, but there may be a zero-width space
-/// somewhere in this text.
 declare_clippy_lint! {
+    /// **What it does:** Checks for the Unicode zero-width space in the code.
+    ///
+    /// **Why is this bad?** Having an invisible character in the code makes for all
+    /// sorts of April fools, but otherwise is very much frowned upon.
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:** You don't see it, but there may be a zero-width space
+    /// somewhere in this text.
     pub ZERO_WIDTH_SPACE,
     correctness,
     "using a zero-width space in a string literal, which is confusing"
 }
 
-/// **What it does:** Checks for non-ASCII characters in string literals.
-///
-/// **Why is this bad?** Yeah, we know, the 90's called and wanted their charset
-/// back. Even so, there still are editors and other programs out there that
-/// don't work well with Unicode. So if the code is meant to be used
-/// internationally, on multiple operating systems, or has other portability
-/// requirements, activating this lint could be useful.
-///
-/// **Known problems:** None.
-///
-/// **Example:**
-/// ```rust
-/// let x = "Hä?"
-/// ```
 declare_clippy_lint! {
+    /// **What it does:** Checks for non-ASCII characters in string literals.
+    ///
+    /// **Why is this bad?** Yeah, we know, the 90's called and wanted their charset
+    /// back. Even so, there still are editors and other programs out there that
+    /// don't work well with Unicode. So if the code is meant to be used
+    /// internationally, on multiple operating systems, or has other portability
+    /// requirements, activating this lint could be useful.
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:**
+    /// ```rust
+    /// let x = "Hä?"
+    /// ```
     pub NON_ASCII_LITERAL,
     pedantic,
-    "using any literal non-ASCII chars in a string literal instead of \
-     using the `\\u` escape"
+    "using any literal non-ASCII chars in a string literal instead of using the `\\u` escape"
 }
 
-/// **What it does:** Checks for string literals that contain Unicode in a form
-/// that is not equal to its
-/// [NFC-recomposition](http://www.unicode.org/reports/tr15/#Norm_Forms).
-///
-/// **Why is this bad?** If such a string is compared to another, the results
-/// may be surprising.
-///
-/// **Known problems** None.
-///
-/// **Example:** You may not see it, but “à” and “à” aren't the same string. The
-/// former when escaped is actually `"a\u{300}"` while the latter is `"\u{e0}"`.
 declare_clippy_lint! {
+    /// **What it does:** Checks for string literals that contain Unicode in a form
+    /// that is not equal to its
+    /// [NFC-recomposition](http://www.unicode.org/reports/tr15/#Norm_Forms).
+    ///
+    /// **Why is this bad?** If such a string is compared to another, the results
+    /// may be surprising.
+    ///
+    /// **Known problems** None.
+    ///
+    /// **Example:** You may not see it, but “à” and “à” aren't the same string. The
+    /// former when escaped is actually `"a\u{300}"` while the latter is `"\u{e0}"`.
     pub UNICODE_NOT_NFC,
     pedantic,
-    "using a unicode literal not in NFC normal form (see \
-     [unicode tr15](http://www.unicode.org/reports/tr15/) for further information)"
+    "using a unicode literal not in NFC normal form (see [unicode tr15](http://www.unicode.org/reports/tr15/) for further information)"
 }
 
-
 #[derive(Copy, Clone)]
 pub struct Unicode;
 
@@ -68,13 +65,17 @@ impl LintPass for Unicode {
     fn get_lints(&self) -> LintArray {
         lint_array!(ZERO_WIDTH_SPACE, NON_ASCII_LITERAL, UNICODE_NOT_NFC)
     }
+
+    fn name(&self) -> &'static str {
+        "Unicode"
+    }
 }
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Unicode {
     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
         if let ExprKind::Lit(ref lit) = expr.node {
             if let LitKind::Str(_, _) = lit.node {
-                check_str(cx, lit.span, expr.id)
+                check_str(cx, lit.span, expr.hir_id)
             }
         }
     }
@@ -94,7 +95,7 @@ fn escape<T: Iterator<Item = char>>(s: T) -> String {
     result
 }
 
-fn check_str(cx: &LateContext<'_, '_>, span: Span, id: NodeId) {
+fn check_str(cx: &LateContext<'_, '_>, span: Span, id: HirId) {
     let string = snippet(cx, span, "");
     if string.contains('\u{200B}') {
         span_help_and_lint(
@@ -130,7 +131,10 @@ fn check_str(cx: &LateContext<'_, '_>, span: Span, id: NodeId) {
             UNICODE_NOT_NFC,
             span,
             "non-nfc unicode sequence detected",
-            &format!("Consider replacing the string with:\n\"{}\"", string.nfc().collect::<String>()),
+            &format!(
+                "Consider replacing the string with:\n\"{}\"",
+                string.nfc().collect::<String>()
+            ),
         );
     }
 }