@@ -46,6 +46,27 @@ fn is_same_type<'tcx>(cx: &LateContext<'tcx>, ty_resolved_path: hir::def::Res, f
4646 false
4747}
4848
49+ fn func_hir_id_to_func_ty < ' tcx > ( cx : & LateContext < ' tcx > , hir_id : hir:: hir_id:: HirId ) -> Option < Ty < ' tcx > > {
50+ if let Some ( ( defkind, func_defid) ) = cx. typeck_results ( ) . type_dependent_def ( hir_id)
51+ && defkind == hir:: def:: DefKind :: AssocFn
52+ && let Some ( init_ty) = cx. tcx . type_of ( func_defid) . no_bound_vars ( )
53+ {
54+ Some ( init_ty)
55+ } else {
56+ None
57+ }
58+ }
59+
60+ fn func_ty_to_return_type < ' tcx > ( cx : & LateContext < ' tcx > , func_ty : Ty < ' tcx > ) -> Option < Ty < ' tcx > > {
61+ if func_ty. is_fn ( )
62+ && let Some ( return_type) = func_ty. fn_sig ( cx. tcx ) . output ( ) . no_bound_vars ( )
63+ {
64+ Some ( return_type)
65+ } else {
66+ None
67+ }
68+ }
69+
4970/// Extracts the fn Ty, e.g. `fn() -> std::string::String {f}`
5071fn extract_fn_ty < ' tcx > (
5172 cx : & LateContext < ' tcx > ,
@@ -66,16 +87,7 @@ fn extract_fn_ty<'tcx>(
6687 // Associated functions like
6788 // let a: String = String::new();
6889 // let a: String = String::get_string();
69- hir:: QPath :: TypeRelative ( ..) => {
70- if let Some ( ( defkind, func_defid) ) = cx. typeck_results ( ) . type_dependent_def ( call. hir_id )
71- && defkind == hir:: def:: DefKind :: AssocFn
72- && let Some ( init_ty) = cx. tcx . type_of ( func_defid) . no_bound_vars ( )
73- {
74- Some ( init_ty)
75- } else {
76- None
77- }
78- } ,
90+ hir:: QPath :: TypeRelative ( ..) => func_hir_id_to_func_ty ( cx, call. hir_id ) ,
7991 hir:: QPath :: LangItem ( ..) => None ,
8092 }
8193}
@@ -89,8 +101,7 @@ fn is_redundant_in_func_call<'tcx>(
89101 let func_type = extract_fn_ty ( cx, call, init_path) ;
90102
91103 if let Some ( func_type) = func_type
92- && func_type. is_fn ( )
93- && let Some ( init_return_type) = func_type. fn_sig ( cx. tcx ) . output ( ) . no_bound_vars ( )
104+ && let Some ( init_return_type) = func_ty_to_return_type ( cx, func_type)
94105 {
95106 return is_same_type ( cx, ty_resolved_path, init_return_type) ;
96107 }
@@ -117,8 +128,17 @@ impl LateLintPass<'_> for RedundantTypeAnnotations {
117128 span_lint ( cx, REDUNDANT_TYPE_ANNOTATIONS , local. span , "redundant type annotation" ) ;
118129 }
119130 } ,
131+ hir:: ExprKind :: MethodCall ( _, _, _, _) => {
132+ if let Some ( func_ty) = func_hir_id_to_func_ty ( cx, init. hir_id )
133+ && let Some ( return_type) = func_ty_to_return_type ( cx, func_ty)
134+ && is_same_type ( cx, resolved_path_ty. res , return_type)
135+ {
136+ span_lint ( cx, REDUNDANT_TYPE_ANNOTATIONS , local. span , "redundant type annotation" ) ;
137+ }
138+ } ,
120139 // When the initialization is a path for example u32::MAX
121140 hir:: ExprKind :: Path ( init_path) => {
141+ // TODO: check for non primty
122142 if let hir:: def:: Res :: PrimTy ( primty) = resolved_path_ty. res
123143
124144 && let hir:: QPath :: TypeRelative ( init_ty, _) = init_path
@@ -130,7 +150,7 @@ impl LateLintPass<'_> for RedundantTypeAnnotations {
130150 {
131151 span_lint ( cx, REDUNDANT_TYPE_ANNOTATIONS , local. span , "redundant type annotation" ) ;
132152 }
133- }
153+ } ,
134154 _ => ( )
135155 }
136156 } ;
0 commit comments