@@ -15,21 +15,20 @@ use rustc_ast::ast;
1515use rustc_errors:: Applicability ;
1616use rustc_hir as hir;
1717use rustc_hir:: intravisit:: { self , Visitor } ;
18- use rustc_hir:: { FnRetTy , FnSig , TraitItem , TraitItemKind } ;
18+ use rustc_hir:: { TraitItem , TraitItemKind } ;
1919use rustc_lint:: { LateContext , LateLintPass , Lint , LintContext } ;
2020use rustc_middle:: hir:: map:: Map ;
2121use rustc_middle:: lint:: in_external_macro;
22- use rustc_middle:: ty:: subst:: GenericArgKind ;
23- use rustc_middle:: ty:: { self , Ty , TyS } ;
22+ use rustc_middle:: ty:: { self , TraitRef , Ty , TyS } ;
2423use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
2524use rustc_span:: source_map:: Span ;
2625use rustc_span:: symbol:: { sym, SymbolStr } ;
2726
2827use crate :: consts:: { constant, Constant } ;
2928use crate :: utils:: usage:: mutated_variables;
3029use crate :: utils:: {
31- get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait, in_macro, is_copy ,
32- is_ctor_or_promotable_const_function , is_expn_of , is_self_ty , is_type_diagnostic_item, iter_input_pats,
30+ contains_ty , get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait, in_macro,
31+ is_copy , is_ctor_or_promotable_const_function , is_expn_of , is_type_diagnostic_item, iter_input_pats,
3332 last_path_segment, match_def_path, match_qpath, match_trait_method, match_type, match_var, method_calls,
3433 method_chain_args, paths, remove_blocks, return_ty, single_segment_path, snippet, snippet_with_applicability,
3534 snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_note, span_lint_and_sugg,
@@ -1656,16 +1655,8 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
16561655 if let hir:: ImplItemKind :: Fn ( _, _) = impl_item. kind {
16571656 let ret_ty = return_ty ( cx, impl_item. hir_id ) ;
16581657
1659- let contains_self_ty = |ty : Ty < ' tcx > | {
1660- ty. walk ( ) . any ( |inner| match inner. unpack ( ) {
1661- GenericArgKind :: Type ( inner_ty) => TyS :: same_type ( self_ty, inner_ty) ,
1662-
1663- GenericArgKind :: Lifetime ( _) | GenericArgKind :: Const ( _) => false ,
1664- } )
1665- } ;
1666-
16671658 // walk the return type and check for Self (this does not check associated types)
1668- if contains_self_ty ( ret_ty) {
1659+ if contains_ty ( ret_ty, self_ty ) {
16691660 return ;
16701661 }
16711662
@@ -1675,7 +1666,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
16751666 for & ( predicate, _span) in cx. tcx . predicates_of ( def_id) . predicates {
16761667 if let ty:: PredicateAtom :: Projection ( projection_predicate) = predicate. skip_binders ( ) {
16771668 // walk the associated type and check for Self
1678- if contains_self_ty ( projection_predicate. ty ) {
1669+ if contains_ty ( projection_predicate. ty , self_ty ) {
16791670 return ;
16801671 }
16811672 }
@@ -1696,44 +1687,23 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
16961687 fn check_trait_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx TraitItem < ' _ > ) {
16971688 if_chain ! {
16981689 if item. ident. name == sym!( new) ;
1699- if let TraitItemKind :: Fn ( FnSig { decl, .. } , _) = & item. kind;
1700- if let FnRetTy :: Return ( ret_ty) = & decl. output;
1690+ if let TraitItemKind :: Fn ( _, _) = item. kind;
1691+ let ret_ty = return_ty( cx, item. hir_id) ;
1692+ let self_ty = TraitRef :: identity( cx. tcx, item. hir_id. owner. to_def_id( ) ) . self_ty( ) ;
1693+ if !contains_ty( ret_ty, self_ty) ;
17011694
17021695 then {
1703- let mut visitor = HasSelfVisitor { has_self_ty: false } ;
1704- visitor. visit_ty( ret_ty) ;
1705- if !visitor. has_self_ty {
1706- span_lint(
1707- cx,
1708- NEW_RET_NO_SELF ,
1709- item. span,
1710- "methods called `new` usually return `Self`" ,
1711- ) ;
1712- }
1696+ span_lint(
1697+ cx,
1698+ NEW_RET_NO_SELF ,
1699+ item. span,
1700+ "methods called `new` usually return `Self`" ,
1701+ ) ;
17131702 }
17141703 }
17151704 }
17161705}
17171706
1718- struct HasSelfVisitor {
1719- pub has_self_ty : bool ,
1720- }
1721-
1722- impl < ' tcx > intravisit:: Visitor < ' tcx > for HasSelfVisitor {
1723- type Map = Map < ' tcx > ;
1724-
1725- fn visit_ty ( & mut self , ty : & ' tcx hir:: Ty < ' _ > ) {
1726- if is_self_ty ( ty) {
1727- self . has_self_ty = true ;
1728- } else {
1729- intravisit:: walk_ty ( self , ty) ;
1730- }
1731- }
1732- fn nested_visit_map ( & mut self ) -> intravisit:: NestedVisitorMap < Self :: Map > {
1733- intravisit:: NestedVisitorMap :: None
1734- }
1735- }
1736-
17371707/// Checks for the `OR_FUN_CALL` lint.
17381708#[ allow( clippy:: too_many_lines) ]
17391709fn lint_or_fun_call < ' tcx > (
0 commit comments