Skip to content

Commit 3cde377

Browse files
committed
Support literals
1 parent 35edab7 commit 3cde377

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
lines changed

clippy_lints/src/redundant_type_annotations.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint;
2+
use rustc_ast::LitKind;
23
use rustc_hir as hir;
34
use rustc_lint::{LateContext, LateLintPass};
45
use 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
};

tests/ui/redundant_type_annotations.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,17 @@ fn test_functions() {
142142
fn test_simple_types() {
143143
let _var: u32 = u32::MAX;
144144

145-
// Should lint (doesn't)
145+
// Should lint
146146
let _var: u32 = 5_u32;
147+
148+
// Should lint
149+
let _var: &str = "test";
150+
151+
// Should lint
152+
let _var: &[u8] = b"test";
153+
154+
// Should lint
155+
let _var: bool = false;
147156
}
148157

149158
fn main() {}

tests/ui/redundant_type_annotations.stderr

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,29 @@ error: redundant type annotation
6666
LL | let _var: u32 = u32::MAX;
6767
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6868

69-
error: aborting due to 11 previous errors
69+
error: redundant type annotation
70+
--> $DIR/redundant_type_annotations.rs:146:5
71+
|
72+
LL | let _var: u32 = 5_u32;
73+
| ^^^^^^^^^^^^^^^^^^^^^^
74+
75+
error: redundant type annotation
76+
--> $DIR/redundant_type_annotations.rs:149:5
77+
|
78+
LL | let _var: &str = "test";
79+
| ^^^^^^^^^^^^^^^^^^^^^^^^
80+
81+
error: redundant type annotation
82+
--> $DIR/redundant_type_annotations.rs:152:5
83+
|
84+
LL | let _var: &[u8] = b"test";
85+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
86+
87+
error: redundant type annotation
88+
--> $DIR/redundant_type_annotations.rs:155:5
89+
|
90+
LL | let _var: bool = false;
91+
| ^^^^^^^^^^^^^^^^^^^^^^^
92+
93+
error: aborting due to 15 previous errors
7094

0 commit comments

Comments
 (0)