@@ -46,6 +46,27 @@ fn is_same_type<'tcx>(cx: &LateContext<'tcx>, ty_resolved_path: hir::def::Res, f
46
46
false
47
47
}
48
48
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
+
49
70
/// Extracts the fn Ty, e.g. `fn() -> std::string::String {f}`
50
71
fn extract_fn_ty < ' tcx > (
51
72
cx : & LateContext < ' tcx > ,
@@ -66,16 +87,7 @@ fn extract_fn_ty<'tcx>(
66
87
// Associated functions like
67
88
// let a: String = String::new();
68
89
// 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 ) ,
79
91
hir:: QPath :: LangItem ( ..) => None ,
80
92
}
81
93
}
@@ -89,8 +101,7 @@ fn is_redundant_in_func_call<'tcx>(
89
101
let func_type = extract_fn_ty ( cx, call, init_path) ;
90
102
91
103
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)
94
105
{
95
106
return is_same_type ( cx, ty_resolved_path, init_return_type) ;
96
107
}
@@ -117,8 +128,17 @@ impl LateLintPass<'_> for RedundantTypeAnnotations {
117
128
span_lint ( cx, REDUNDANT_TYPE_ANNOTATIONS , local. span , "redundant type annotation" ) ;
118
129
}
119
130
} ,
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
+ } ,
120
139
// When the initialization is a path for example u32::MAX
121
140
hir:: ExprKind :: Path ( init_path) => {
141
+ // TODO: check for non primty
122
142
if let hir:: def:: Res :: PrimTy ( primty) = resolved_path_ty. res
123
143
124
144
&& let hir:: QPath :: TypeRelative ( init_ty, _) = init_path
@@ -130,7 +150,7 @@ impl LateLintPass<'_> for RedundantTypeAnnotations {
130
150
{
131
151
span_lint ( cx, REDUNDANT_TYPE_ANNOTATIONS , local. span , "redundant type annotation" ) ;
132
152
}
133
- }
153
+ } ,
134
154
_ => ( )
135
155
}
136
156
} ;
0 commit comments