1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
#![allow(unused)]
#![warn(clippy::redundant_type_annotations)]
#[derive(Debug, Default)]
struct Cake<T> {
_data: T,
}
fn make_something<T>() -> T {
unimplemented!()
}
fn make_cake<T: Default>() -> Cake<T> {
Cake::<T>::default()
}
fn plus_one<T: std::ops::Add<u8, Output = T>>(val: T) -> T {
val + 1
}
#[derive(Default)]
struct Slice {
inner: u32,
}
#[derive(Default)]
struct Pie {
inner: u32,
inner_struct: Slice,
}
enum Pizza {
One,
Two,
}
fn return_a_string() -> String {
String::new()
}
fn return_a_struct() -> Pie {
Pie::default()
}
fn return_an_enum() -> Pizza {
Pizza::One
}
fn return_an_int() -> u32 {
5
}
impl Pie {
fn return_an_int(&self) -> u32 {
self.inner
}
fn return_a_ref(&self) -> &u32 {
&self.inner
}
fn return_a_ref_to_struct(&self) -> &Slice {
&self.inner_struct
}
fn associated_return_an_int() -> u32 {
5
}
fn new() -> Self {
Self::default()
}
fn associated_return_a_string() -> String {
String::from("")
}
fn test_method_call(&self) {
// Everything here should be lint
let v: u32 = self.return_an_int();
//~^ ERROR: redundant type annotation
//~| NOTE: `-D clippy::redundant-type-annotations` implied by `-D warnings`
let v: &u32 = self.return_a_ref();
//~^ ERROR: redundant type annotation
let v: &Slice = self.return_a_ref_to_struct();
//~^ ERROR: redundant type annotation
}
}
fn test_generics() {
// The type annotation is needed to determine T
let _c: Cake<i32> = make_something();
// The type annotation is needed to determine the topic
let _c: Cake<u8> = make_cake();
// This could be lint, but currently doesn't
let _c: Cake<u8> = make_cake::<u8>();
// This could be lint, but currently doesn't
let _c: u8 = make_something::<u8>();
// This could be lint, but currently doesn't
let _c: u8 = plus_one(5_u8);
// Annotation needed otherwise T is i32
let _c: u8 = plus_one(5);
// This could be lint, but currently doesn't
let _return: String = String::from("test");
}
fn test_non_locals() {
// This shouldn't be lint
fn _arg(x: u32) -> u32 {
x
}
// This could lint, but probably shouldn't
let _closure_arg = |x: u32| x;
}
trait Trait {
type AssocTy;
}
impl Trait for () {
type AssocTy = String;
}
fn test_complex_types<T>() {
// Shouldn't be lint, since the literal will be i32 otherwise
let _u8: u8 = 128;
// This could be lint, but currently doesn't
let _tuple_i32: (i32, i32) = (12, 13);
// Shouldn't be lint, since the tuple will be i32 otherwise
let _tuple_u32: (u32, u32) = (1, 2);
// Should be lint, since the type is determined by the init value, but currently doesn't
let _tuple_u32: (u32, u32) = (3_u32, 4_u32);
// This could be lint, but currently doesn't
let _array: [i32; 3] = [5, 6, 7];
// Shouldn't be lint
let _array: [u32; 2] = [8, 9];
let ty_param: T = make_something();
let assoc_ty: <() as Trait>::AssocTy = String::new();
}
fn test_functions() {
// Everything here should be lint
let _return: String = return_a_string();
//~^ ERROR: redundant type annotation
let _return: Pie = return_a_struct();
//~^ ERROR: redundant type annotation
let _return: Pizza = return_an_enum();
//~^ ERROR: redundant type annotation
let _return: u32 = return_an_int();
//~^ ERROR: redundant type annotation
let _return: String = String::new();
//~^ ERROR: redundant type annotation
let new_pie: Pie = Pie::new();
//~^ ERROR: redundant type annotation
let _return: u32 = new_pie.return_an_int();
//~^ ERROR: redundant type annotation
let _return: u32 = Pie::associated_return_an_int();
//~^ ERROR: redundant type annotation
let _return: String = Pie::associated_return_a_string();
//~^ ERROR: redundant type annotation
}
fn test_simple_types() {
// Everything here should be lint
let _var: u32 = u32::MAX;
//~^ ERROR: redundant type annotation
let _var: u32 = 5_u32;
//~^ ERROR: redundant type annotation
let _var: &str = "test";
//~^ ERROR: redundant type annotation
let _var: &[u8] = b"test";
//~^ ERROR: redundant type annotation
let _var: bool = false;
//~^ ERROR: redundant type annotation
}
fn issue11190() {}
fn main() {}
|