@@ -15,6 +15,7 @@ use rustc_hir::intravisit::{self, Visitor};
1515use rustc_lint:: { LateContext , LateLintPass , Lint , LintContext } ;
1616use rustc_middle:: hir:: map:: Map ;
1717use rustc_middle:: lint:: in_external_macro;
18+ use rustc_middle:: ty:: subst:: GenericArgKind ;
1819use rustc_middle:: ty:: { self , Predicate , Ty } ;
1920use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
2021use rustc_span:: source_map:: Span ;
@@ -1407,7 +1408,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
14071408 let parent = cx. tcx . hir ( ) . get_parent_item ( impl_item. hir_id ) ;
14081409 let item = cx. tcx . hir ( ) . expect_item ( parent) ;
14091410 let def_id = cx. tcx . hir ( ) . local_def_id ( item. hir_id ) ;
1410- let ty = cx. tcx . type_of ( def_id) ;
1411+ let self_ty = cx. tcx . type_of ( def_id) ;
14111412 if_chain ! {
14121413 if let hir:: ImplItemKind :: Fn ( ref sig, id) = impl_item. kind;
14131414 if let Some ( first_arg) = iter_input_pats( & sig. decl, cx. tcx. hir( ) . body( id) ) . next( ) ;
@@ -1429,7 +1430,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
14291430 if name == method_name &&
14301431 sig. decl. inputs. len( ) == n_args &&
14311432 out_type. matches( cx, & sig. decl. output) &&
1432- self_kind. matches( cx, ty , first_arg_ty) {
1433+ self_kind. matches( cx, self_ty , first_arg_ty) {
14331434 span_lint( cx, SHOULD_IMPLEMENT_TRAIT , impl_item. span, & format!(
14341435 "defining a method called `{}` on this type; consider implementing \
14351436 the `{}` trait or choosing a less ambiguous name", name, trait_name) ) ;
@@ -1441,7 +1442,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
14411442 . iter( )
14421443 . find( |( ref conv, _) | conv. check( & name) )
14431444 {
1444- if !self_kinds. iter( ) . any( |k| k. matches( cx, ty , first_arg_ty) ) {
1445+ if !self_kinds. iter( ) . any( |k| k. matches( cx, self_ty , first_arg_ty) ) {
14451446 let lint = if item. vis. node. is_pub( ) {
14461447 WRONG_PUB_SELF_CONVENTION
14471448 } else {
@@ -1471,8 +1472,16 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
14711472 if let hir:: ImplItemKind :: Fn ( _, _) = impl_item. kind {
14721473 let ret_ty = return_ty ( cx, impl_item. hir_id ) ;
14731474
1475+ let contains_self_ty = |ty : Ty < ' tcx > | {
1476+ ty. walk ( ) . any ( |inner| match inner. unpack ( ) {
1477+ GenericArgKind :: Type ( inner_ty) => same_tys ( cx, self_ty, inner_ty) ,
1478+
1479+ GenericArgKind :: Lifetime ( _) | GenericArgKind :: Const ( _) => false ,
1480+ } )
1481+ } ;
1482+
14741483 // walk the return type and check for Self (this does not check associated types)
1475- if ret_ty . walk ( ) . any ( |inner_type| same_tys ( cx , ty , inner_type ) ) {
1484+ if contains_self_ty ( ret_ty ) {
14761485 return ;
14771486 }
14781487
@@ -1486,18 +1495,16 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
14861495 let associated_type = binder. skip_binder ( ) ;
14871496
14881497 // walk the associated type and check for Self
1489- for inner_type in associated_type. walk ( ) {
1490- if same_tys ( cx, ty, inner_type) {
1491- return ;
1492- }
1498+ if contains_self_ty ( associated_type) {
1499+ return ;
14931500 }
14941501 } ,
14951502 ( _, _) => { } ,
14961503 }
14971504 }
14981505 }
14991506
1500- if name == "new" && !same_tys ( cx, ret_ty, ty ) {
1507+ if name == "new" && !same_tys ( cx, ret_ty, self_ty ) {
15011508 span_lint (
15021509 cx,
15031510 NEW_RET_NO_SELF ,
0 commit comments