11use clippy_utils:: diagnostics:: span_lint;
2+ use rustc_ast:: LitKind ;
23use rustc_hir as hir;
34use rustc_lint:: { LateContext , LateLintPass } ;
45use rustc_middle:: ty:: Ty ;
@@ -115,21 +116,25 @@ impl LateLintPass<'_> for RedundantTypeAnnotations {
115116 // type annotation part
116117 if !local. span . from_expansion ( )
117118 && let Some ( ty) = & local. ty
118- && let hir:: TyKind :: Path ( ty_path) = & ty. kind
119- && let hir:: QPath :: Resolved ( _, resolved_path_ty) = ty_path
120119
121120 // initialization part
122121 && let Some ( init) = local. init
123122 {
124123 match & init. kind {
125124 // When the initialization is a call to a function
126125 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) {
128130 span_lint ( cx, REDUNDANT_TYPE_ANNOTATIONS , local. span , "redundant type annotation" ) ;
129131 }
130132 } ,
131133 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 )
133138 && let Some ( return_type) = func_ty_to_return_type ( cx, func_ty)
134139 && is_same_type ( cx, resolved_path_ty. res , return_type)
135140 {
@@ -139,7 +144,9 @@ impl LateLintPass<'_> for RedundantTypeAnnotations {
139144 // When the initialization is a path for example u32::MAX
140145 hir:: ExprKind :: Path ( init_path) => {
141146 // 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
143150
144151 && let hir:: QPath :: TypeRelative ( init_ty, _) = init_path
145152 && let hir:: TyKind :: Path ( init_ty_path) = & init_ty. kind
@@ -151,6 +158,25 @@ impl LateLintPass<'_> for RedundantTypeAnnotations {
151158 span_lint ( cx, REDUNDANT_TYPE_ANNOTATIONS , local. span , "redundant type annotation" ) ;
152159 }
153160 } ,
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+ }
154180 _ => ( )
155181 }
156182 } ;
0 commit comments