1
1
use clippy_utils:: diagnostics:: span_lint;
2
+ use rustc_ast:: LitKind ;
2
3
use rustc_hir as hir;
3
4
use rustc_lint:: { LateContext , LateLintPass } ;
4
5
use rustc_middle:: ty:: Ty ;
@@ -115,21 +116,25 @@ impl LateLintPass<'_> for RedundantTypeAnnotations {
115
116
// type annotation part
116
117
if !local. span . from_expansion ( )
117
118
&& let Some ( ty) = & local. ty
118
- && let hir:: TyKind :: Path ( ty_path) = & ty. kind
119
- && let hir:: QPath :: Resolved ( _, resolved_path_ty) = ty_path
120
119
121
120
// initialization part
122
121
&& let Some ( init) = local. init
123
122
{
124
123
match & init. kind {
125
124
// When the initialization is a call to a function
126
125
hir:: ExprKind :: Call ( init_call, _) => {
127
- if is_redundant_in_func_call ( cx, resolved_path_ty. res , init_call) {
126
+ if let hir:: TyKind :: Path ( ty_path) = & ty. kind
127
+ && let hir:: QPath :: Resolved ( _, resolved_path_ty) = ty_path
128
+
129
+ && is_redundant_in_func_call ( cx, resolved_path_ty. res , init_call) {
128
130
span_lint ( cx, REDUNDANT_TYPE_ANNOTATIONS , local. span , "redundant type annotation" ) ;
129
131
}
130
132
} ,
131
133
hir:: ExprKind :: MethodCall ( _, _, _, _) => {
132
- if let Some ( func_ty) = func_hir_id_to_func_ty ( cx, init. hir_id )
134
+ if let hir:: TyKind :: Path ( ty_path) = & ty. kind
135
+ && let hir:: QPath :: Resolved ( _, resolved_path_ty) = ty_path
136
+
137
+ && let Some ( func_ty) = func_hir_id_to_func_ty ( cx, init. hir_id )
133
138
&& let Some ( return_type) = func_ty_to_return_type ( cx, func_ty)
134
139
&& is_same_type ( cx, resolved_path_ty. res , return_type)
135
140
{
@@ -139,7 +144,9 @@ impl LateLintPass<'_> for RedundantTypeAnnotations {
139
144
// When the initialization is a path for example u32::MAX
140
145
hir:: ExprKind :: Path ( init_path) => {
141
146
// TODO: check for non primty
142
- if let hir:: def:: Res :: PrimTy ( primty) = resolved_path_ty. res
147
+ if let hir:: TyKind :: Path ( ty_path) = & ty. kind
148
+ && let hir:: QPath :: Resolved ( _, resolved_path_ty) = ty_path
149
+ && let hir:: def:: Res :: PrimTy ( primty) = resolved_path_ty. res
143
150
144
151
&& let hir:: QPath :: TypeRelative ( init_ty, _) = init_path
145
152
&& let hir:: TyKind :: Path ( init_ty_path) = & init_ty. kind
@@ -151,6 +158,25 @@ impl LateLintPass<'_> for RedundantTypeAnnotations {
151
158
span_lint ( cx, REDUNDANT_TYPE_ANNOTATIONS , local. span , "redundant type annotation" ) ;
152
159
}
153
160
} ,
161
+ hir:: ExprKind :: Lit ( init_lit) => {
162
+ match init_lit. node {
163
+ // In these cases the annotation is redundant
164
+ LitKind :: Str ( ..)
165
+ | LitKind :: ByteStr ( ..)
166
+ | LitKind :: Byte ( ..)
167
+ | LitKind :: Char ( ..)
168
+ | LitKind :: Bool ( ..) => {
169
+ span_lint ( cx, REDUNDANT_TYPE_ANNOTATIONS , local. span , "redundant type annotation" ) ;
170
+ } ,
171
+ LitKind :: Int ( ..) | LitKind :: Float ( ..) => {
172
+ // If the initialization value is a suffixed literal we lint
173
+ if init_lit. node . is_suffixed ( ) {
174
+ span_lint ( cx, REDUNDANT_TYPE_ANNOTATIONS , local. span , "redundant type annotation" ) ;
175
+ }
176
+ } ,
177
+ LitKind :: Err => ( ) ,
178
+ }
179
+ }
154
180
_ => ( )
155
181
}
156
182
} ;
0 commit comments