Skip to content

Commit fffd85b

Browse files
committed
Add check from_expansion
1 parent 8c97cfc commit fffd85b

File tree

3 files changed

+119
-59
lines changed

3 files changed

+119
-59
lines changed

clippy_lints/src/redundant_type_annotations.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ fn is_redundant_in_func_call<'tcx>(
102102
impl LateLintPass<'_> for RedundantTypeAnnotations {
103103
fn check_local<'tcx>(&mut self, cx: &LateContext<'tcx>, local: &'tcx rustc_hir::Local<'tcx>) {
104104
// type annotation part
105-
if let Some(ty) = &local.ty
105+
if !local.span.from_expansion()
106+
&& let Some(ty) = &local.ty
106107
&& let hir::TyKind::Path(ty_path) = &ty.kind
107108
&& let hir::QPath::Resolved(_, resolved_path_ty) = ty_path
108109

Lines changed: 90 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,132 @@
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+
struct Pie {
18+
inner: u32
19+
}
20+
21+
enum Pizza {
622
One,
723
Two,
824
}
925

10-
fn f() -> String {
26+
fn return_a_string() -> String {
1127
String::new()
1228
}
1329

14-
fn f_struct() -> A {
15-
A
30+
fn return_a_struct() -> Pie {
31+
Pie {
32+
inner: 5
33+
}
1634
}
1735

18-
fn f_enum() -> E {
19-
E::One
36+
fn return_an_enum() -> Pizza {
37+
Pizza::One
2038
}
2139

22-
struct ATest2 {
23-
inner: u32,
40+
fn return_an_int() -> u32 {
41+
5
2442
}
2543

26-
impl ATest2 {
27-
fn func(&self) -> u32 {
44+
impl Pie {
45+
fn return_an_int(&self) -> u32 {
2846
self.inner
2947
}
3048

31-
fn a(&self) {
32-
let v: u32 = self.func(); // This should lint but doesn't (MethodCall)
33-
}
34-
35-
fn get_num() -> u32 {
49+
fn associated_return_an_int() -> u32 {
3650
5
3751
}
3852

3953
fn new() -> Self {
4054
Self { inner: 5 }
4155
}
4256

43-
fn get_string() -> String {
57+
fn associated_return_a_string() -> String {
4458
String::from("")
4559
}
60+
61+
fn test_method_call(&self) {
62+
let v: u32 = self.return_an_int(); // This should lint but doesn't (MethodCall)
63+
}
4664
}
4765

48-
fn f_prim() -> u32 {
49-
5
66+
fn test_generics() {
67+
// The type annotation is needed to determine T
68+
let _c: Cake<i32> = make_something();
69+
70+
// The type annotation is needed to determine the topic
71+
let _c: Cake<u8> = make_cake();
72+
}
73+
74+
fn test_non_locals() {
75+
// This shouldn't lint
76+
fn _arg(x: u32) -> u32 {
77+
x
78+
}
79+
80+
// This could lint, but probably shouldn't
81+
let _closure_arg = |x: u32| x;
5082
}
5183

52-
fn main() {
53-
let a: String = f();
84+
fn test_complex_types() {
85+
// Shouldn't lint, since the literal will be i32 otherwise
86+
let _u8: u8 = 128;
87+
88+
// Should lint
89+
let _tuple_i32: (i32, i32) = (12, 13);
5490

55-
let a: A = f_struct();
91+
// Shouldn't lint, since the tuple will be i32 otherwise
92+
let _tuple_u32: (u32, u32) = (1, 2);
5693

57-
let a: E = f_enum();
94+
// Should lint, since the type is determined by the init value
95+
let _tuple_u32: (u32, u32) = (3_u32, 4_u32);
5896

59-
let a: u32 = f_prim();
97+
// Should lint
98+
let _array: [i32; 3] = [5, 6, 7];
99+
100+
// Shouldn't lint
101+
let _array: [u32; 2] = [8, 9];
102+
}
60103

61-
let a: String = String::new();
104+
fn test_functions() {
105+
// Everything here should lint
106+
107+
let _return: String = return_a_string();
62108

63-
let st: ATest2 = ATest2::new();
64-
let a: u32 = st.func(); // this should lint but doesn't (MethodCall)
109+
let _return: Pie = return_a_struct();
65110

66-
let a: String = String::from("test");
111+
let _return: Pizza = return_an_enum();
67112

68-
let a: u32 = u32::MAX;
113+
let _return: u32 = return_an_int();
69114

70-
let a: u32 = ATest2::get_num();
115+
let _return: String = String::new();
71116

72-
let a: String = ATest2::get_string();
117+
let new_pie: Pie = Pie::new();
118+
119+
let _return: u32 = new_pie.return_an_int(); // this should lint but doesn't (MethodCall)
120+
121+
let _return: String = String::from("test");
122+
123+
let _return: u32 = Pie::associated_return_an_int();
124+
125+
let _return: String = Pie::associated_return_a_string();
73126
}
127+
128+
fn test_simple_types() {
129+
let _var: u32 = u32::MAX;
130+
}
131+
132+
fn main() {}
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:107: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:109: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:111: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:113: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:115: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:117: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:123: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:125: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:129: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)