|
1 | 1 | #![allow(unused)]
|
2 | 2 | #![warn(clippy::redundant_type_annotations)]
|
3 | 3 |
|
4 |
| -struct A; |
5 |
| -enum E { |
| 4 | +#[derive(Debug, Default)] |
| 5 | +struct Cake<T> { |
| 6 | + _data: T, |
| 7 | +} |
| 8 | + |
| 9 | +fn make_something<T: Default>() -> T { |
| 10 | + T::default() |
| 11 | +} |
| 12 | + |
| 13 | +fn make_cake<T: Default>() -> Cake<T> { |
| 14 | + Cake::<T>::default() |
| 15 | +} |
| 16 | + |
| 17 | +fn plus_one<T: std::ops::Add<u8, Output = T>>(val: T) -> T { |
| 18 | + val + 1 |
| 19 | +} |
| 20 | + |
| 21 | +struct Pie { |
| 22 | + inner: u32, |
| 23 | +} |
| 24 | + |
| 25 | +enum Pizza { |
6 | 26 | One,
|
7 | 27 | Two,
|
8 | 28 | }
|
9 | 29 |
|
10 |
| -fn f() -> String { |
| 30 | +fn return_a_string() -> String { |
11 | 31 | String::new()
|
12 | 32 | }
|
13 | 33 |
|
14 |
| -fn f_struct() -> A { |
15 |
| - A |
| 34 | +fn return_a_struct() -> Pie { |
| 35 | + Pie { inner: 5 } |
16 | 36 | }
|
17 | 37 |
|
18 |
| -fn f_enum() -> E { |
19 |
| - E::One |
| 38 | +fn return_an_enum() -> Pizza { |
| 39 | + Pizza::One |
20 | 40 | }
|
21 | 41 |
|
22 |
| -struct ATest2 { |
23 |
| - inner: u32, |
| 42 | +fn return_an_int() -> u32 { |
| 43 | + 5 |
24 | 44 | }
|
25 | 45 |
|
26 |
| -impl ATest2 { |
27 |
| - fn func(&self) -> u32 { |
| 46 | +impl Pie { |
| 47 | + fn return_an_int(&self) -> u32 { |
28 | 48 | self.inner
|
29 | 49 | }
|
30 | 50 |
|
31 |
| - fn a(&self) { |
32 |
| - let v: u32 = self.func(); // This should lint but doesn't (MethodCall) |
33 |
| - } |
34 |
| - |
35 |
| - fn get_num() -> u32 { |
| 51 | + fn associated_return_an_int() -> u32 { |
36 | 52 | 5
|
37 | 53 | }
|
38 | 54 |
|
39 | 55 | fn new() -> Self {
|
40 | 56 | Self { inner: 5 }
|
41 | 57 | }
|
42 | 58 |
|
43 |
| - fn get_string() -> String { |
| 59 | + fn associated_return_a_string() -> String { |
44 | 60 | String::from("")
|
45 | 61 | }
|
| 62 | + |
| 63 | + fn test_method_call(&self) { |
| 64 | + let v: u32 = self.return_an_int(); // This should lint but doesn't (MethodCall) |
| 65 | + } |
46 | 66 | }
|
47 | 67 |
|
48 |
| -fn f_prim() -> u32 { |
49 |
| - 5 |
| 68 | +fn test_generics() { |
| 69 | + // The type annotation is needed to determine T |
| 70 | + let _c: Cake<i32> = make_something(); |
| 71 | + |
| 72 | + // The type annotation is needed to determine the topic |
| 73 | + let _c: Cake<u8> = make_cake(); |
| 74 | + |
| 75 | + // This should lint (doesn't) |
| 76 | + let _c: Cake<u8> = make_cake::<u8>(); |
| 77 | + |
| 78 | + // This should lint (doesn't) |
| 79 | + let _c: u8 = make_something::<u8>(); |
| 80 | + |
| 81 | + // This should lint (doesn't) |
| 82 | + let _c: u8 = plus_one(5_u8); |
| 83 | + |
| 84 | + // Annotation needed otherwise T is i32 |
| 85 | + let _c: u8 = plus_one(5); |
| 86 | +} |
| 87 | + |
| 88 | +fn test_non_locals() { |
| 89 | + // This shouldn't lint |
| 90 | + fn _arg(x: u32) -> u32 { |
| 91 | + x |
| 92 | + } |
| 93 | + |
| 94 | + // This could lint, but probably shouldn't |
| 95 | + let _closure_arg = |x: u32| x; |
| 96 | +} |
| 97 | + |
| 98 | +fn test_complex_types() { |
| 99 | + // Shouldn't lint, since the literal will be i32 otherwise |
| 100 | + let _u8: u8 = 128; |
| 101 | + |
| 102 | + // Should lint (doesn't) |
| 103 | + let _tuple_i32: (i32, i32) = (12, 13); |
| 104 | + |
| 105 | + // Shouldn't lint, since the tuple will be i32 otherwise |
| 106 | + let _tuple_u32: (u32, u32) = (1, 2); |
| 107 | + |
| 108 | + // Should lint, since the type is determined by the init value (doesn't) |
| 109 | + let _tuple_u32: (u32, u32) = (3_u32, 4_u32); |
| 110 | + |
| 111 | + // Should lint (doesn't) |
| 112 | + let _array: [i32; 3] = [5, 6, 7]; |
| 113 | + |
| 114 | + // Shouldn't lint |
| 115 | + let _array: [u32; 2] = [8, 9]; |
50 | 116 | }
|
51 | 117 |
|
52 |
| -fn main() { |
53 |
| - let a: String = f(); |
| 118 | +fn test_functions() { |
| 119 | + // Everything here should lint |
| 120 | + |
| 121 | + let _return: String = return_a_string(); |
54 | 122 |
|
55 |
| - let a: A = f_struct(); |
| 123 | + let _return: Pie = return_a_struct(); |
56 | 124 |
|
57 |
| - let a: E = f_enum(); |
| 125 | + let _return: Pizza = return_an_enum(); |
58 | 126 |
|
59 |
| - let a: u32 = f_prim(); |
| 127 | + let _return: u32 = return_an_int(); |
60 | 128 |
|
61 |
| - let a: String = String::new(); |
| 129 | + let _return: String = String::new(); |
62 | 130 |
|
63 |
| - let st: ATest2 = ATest2::new(); |
64 |
| - let a: u32 = st.func(); // this should lint but doesn't (MethodCall) |
| 131 | + let new_pie: Pie = Pie::new(); |
65 | 132 |
|
66 |
| - let a: String = String::from("test"); |
| 133 | + let _return: u32 = new_pie.return_an_int(); // this should lint but doesn't (MethodCall) |
67 | 134 |
|
68 |
| - let a: u32 = u32::MAX; |
| 135 | + let _return: String = String::from("test"); |
69 | 136 |
|
70 |
| - let a: u32 = ATest2::get_num(); |
| 137 | + let _return: u32 = Pie::associated_return_an_int(); |
71 | 138 |
|
72 |
| - let a: String = ATest2::get_string(); |
| 139 | + let _return: String = Pie::associated_return_a_string(); |
73 | 140 | }
|
| 141 | + |
| 142 | +fn test_simple_types() { |
| 143 | + let _var: u32 = u32::MAX; |
| 144 | + |
| 145 | + // Should lint (doesn't) |
| 146 | + let _var: u32 = 5_u32; |
| 147 | +} |
| 148 | + |
| 149 | +fn main() {} |
| 150 | + |
| 151 | +// TODO: test refs |
0 commit comments