Skip to content

Commit a2d4e85

Browse files
committed
Uplift get_def_path from Clippy
This uplifts `get_def_path` from Clippy. This is a follow up on the implementation of internal lints: rust-lang#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.
1 parent dec0a98 commit a2d4e85

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/librustc/lint/context.rs

+30-5
Original file line numberDiff line numberDiff line change
@@ -755,8 +755,31 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
755755
}
756756

757757
/// Check if a `DefId`'s path matches the given absolute type path usage.
758+
///
759+
/// # Examples
760+
/// ```rust,ignore (no cx or def_id available)
761+
/// if cx.match_def_path(def_id, &["core", "option", "Option"]) {
762+
/// // The given `def_id` is that of an `Option` type
763+
/// }
764+
/// ```
758765
// Uplifted from rust-lang/rust-clippy
759-
pub fn match_path(&self, def_id: DefId, path: &[&str]) -> bool {
766+
pub fn match_def_path(&self, def_id: DefId, path: &[&str]) -> bool {
767+
let names = self.get_def_path(def_id);
768+
769+
names.len() == path.len() && names.into_iter().zip(path.iter()).all(|(a, &b)| *a == *b)
770+
}
771+
772+
/// Gets the absolute path of `def_id` as a vector of `&str`.
773+
///
774+
/// # Examples
775+
/// ```rust,ignore (no cx or def_id available)
776+
/// let def_path = cx.get_def_path(def_id);
777+
/// if let &["core", "option", "Option"] = &def_path[..] {
778+
/// // The given `def_id` is that of an `Option` type
779+
/// }
780+
/// ```
781+
// Uplifted from rust-lang/rust-clippy
782+
pub fn get_def_path(&self, def_id: DefId) -> Vec<&'static str> {
760783
pub struct AbsolutePathPrinter<'a, 'tcx> {
761784
pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
762785
}
@@ -856,10 +879,12 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
856879
}
857880
}
858881

859-
let names = AbsolutePathPrinter { tcx: self.tcx }.print_def_path(def_id, &[]).unwrap();
860-
861-
names.len() == path.len()
862-
&& names.into_iter().zip(path.iter()).all(|(a, &b)| *a == *b)
882+
AbsolutePathPrinter { tcx: self.tcx }
883+
.print_def_path(def_id, &[])
884+
.unwrap()
885+
.iter()
886+
.map(LocalInternedString::get)
887+
.collect()
863888
}
864889
}
865890

src/librustc/lint/internal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fn lint_ty_kind_usage(cx: &LateContext<'_, '_>, segment: &PathSegment) -> bool {
100100
if segment.ident.as_str() == "TyKind" {
101101
if let Some(def) = segment.def {
102102
if let Some(did) = def.opt_def_id() {
103-
return cx.match_path(did, &["rustc", "ty", "sty", "TyKind"]);
103+
return cx.match_def_path(did, &["rustc", "ty", "sty", "TyKind"]);
104104
}
105105
}
106106
}

0 commit comments

Comments
 (0)