@@ -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 < LocalInternedString > {
760783 pub struct AbsolutePathPrinter < ' a , ' tcx > {
761784 pub tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
762785 }
@@ -856,10 +879,9 @@ 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 ( )
863885 }
864886}
865887
0 commit comments