@@ -73,7 +73,7 @@ fn span_use_self_lint(cx: &LateContext<'_, '_>, path: &Path) {
73
73
}
74
74
75
75
struct TraitImplTyVisitor < ' a , ' tcx : ' a > {
76
- item_path : & ' a Path ,
76
+ item_type : ty :: Ty < ' tcx > ,
77
77
cx : & ' a LateContext < ' a , ' tcx > ,
78
78
trait_type_walker : ty:: walk:: TypeWalker < ' tcx > ,
79
79
impl_type_walker : ty:: walk:: TypeWalker < ' tcx > ,
@@ -85,21 +85,28 @@ impl<'a, 'tcx> Visitor<'tcx> for TraitImplTyVisitor<'a, 'tcx> {
85
85
let impl_ty = self . impl_type_walker . next ( ) ;
86
86
87
87
if let TyKind :: Path ( QPath :: Resolved ( _, path) ) = & t. node {
88
- if self . item_path . def == path. def {
89
- let is_self_ty = if let def:: Def :: SelfTy ( ..) = path. def {
90
- true
91
- } else {
92
- false
93
- } ;
94
88
95
- if !is_self_ty && impl_ty != trait_ty {
96
- // The implementation and trait types don't match which means that
97
- // the concrete type was specified by the implementation but
98
- // it didn't use `Self`
99
- span_use_self_lint ( self . cx , path) ;
89
+ // The implementation and trait types don't match which means that
90
+ // the concrete type was specified by the implementation
91
+ if impl_ty != trait_ty {
92
+
93
+ if let Some ( impl_ty) = impl_ty {
94
+ if self . item_type == impl_ty {
95
+ let is_self_ty = if let def:: Def :: SelfTy ( ..) = path. def {
96
+ true
97
+ } else {
98
+ false
99
+ } ;
100
+
101
+ if !is_self_ty {
102
+ span_use_self_lint ( self . cx , path) ;
103
+ }
104
+ }
100
105
}
106
+
101
107
}
102
108
}
109
+
103
110
walk_ty ( self , t)
104
111
}
105
112
@@ -110,7 +117,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TraitImplTyVisitor<'a, 'tcx> {
110
117
111
118
fn check_trait_method_impl_decl < ' a , ' tcx : ' a > (
112
119
cx : & ' a LateContext < ' a , ' tcx > ,
113
- item_path : & ' a Path ,
120
+ item_type : ty :: Ty < ' tcx > ,
114
121
impl_item : & ImplItem ,
115
122
impl_decl : & ' tcx FnDecl ,
116
123
impl_trait_ref : & ty:: TraitRef < ' _ > ,
@@ -151,7 +158,7 @@ fn check_trait_method_impl_decl<'a, 'tcx: 'a>(
151
158
) {
152
159
let mut visitor = TraitImplTyVisitor {
153
160
cx,
154
- item_path ,
161
+ item_type ,
155
162
trait_type_walker : trait_ty. walk ( ) ,
156
163
impl_type_walker : impl_ty. walk ( ) ,
157
164
} ;
@@ -192,7 +199,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
192
199
let impl_item = cx. tcx. hir. impl_item( impl_item_ref. id) ;
193
200
if let ImplItemKind :: Method ( MethodSig { decl: impl_decl, .. } , impl_body_id)
194
201
= & impl_item. node {
195
- check_trait_method_impl_decl( cx, item_path, impl_item, impl_decl, & impl_trait_ref) ;
202
+ let item_type = cx. tcx. type_of( impl_def_id) ;
203
+ check_trait_method_impl_decl( cx, item_type, impl_item, impl_decl, & impl_trait_ref) ;
204
+
196
205
let body = cx. tcx. hir. body( * impl_body_id) ;
197
206
visitor. visit_body( body) ;
198
207
} else {
0 commit comments