Skip to content

Commit 0583a29

Browse files
committed
Add support to returned refs from MethodCall
1 parent 4f6eafe commit 0583a29

File tree

3 files changed

+83
-46
lines changed

3 files changed

+83
-46
lines changed

clippy_lints/src/redundant_type_annotations.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ fn func_hir_id_to_func_ty<'tcx>(cx: &LateContext<'tcx>, hir_id: hir::hir_id::Hir
5959
}
6060

6161
fn func_ty_to_return_type<'tcx>(cx: &LateContext<'tcx>, func_ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
62-
if func_ty.is_fn()
63-
&& let Some(return_type) = func_ty.fn_sig(cx.tcx).output().no_bound_vars()
64-
{
65-
Some(return_type)
62+
if func_ty.is_fn() {
63+
Some(func_ty.fn_sig(cx.tcx).output().skip_binder())
6664
} else {
6765
None
6866
}
@@ -142,12 +140,23 @@ impl LateLintPass<'_> for RedundantTypeAnnotations {
142140
}
143141
},
144142
hir::ExprKind::MethodCall(_, _, _, _) => {
145-
if let hir::TyKind::Path(ty_path) = &ty.kind
146-
&& let hir::QPath::Resolved(_, resolved_path_ty) = ty_path
143+
let mut is_ref = false;
144+
let mut ty_kind = &ty.kind;
147145

146+
// If the annotation is a ref we "peel" it
147+
if let hir::TyKind::Ref(_, mut_ty) = &ty.kind {
148+
is_ref = true;
149+
ty_kind = &mut_ty.ty.kind;
150+
}
151+
152+
if let hir::TyKind::Path(ty_path) = ty_kind
153+
&& let hir::QPath::Resolved(_, resolved_path_ty) = ty_path
148154
&& let Some(func_ty) = func_hir_id_to_func_ty(cx, init.hir_id)
149155
&& let Some(return_type) = func_ty_to_return_type(cx, func_ty)
150-
&& is_same_type(cx, resolved_path_ty.res, return_type)
156+
&& is_same_type(cx, resolved_path_ty.res, match is_ref {
157+
true => return_type.peel_refs(),
158+
false => return_type,
159+
})
151160
{
152161
span_lint(cx, REDUNDANT_TYPE_ANNOTATIONS, local.span, "redundant type annotation");
153162
}

tests/ui/redundant_type_annotations.rs

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@ fn plus_one<T: std::ops::Add<u8, Output = T>>(val: T) -> T {
1818
val + 1
1919
}
2020

21+
#[derive(Default)]
22+
struct Slice {
23+
inner: u32,
24+
}
25+
26+
#[derive(Default)]
2127
struct Pie {
2228
inner: u32,
29+
inner_struct: Slice,
2330
}
2431

2532
enum Pizza {
@@ -32,7 +39,7 @@ fn return_a_string() -> String {
3239
}
3340

3441
fn return_a_struct() -> Pie {
35-
Pie { inner: 5 }
42+
Pie::default()
3643
}
3744

3845
fn return_an_enum() -> Pizza {
@@ -48,20 +55,32 @@ impl Pie {
4855
self.inner
4956
}
5057

58+
fn return_a_ref(&self) -> &u32 {
59+
&self.inner
60+
}
61+
62+
fn return_a_ref_to_struct(&self) -> &Slice {
63+
&self.inner_struct
64+
}
65+
5166
fn associated_return_an_int() -> u32 {
5267
5
5368
}
5469

5570
fn new() -> Self {
56-
Self { inner: 5 }
71+
Self::default()
5772
}
5873

5974
fn associated_return_a_string() -> String {
6075
String::from("")
6176
}
6277

6378
fn test_method_call(&self) {
64-
let v: u32 = self.return_an_int(); // Should lint
79+
// Everything here should be lint
80+
81+
let v: u32 = self.return_an_int();
82+
let v: &u32 = self.return_a_ref();
83+
let v: &Slice = self.return_a_ref_to_struct();
6584
}
6685
}
6786

@@ -72,21 +91,24 @@ fn test_generics() {
7291
// The type annotation is needed to determine the topic
7392
let _c: Cake<u8> = make_cake();
7493

75-
// This should lint (doesn't)
94+
// This could be lint, but currently doesn't
7695
let _c: Cake<u8> = make_cake::<u8>();
7796

78-
// This should lint (doesn't)
97+
// This could be lint, but currently doesn't
7998
let _c: u8 = make_something::<u8>();
8099

81-
// This should lint (doesn't)
100+
// This could be lint, but currently doesn't
82101
let _c: u8 = plus_one(5_u8);
83102

84103
// Annotation needed otherwise T is i32
85104
let _c: u8 = plus_one(5);
105+
106+
// This could be lint, but currently doesn't
107+
let _return: String = String::from("test");
86108
}
87109

88110
fn test_non_locals() {
89-
// This shouldn't lint
111+
// This shouldn't be lint
90112
fn _arg(x: u32) -> u32 {
91113
x
92114
}
@@ -96,27 +118,27 @@ fn test_non_locals() {
96118
}
97119

98120
fn test_complex_types() {
99-
// Shouldn't lint, since the literal will be i32 otherwise
121+
// Shouldn't be lint, since the literal will be i32 otherwise
100122
let _u8: u8 = 128;
101123

102-
// Should lint (doesn't)
124+
// This could be lint, but currently doesn't
103125
let _tuple_i32: (i32, i32) = (12, 13);
104126

105-
// Shouldn't lint, since the tuple will be i32 otherwise
127+
// Shouldn't be lint, since the tuple will be i32 otherwise
106128
let _tuple_u32: (u32, u32) = (1, 2);
107129

108-
// Should lint, since the type is determined by the init value (doesn't)
130+
// Should be lint, since the type is determined by the init value, but currently doesn't
109131
let _tuple_u32: (u32, u32) = (3_u32, 4_u32);
110132

111-
// Should lint (doesn't)
133+
// This could be lint, but currently doesn't
112134
let _array: [i32; 3] = [5, 6, 7];
113135

114-
// Shouldn't lint
136+
// Shouldn't be lint
115137
let _array: [u32; 2] = [8, 9];
116138
}
117139

118140
fn test_functions() {
119-
// Everything here should lint
141+
// Everything here should be lint
120142

121143
let _return: String = return_a_string();
122144

@@ -132,29 +154,23 @@ fn test_functions() {
132154

133155
let _return: u32 = new_pie.return_an_int();
134156

135-
let _return: String = String::from("test");
136-
137157
let _return: u32 = Pie::associated_return_an_int();
138158

139159
let _return: String = Pie::associated_return_a_string();
140160
}
141161

142162
fn test_simple_types() {
163+
// Everything here should be lint
164+
143165
let _var: u32 = u32::MAX;
144166

145-
// Should lint
146167
let _var: u32 = 5_u32;
147168

148-
// Should lint
149169
let _var: &str = "test";
150170

151-
// Should lint
152171
let _var: &[u8] = b"test";
153172

154-
// Should lint
155173
let _var: bool = false;
156174
}
157175

158176
fn main() {}
159-
160-
// TODO: test refs
Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,106 @@
11
error: redundant type annotation
2-
--> $DIR/redundant_type_annotations.rs:64:9
2+
--> $DIR/redundant_type_annotations.rs:81:9
33
|
4-
LL | let v: u32 = self.return_an_int(); // Should lint
4+
LL | let v: u32 = self.return_an_int();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::redundant-type-annotations` implied by `-D warnings`
88

99
error: redundant type annotation
10-
--> $DIR/redundant_type_annotations.rs:121:5
10+
--> $DIR/redundant_type_annotations.rs:82:9
11+
|
12+
LL | let v: &u32 = self.return_a_ref();
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error: redundant type annotation
16+
--> $DIR/redundant_type_annotations.rs:83:9
17+
|
18+
LL | let v: &Slice = self.return_a_ref_to_struct();
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
21+
error: redundant type annotation
22+
--> $DIR/redundant_type_annotations.rs:140:5
1123
|
1224
LL | let _return: String = return_a_string();
1325
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1426

1527
error: redundant type annotation
16-
--> $DIR/redundant_type_annotations.rs:123:5
28+
--> $DIR/redundant_type_annotations.rs:142:5
1729
|
1830
LL | let _return: Pie = return_a_struct();
1931
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2032

2133
error: redundant type annotation
22-
--> $DIR/redundant_type_annotations.rs:125:5
34+
--> $DIR/redundant_type_annotations.rs:144:5
2335
|
2436
LL | let _return: Pizza = return_an_enum();
2537
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2638

2739
error: redundant type annotation
28-
--> $DIR/redundant_type_annotations.rs:127:5
40+
--> $DIR/redundant_type_annotations.rs:146:5
2941
|
3042
LL | let _return: u32 = return_an_int();
3143
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3244

3345
error: redundant type annotation
34-
--> $DIR/redundant_type_annotations.rs:129:5
46+
--> $DIR/redundant_type_annotations.rs:148:5
3547
|
3648
LL | let _return: String = String::new();
3749
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3850

3951
error: redundant type annotation
40-
--> $DIR/redundant_type_annotations.rs:131:5
52+
--> $DIR/redundant_type_annotations.rs:150:5
4153
|
4254
LL | let new_pie: Pie = Pie::new();
4355
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4456

4557
error: redundant type annotation
46-
--> $DIR/redundant_type_annotations.rs:133:5
58+
--> $DIR/redundant_type_annotations.rs:152:5
4759
|
4860
LL | let _return: u32 = new_pie.return_an_int();
4961
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5062

5163
error: redundant type annotation
52-
--> $DIR/redundant_type_annotations.rs:137:5
64+
--> $DIR/redundant_type_annotations.rs:156:5
5365
|
5466
LL | let _return: u32 = Pie::associated_return_an_int();
5567
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5668

5769
error: redundant type annotation
58-
--> $DIR/redundant_type_annotations.rs:139:5
70+
--> $DIR/redundant_type_annotations.rs:158:5
5971
|
6072
LL | let _return: String = Pie::associated_return_a_string();
6173
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6274

6375
error: redundant type annotation
64-
--> $DIR/redundant_type_annotations.rs:143:5
76+
--> $DIR/redundant_type_annotations.rs:162:5
6577
|
6678
LL | let _var: u32 = u32::MAX;
6779
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6880

6981
error: redundant type annotation
70-
--> $DIR/redundant_type_annotations.rs:146:5
82+
--> $DIR/redundant_type_annotations.rs:165:5
7183
|
7284
LL | let _var: u32 = 5_u32;
7385
| ^^^^^^^^^^^^^^^^^^^^^^
7486

7587
error: redundant type annotation
76-
--> $DIR/redundant_type_annotations.rs:149:5
88+
--> $DIR/redundant_type_annotations.rs:168:5
7789
|
7890
LL | let _var: &str = "test";
7991
| ^^^^^^^^^^^^^^^^^^^^^^^^
8092

8193
error: redundant type annotation
82-
--> $DIR/redundant_type_annotations.rs:152:5
94+
--> $DIR/redundant_type_annotations.rs:171:5
8395
|
8496
LL | let _var: &[u8] = b"test";
8597
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
8698

8799
error: redundant type annotation
88-
--> $DIR/redundant_type_annotations.rs:155:5
100+
--> $DIR/redundant_type_annotations.rs:174:5
89101
|
90102
LL | let _var: bool = false;
91103
| ^^^^^^^^^^^^^^^^^^^^^^^
92104

93-
error: aborting due to 15 previous errors
105+
error: aborting due to 17 previous errors
94106

0 commit comments

Comments
 (0)