@@ -9,7 +9,7 @@ use rustc_hir::{
9
9
def:: { CtorOf , DefKind , Res } ,
10
10
def_id:: LocalDefId ,
11
11
intravisit:: { walk_ty, NestedVisitorMap , Visitor } ,
12
- Expr , ExprKind , FnRetTy , FnSig , GenericArg , HirId , Impl , ImplItemKind , Item , ItemKind , Node , Path , QPath , TyKind ,
12
+ Expr , ExprKind , FnRetTy , FnSig , GenericArg , HirId , Impl , ImplItemKind , Item , ItemKind , Path , QPath , TyKind ,
13
13
} ;
14
14
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
15
15
use rustc_middle:: hir:: map:: Map ;
@@ -74,8 +74,7 @@ impl UseSelf {
74
74
#[ derive( Debug ) ]
75
75
enum StackItem {
76
76
Check {
77
- hir_id : HirId ,
78
- impl_trait_ref_def_id : Option < LocalDefId > ,
77
+ impl_id : LocalDefId ,
79
78
types_to_skip : FxHashSet < HirId > ,
80
79
types_to_lint : Vec < HirId > ,
81
80
} ,
@@ -87,7 +86,7 @@ impl_lint_pass!(UseSelf => [USE_SELF]);
87
86
const SEGMENTS_MSG : & str = "segments should be composed of at least 1 element" ;
88
87
89
88
impl < ' tcx > LateLintPass < ' tcx > for UseSelf {
90
- fn check_item ( & mut self , cx : & LateContext < ' _ > , item : & Item < ' _ > ) {
89
+ fn check_item ( & mut self , _cx : & LateContext < ' _ > , item : & Item < ' _ > ) {
91
90
if !is_item_interesting ( item) {
92
91
// This does two things:
93
92
// 1) Reduce needless churn on `self.stack`
@@ -100,17 +99,15 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
100
99
// avoid linting on nested items, we push `StackItem::NoCheck` on the stack to signal, that
101
100
// we're in an `impl` or nested item, that we don't want to lint
102
101
let stack_item = if_chain ! {
103
- if let ItemKind :: Impl ( Impl { self_ty, ref of_trait , .. } ) = item. kind;
102
+ if let ItemKind :: Impl ( Impl { self_ty, .. } ) = item. kind;
104
103
if let TyKind :: Path ( QPath :: Resolved ( _, item_path) ) = self_ty. kind;
105
104
let parameters = & item_path. segments. last( ) . expect( SEGMENTS_MSG ) . args;
106
105
if parameters. as_ref( ) . map_or( true , |params| {
107
106
!params. parenthesized && !params. args. iter( ) . any( |arg| matches!( arg, GenericArg :: Lifetime ( _) ) )
108
107
} ) ;
109
108
then {
110
- let impl_trait_ref_def_id = of_trait. as_ref( ) . map( |_| cx. tcx. hir( ) . local_def_id( item. hir_id( ) ) ) ;
111
109
StackItem :: Check {
112
- hir_id: self_ty. hir_id,
113
- impl_trait_ref_def_id,
110
+ impl_id: item. def_id,
114
111
types_to_lint: Vec :: new( ) ,
115
112
types_to_skip: std:: iter:: once( self_ty. hir_id) . collect( ) ,
116
113
}
@@ -133,11 +130,11 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
133
130
if_chain ! {
134
131
if let ImplItemKind :: Fn ( FnSig { decl, .. } , ..) = impl_item. kind;
135
132
if let Some ( & mut StackItem :: Check {
136
- impl_trait_ref_def_id : Some ( def_id ) ,
133
+ impl_id ,
137
134
ref mut types_to_skip,
138
135
..
139
136
} ) = self . stack. last_mut( ) ;
140
- if let Some ( impl_trait_ref) = cx. tcx. impl_trait_ref( def_id ) ;
137
+ if let Some ( impl_trait_ref) = cx. tcx. impl_trait_ref( impl_id ) ;
141
138
then {
142
139
// `self_ty` is the semantic self type of `impl <trait> for <type>`. This cannot be
143
140
// `Self`.
@@ -195,13 +192,13 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
195
192
// could only allow this lint on item scope. And we would have to check if those types are
196
193
// already dealt with in `check_ty` anyway.
197
194
if let Some ( StackItem :: Check {
198
- hir_id ,
195
+ impl_id ,
199
196
types_to_lint,
200
197
types_to_skip,
201
198
..
202
199
} ) = self . stack . last_mut ( )
203
200
{
204
- let self_ty = ty_from_hir_id ( cx , * hir_id ) ;
201
+ let self_ty = cx . tcx . type_of ( * impl_id ) ;
205
202
206
203
let mut visitor = LintTyCollector {
207
204
cx,
@@ -220,15 +217,14 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
220
217
if !in_macro( hir_ty. span) ;
221
218
if meets_msrv( self . msrv. as_ref( ) , & msrvs:: TYPE_ALIAS_ENUM_VARIANTS ) ;
222
219
if let Some ( StackItem :: Check {
223
- hir_id ,
220
+ impl_id ,
224
221
types_to_lint,
225
222
types_to_skip,
226
- ..
227
223
} ) = self . stack. last( ) ;
228
224
if !types_to_skip. contains( & hir_ty. hir_id) ;
229
225
if types_to_lint. contains( & hir_ty. hir_id)
230
226
|| {
231
- let self_ty = ty_from_hir_id ( cx , * hir_id ) ;
227
+ let self_ty = cx . tcx . type_of ( * impl_id ) ;
232
228
should_lint_ty( hir_ty, hir_ty_to_ty( cx. tcx, hir_ty) , self_ty)
233
229
} ;
234
230
let hir = cx. tcx. hir( ) ;
@@ -244,8 +240,8 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
244
240
if_chain ! {
245
241
if !in_macro( expr. span) ;
246
242
if meets_msrv( self . msrv. as_ref( ) , & msrvs:: TYPE_ALIAS_ENUM_VARIANTS ) ;
247
- if let Some ( StackItem :: Check { hir_id , .. } ) = self . stack. last( ) ;
248
- if cx. typeck_results( ) . expr_ty( expr) == ty_from_hir_id ( cx , * hir_id ) ;
243
+ if let Some ( & StackItem :: Check { impl_id , .. } ) = self . stack. last( ) ;
244
+ if cx. typeck_results( ) . expr_ty( expr) == cx . tcx . type_of ( impl_id ) ;
249
245
then { } else { return ; }
250
246
}
251
247
match expr. kind {
@@ -351,14 +347,6 @@ fn is_item_interesting(item: &Item<'_>) -> bool {
351
347
)
352
348
}
353
349
354
- fn ty_from_hir_id < ' tcx > ( cx : & LateContext < ' tcx > , hir_id : HirId ) -> Ty < ' tcx > {
355
- if let Some ( Node :: Ty ( hir_ty) ) = cx. tcx . hir ( ) . find ( hir_id) {
356
- hir_ty_to_ty ( cx. tcx , hir_ty)
357
- } else {
358
- unreachable ! ( "This function should only be called with `HirId`s that are for sure `Node::Ty`" )
359
- }
360
- }
361
-
362
350
fn should_lint_ty ( hir_ty : & hir:: Ty < ' _ > , ty : Ty < ' _ > , self_ty : Ty < ' _ > ) -> bool {
363
351
if_chain ! {
364
352
if same_type_and_consts( ty, self_ty) ;
0 commit comments