@@ -755,8 +755,31 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
755
755
}
756
756
757
757
/// 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
+ /// ```
758
765
// 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 > {
760
783
pub struct AbsolutePathPrinter < ' a , ' tcx > {
761
784
pub tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
762
785
}
@@ -856,10 +879,9 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
856
879
}
857
880
}
858
881
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 ( )
863
885
}
864
886
}
865
887
0 commit comments