11use clippy_utils:: diagnostics:: span_lint;
22
33use rustc_data_structures:: fx:: FxHashSet ;
4- use rustc_hir:: { def:: Res , Item , ItemKind , PolyTraitRef , TraitBoundModifier , Ty , TyKind , UseKind } ;
4+ use rustc_hir:: {
5+ def:: Res , def_id:: DefId , Crate , Item , ItemKind , PolyTraitRef , TraitBoundModifier , Ty , TyKind , UseKind ,
6+ } ;
57use rustc_lint:: { LateContext , LateLintPass } ;
68use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
79use rustc_span:: { Span , Symbol } ;
@@ -47,6 +49,7 @@ declare_clippy_lint! {
4749#[ derive( Clone , Debug ) ]
4850pub struct DisallowedType {
4951 disallowed : FxHashSet < Vec < Symbol > > ,
52+ def_ids : FxHashSet < ( DefId , Vec < Symbol > ) > ,
5053}
5154
5255impl DisallowedType {
@@ -56,19 +59,29 @@ impl DisallowedType {
5659 . iter ( )
5760 . map ( |s| s. split ( "::" ) . map ( |seg| Symbol :: intern ( seg) ) . collect :: < Vec < _ > > ( ) )
5861 . collect ( ) ,
62+ def_ids : FxHashSet :: default ( ) ,
5963 }
6064 }
6165}
6266
6367impl_lint_pass ! ( DisallowedType => [ DISALLOWED_TYPE ] ) ;
6468
6569impl < ' tcx > LateLintPass < ' tcx > for DisallowedType {
70+ fn check_crate ( & mut self , cx : & LateContext < ' _ > , _: & Crate < ' _ > ) {
71+ for path in & self . disallowed {
72+ let segs = path. iter ( ) . map ( ToString :: to_string) . collect :: < Vec < _ > > ( ) ;
73+ if let Res :: Def ( _, id) = clippy_utils:: path_to_res ( cx, & segs. iter ( ) . map ( String :: as_str) . collect :: < Vec < _ > > ( ) )
74+ {
75+ self . def_ids . insert ( ( id, path. clone ( ) ) ) ;
76+ }
77+ }
78+ }
79+
6680 fn check_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx Item < ' tcx > ) {
6781 if_chain ! {
6882 if let ItemKind :: Use ( path, UseKind :: Single ) = & item. kind;
69- if let Res :: Def ( _, id) = path. res;
70- let use_path = cx. get_def_path( id) ;
71- if let Some ( name) = self . disallowed. iter( ) . find( |path| * * path == use_path) ;
83+ if let Res :: Def ( _, did) = path. res;
84+ if let Some ( ( _, name) ) = self . def_ids. iter( ) . find( |( id, _) | * id == did) ;
7285 then {
7386 emit( cx, name, item. span, ) ;
7487 }
@@ -79,8 +92,7 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedType {
7992 if_chain ! {
8093 if let TyKind :: Path ( path) = & ty. kind;
8194 if let Some ( did) = cx. qpath_res( path, ty. hir_id) . opt_def_id( ) ;
82- let use_path = cx. get_def_path( did) ;
83- if let Some ( name) = self . disallowed. iter( ) . find( |path| * * path == use_path) ;
95+ if let Some ( ( _, name) ) = self . def_ids. iter( ) . find( |( id, _) | * id == did) ;
8496 then {
8597 emit( cx, name, path. span( ) ) ;
8698 }
@@ -90,8 +102,7 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedType {
90102 fn check_poly_trait_ref ( & mut self , cx : & LateContext < ' tcx > , poly : & ' tcx PolyTraitRef < ' tcx > , _: TraitBoundModifier ) {
91103 if_chain ! {
92104 if let Res :: Def ( _, did) = poly. trait_ref. path. res;
93- let use_path = cx. get_def_path( did) ;
94- if let Some ( name) = self . disallowed. iter( ) . find( |path| * * path == use_path) ;
105+ if let Some ( ( _, name) ) = self . def_ids. iter( ) . find( |( id, _) | * id == did) ;
95106 then {
96107 emit( cx, name, poly. trait_ref. path. span) ;
97108 }
0 commit comments