]> git.lizzy.rs Git - rust.git/commitdiff
Uplift `get_def_path` from Clippy
authorflip1995 <hello@philkrones.com>
Sun, 7 Apr 2019 17:47:54 +0000 (19:47 +0200)
committerflip1995 <hello@philkrones.com>
Fri, 12 Apr 2019 08:38:39 +0000 (10:38 +0200)
This uplifts `get_def_path` from Clippy. This is a follow up on the
implementation of internal lints: #59316

The internal lint implementation also copied the implementation of the
`AbsolutePathPrinter`. To get rid of this code duplication this also
uplifts the `get_def_path` function from Clippy.

This also renames `match_path` to `match_def_path`, as it was originally
named in Clippy.

src/librustc/lint/context.rs
src/librustc/lint/internal.rs

index 15ea6403e38bab7f7b85c4848a4f69e629fc5966..4b615345a26f392849e1be884731bfaa95cbca5e 100644 (file)
@@ -755,8 +755,31 @@ pub fn current_lint_root(&self) -> hir::HirId {
     }
 
     /// Check if a `DefId`'s path matches the given absolute type path usage.
+    ///
+    /// # Examples
+    /// ```rust,ignore (no `cx` or `def_id` available)
+    /// if cx.match_def_path(def_id, &["core", "option", "Option"]) {
+    ///     // The given `def_id` is that of an `Option` type
+    /// }
+    /// ```
     // Uplifted from rust-lang/rust-clippy
-    pub fn match_path(&self, def_id: DefId, path: &[&str]) -> bool {
+    pub fn match_def_path(&self, def_id: DefId, path: &[&str]) -> bool {
+        let names = self.get_def_path(def_id);
+
+        names.len() == path.len() && names.into_iter().zip(path.iter()).all(|(a, &b)| *a == *b)
+    }
+
+    /// Gets the absolute path of `def_id` as a vector of `&str`.
+    ///
+    /// # Examples
+    /// ```rust,ignore (no `cx` or `def_id` available)
+    /// let def_path = cx.get_def_path(def_id);
+    /// if let &["core", "option", "Option"] = &def_path[..] {
+    ///     // The given `def_id` is that of an `Option` type
+    /// }
+    /// ```
+    // Uplifted from rust-lang/rust-clippy
+    pub fn get_def_path(&self, def_id: DefId) -> Vec<LocalInternedString> {
         pub struct AbsolutePathPrinter<'a, 'tcx> {
             pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
         }
@@ -856,10 +879,9 @@ fn path_generic_args(
             }
         }
 
-        let names = AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]).unwrap();
-
-        names.len() == path.len()
-            && names.into_iter().zip(path.iter()).all(|(a, &b)| *a == *b)
+        AbsolutePathPrinter { tcx: self.tcx }
+            .print_def_path(def_id, &[])
+            .unwrap()
     }
 }
 
index 0bafa93011ec4d1be96dc8d9250d38cebd7b0d0a..91f1bee26de3275a62c22c6bb0782f55f385d736 100644 (file)
@@ -100,7 +100,7 @@ fn lint_ty_kind_usage(cx: &LateContext<'_, '_>, segment: &PathSegment) -> bool {
     if segment.ident.as_str() == "TyKind" {
         if let Some(def) = segment.def {
             if let Some(did) = def.opt_def_id() {
-                return cx.match_path(did, &["rustc", "ty", "sty", "TyKind"]);
+                return cx.match_def_path(did, &["rustc", "ty", "sty", "TyKind"]);
             }
         }
     }