1
1
//! Some lints that are only useful in the compiler or crates that use compiler internals, such as
2
2
//! Clippy.
3
3
4
- use crate :: hir:: { def :: Def , HirId , Path , QPath , Ty , TyKind } ;
4
+ use crate :: hir:: { HirId , Path , PathSegment , QPath , Ty , TyKind } ;
5
5
use crate :: lint:: {
6
6
EarlyContext , EarlyLintPass , LateContext , LateLintPass , LintArray , LintContext , LintPass ,
7
7
} ;
@@ -82,38 +82,19 @@ impl LintPass for TyKindUsage {
82
82
83
83
impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for TyKindUsage {
84
84
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 ( ) ;
117
98
}
118
99
}
119
100
}
@@ -122,24 +103,25 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyKindUsage {
122
103
if let TyKind :: Path ( qpath) = & ty. node {
123
104
if let QPath :: Resolved ( _, path) = qpath {
124
105
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 ( ) ;
140
110
}
141
111
}
142
112
}
143
113
}
144
114
}
145
115
}
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