Skip to content

Commit e858255

Browse files
committed
Refactor tests
1 parent b559103 commit e858255

File tree

2 files changed

+136
-58
lines changed

2 files changed

+136
-58
lines changed
Lines changed: 109 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,151 @@
11
#![allow(unused)]
22
#![warn(clippy::redundant_type_annotations)]
33

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 {
626
One,
727
Two,
828
}
929

10-
fn f() -> String {
30+
fn return_a_string() -> String {
1131
String::new()
1232
}
1333

14-
fn f_struct() -> A {
15-
A
34+
fn return_a_struct() -> Pie {
35+
Pie { inner: 5 }
1636
}
1737

18-
fn f_enum() -> E {
19-
E::One
38+
fn return_an_enum() -> Pizza {
39+
Pizza::One
2040
}
2141

22-
struct ATest2 {
23-
inner: u32,
42+
fn return_an_int() -> u32 {
43+
5
2444
}
2545

26-
impl ATest2 {
27-
fn func(&self) -> u32 {
46+
impl Pie {
47+
fn return_an_int(&self) -> u32 {
2848
self.inner
2949
}
3050

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 {
3652
5
3753
}
3854

3955
fn new() -> Self {
4056
Self { inner: 5 }
4157
}
4258

43-
fn get_string() -> String {
59+
fn associated_return_a_string() -> String {
4460
String::from("")
4561
}
62+
63+
fn test_method_call(&self) {
64+
let v: u32 = self.return_an_int(); // This should lint but doesn't (MethodCall)
65+
}
4666
}
4767

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];
50116
}
51117

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();
54122

55-
let a: A = f_struct();
123+
let _return: Pie = return_a_struct();
56124

57-
let a: E = f_enum();
125+
let _return: Pizza = return_an_enum();
58126

59-
let a: u32 = f_prim();
127+
let _return: u32 = return_an_int();
60128

61-
let a: String = String::new();
129+
let _return: String = String::new();
62130

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();
65132

66-
let a: String = String::from("test");
133+
let _return: u32 = new_pie.return_an_int(); // this should lint but doesn't (MethodCall)
67134

68-
let a: u32 = u32::MAX;
135+
let _return: String = String::from("test");
69136

70-
let a: u32 = ATest2::get_num();
137+
let _return: u32 = Pie::associated_return_an_int();
71138

72-
let a: String = ATest2::get_string();
139+
let _return: String = Pie::associated_return_a_string();
73140
}
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
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,58 @@
11
error: redundant type annotation
2-
--> $DIR/redundant_type_annotations.rs:53:5
2+
--> $DIR/redundant_type_annotations.rs:118:5
33
|
4-
LL | let a: String = f();
5-
| ^^^^^^^^^^^^^^^^^^^^
4+
LL | let _return: String = return_a_string();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::redundant-type-annotations` implied by `-D warnings`
88

99
error: redundant type annotation
10-
--> $DIR/redundant_type_annotations.rs:55:5
10+
--> $DIR/redundant_type_annotations.rs:120:5
1111
|
12-
LL | let a: A = f_struct();
13-
| ^^^^^^^^^^^^^^^^^^^^^^
12+
LL | let _return: Pie = return_a_struct();
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

1515
error: redundant type annotation
16-
--> $DIR/redundant_type_annotations.rs:57:5
16+
--> $DIR/redundant_type_annotations.rs:122:5
1717
|
18-
LL | let a: E = f_enum();
19-
| ^^^^^^^^^^^^^^^^^^^^
18+
LL | let _return: Pizza = return_an_enum();
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2020

2121
error: redundant type annotation
22-
--> $DIR/redundant_type_annotations.rs:59:5
22+
--> $DIR/redundant_type_annotations.rs:124:5
2323
|
24-
LL | let a: u32 = f_prim();
25-
| ^^^^^^^^^^^^^^^^^^^^^^
24+
LL | let _return: u32 = return_an_int();
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2626

2727
error: redundant type annotation
28-
--> $DIR/redundant_type_annotations.rs:61:5
28+
--> $DIR/redundant_type_annotations.rs:126:5
2929
|
30-
LL | let a: String = String::new();
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
LL | let _return: String = String::new();
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3232

3333
error: redundant type annotation
34-
--> $DIR/redundant_type_annotations.rs:63:5
34+
--> $DIR/redundant_type_annotations.rs:128:5
3535
|
36-
LL | let st: ATest2 = ATest2::new();
37-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
36+
LL | let new_pie: Pie = Pie::new();
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3838

3939
error: redundant type annotation
40-
--> $DIR/redundant_type_annotations.rs:68:5
40+
--> $DIR/redundant_type_annotations.rs:134:5
4141
|
42-
LL | let a: u32 = u32::MAX;
43-
| ^^^^^^^^^^^^^^^^^^^^^^
42+
LL | let _return: u32 = Pie::associated_return_an_int();
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4444

4545
error: redundant type annotation
46-
--> $DIR/redundant_type_annotations.rs:70:5
46+
--> $DIR/redundant_type_annotations.rs:136:5
4747
|
48-
LL | let a: u32 = ATest2::get_num();
49-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
LL | let _return: String = Pie::associated_return_a_string();
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5050

5151
error: redundant type annotation
52-
--> $DIR/redundant_type_annotations.rs:72:5
52+
--> $DIR/redundant_type_annotations.rs:140:5
5353
|
54-
LL | let a: String = ATest2::get_string();
55-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
54+
LL | let _var: u32 = u32::MAX;
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
5656

5757
error: aborting due to 9 previous errors
5858

0 commit comments

Comments
 (0)