11//! Some lints that are only useful in the compiler or crates that use compiler internals, such as
22//! Clippy.
33
4- use crate :: hir:: { def :: Def , HirId , Path , QPath , Ty , TyKind } ;
4+ use crate :: hir:: { HirId , Path , PathSegment , QPath , Ty , TyKind } ;
55use crate :: lint:: {
66 EarlyContext , EarlyLintPass , LateContext , LateLintPass , LintArray , LintContext , LintPass ,
77} ;
@@ -82,38 +82,19 @@ impl LintPass for TyKindUsage {
8282
8383impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for TyKindUsage {
8484 fn check_path ( & mut self , cx : & LateContext < ' _ , ' _ > , path : & ' tcx Path , _: HirId ) {
85- let segments_iter = path. segments . iter ( ) . rev ( ) . skip ( 1 ) . rev ( ) ;
86-
87- if let Some ( last) = segments_iter. clone ( ) . last ( ) {
88- if last. ident . as_str ( ) == "TyKind" {
89- let path = Path {
90- span : path. span . with_hi ( last. ident . span . hi ( ) ) ,
91- def : path. def ,
92- segments : segments_iter. cloned ( ) . collect ( ) ,
93- } ;
94-
95- match last. def {
96- Some ( Def :: Err ) => ( ) ,
97- Some ( def)
98- if def
99- . def_id ( )
100- . match_path ( cx. tcx , & [ "rustc" , "ty" , "sty" , "TyKind" ] ) =>
101- {
102- cx. struct_span_lint (
103- USAGE_OF_TY_TYKIND ,
104- path. span ,
105- "usage of `ty::TyKind::<kind>`" ,
106- )
107- . span_suggestion (
108- path. span ,
109- "try using ty::<kind> directly" ,
110- "ty" . to_string ( ) ,
111- Applicability :: MaybeIncorrect , // ty maybe needs an import
112- )
113- . emit ( ) ;
114- }
115- _ => ( ) ,
116- }
85+ let segments = path. segments . iter ( ) . rev ( ) . skip ( 1 ) . rev ( ) ;
86+
87+ if let Some ( last) = segments. last ( ) {
88+ let span = path. span . with_hi ( last. ident . span . hi ( ) ) ;
89+ if lint_ty_kind_usage ( cx, last) {
90+ cx. struct_span_lint ( USAGE_OF_TY_TYKIND , span, "usage of `ty::TyKind::<kind>`" )
91+ . span_suggestion (
92+ span,
93+ "try using ty::<kind> directly" ,
94+ "ty" . to_string ( ) ,
95+ Applicability :: MaybeIncorrect , // ty maybe needs an import
96+ )
97+ . emit ( ) ;
11798 }
11899 }
119100 }
@@ -122,24 +103,25 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyKindUsage {
122103 if let TyKind :: Path ( qpath) = & ty. node {
123104 if let QPath :: Resolved ( _, path) = qpath {
124105 if let Some ( last) = path. segments . iter ( ) . last ( ) {
125- if last. ident . as_str ( ) == "TyKind" {
126- if let Some ( def) = last. def {
127- if def
128- . def_id ( )
129- . match_path ( cx. tcx , & [ "rustc" , "ty" , "sty" , "TyKind" ] )
130- {
131- cx. struct_span_lint (
132- USAGE_OF_TY_TYKIND ,
133- path. span ,
134- "usage of `ty::TyKind`" ,
135- )
136- . help ( "try using `ty::Ty` instead" )
137- . emit ( ) ;
138- }
139- }
106+ if lint_ty_kind_usage ( cx, last) {
107+ cx. struct_span_lint ( USAGE_OF_TY_TYKIND , path. span , "usage of `ty::TyKind`" )
108+ . help ( "try using `ty::Ty` instead" )
109+ . emit ( ) ;
140110 }
141111 }
142112 }
143113 }
144114 }
145115}
116+
117+ fn lint_ty_kind_usage ( cx : & LateContext < ' _ , ' _ > , segment : & PathSegment ) -> bool {
118+ if segment. ident . as_str ( ) == "TyKind" {
119+ if let Some ( def) = segment. def {
120+ if let Some ( did) = def. opt_def_id ( ) {
121+ return did. match_path ( cx. tcx , & [ "rustc" , "ty" , "sty" , "TyKind" ] ) ;
122+ }
123+ }
124+ }
125+
126+ false
127+ }
0 commit comments