@@ -15,21 +15,20 @@ use rustc_ast::ast;
15
15
use rustc_errors:: Applicability ;
16
16
use rustc_hir as hir;
17
17
use rustc_hir:: intravisit:: { self , Visitor } ;
18
- use rustc_hir:: { FnRetTy , FnSig , TraitItem , TraitItemKind } ;
18
+ use rustc_hir:: { TraitItem , TraitItemKind } ;
19
19
use rustc_lint:: { LateContext , LateLintPass , Lint , LintContext } ;
20
20
use rustc_middle:: hir:: map:: Map ;
21
21
use 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 } ;
24
23
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
25
24
use rustc_span:: source_map:: Span ;
26
25
use rustc_span:: symbol:: { sym, SymbolStr } ;
27
26
28
27
use crate :: consts:: { constant, Constant } ;
29
28
use crate :: utils:: usage:: mutated_variables;
30
29
use 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,
33
32
last_path_segment, match_def_path, match_qpath, match_trait_method, match_type, match_var, method_calls,
34
33
method_chain_args, paths, remove_blocks, return_ty, single_segment_path, snippet, snippet_with_applicability,
35
34
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 {
1656
1655
if let hir:: ImplItemKind :: Fn ( _, _) = impl_item. kind {
1657
1656
let ret_ty = return_ty ( cx, impl_item. hir_id ) ;
1658
1657
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
-
1667
1658
// 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 ) {
1669
1660
return ;
1670
1661
}
1671
1662
@@ -1675,7 +1666,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
1675
1666
for & ( predicate, _span) in cx. tcx . predicates_of ( def_id) . predicates {
1676
1667
if let ty:: PredicateAtom :: Projection ( projection_predicate) = predicate. skip_binders ( ) {
1677
1668
// 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 ) {
1679
1670
return ;
1680
1671
}
1681
1672
}
@@ -1696,44 +1687,23 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
1696
1687
fn check_trait_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx TraitItem < ' _ > ) {
1697
1688
if_chain ! {
1698
1689
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) ;
1701
1694
1702
1695
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
+ ) ;
1713
1702
}
1714
1703
}
1715
1704
}
1716
1705
}
1717
1706
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
-
1737
1707
/// Checks for the `OR_FUN_CALL` lint.
1738
1708
#[ allow( clippy:: too_many_lines) ]
1739
1709
fn lint_or_fun_call < ' tcx > (
0 commit comments