Skip to content

Commit 9b60cbf

Browse files
committed
Supporting MethodCall
1 parent 5977676 commit 9b60cbf

File tree

3 files changed

+50
-18
lines changed

3 files changed

+50
-18
lines changed

clippy_lints/src/redundant_type_annotations.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@ fn is_same_type<'tcx>(cx: &LateContext<'tcx>, ty_resolved_path: hir::def::Res, f
4646
false
4747
}
4848

49+
fn func_hir_id_to_func_ty<'tcx>(cx: &LateContext<'tcx>, hir_id: hir::hir_id::HirId) -> Option<Ty<'tcx>> {
50+
if let Some((defkind, func_defid)) = cx.typeck_results().type_dependent_def(hir_id)
51+
&& defkind == hir::def::DefKind::AssocFn
52+
&& let Some(init_ty) = cx.tcx.type_of(func_defid).no_bound_vars()
53+
{
54+
Some(init_ty)
55+
} else {
56+
None
57+
}
58+
}
59+
60+
fn func_ty_to_return_type<'tcx>(cx: &LateContext<'tcx>, func_ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
61+
if func_ty.is_fn()
62+
&& let Some(return_type) = func_ty.fn_sig(cx.tcx).output().no_bound_vars()
63+
{
64+
Some(return_type)
65+
} else {
66+
None
67+
}
68+
}
69+
4970
/// Extracts the fn Ty, e.g. `fn() -> std::string::String {f}`
5071
fn extract_fn_ty<'tcx>(
5172
cx: &LateContext<'tcx>,
@@ -66,16 +87,7 @@ fn extract_fn_ty<'tcx>(
6687
// Associated functions like
6788
// let a: String = String::new();
6889
// let a: String = String::get_string();
69-
hir::QPath::TypeRelative(..) => {
70-
if let Some((defkind, func_defid)) = cx.typeck_results().type_dependent_def(call.hir_id)
71-
&& defkind == hir::def::DefKind::AssocFn
72-
&& let Some(init_ty) = cx.tcx.type_of(func_defid).no_bound_vars()
73-
{
74-
Some(init_ty)
75-
} else {
76-
None
77-
}
78-
},
90+
hir::QPath::TypeRelative(..) => func_hir_id_to_func_ty(cx, call.hir_id),
7991
hir::QPath::LangItem(..) => None,
8092
}
8193
}
@@ -89,8 +101,7 @@ fn is_redundant_in_func_call<'tcx>(
89101
let func_type = extract_fn_ty(cx, call, init_path);
90102

91103
if let Some(func_type) = func_type
92-
&& func_type.is_fn()
93-
&& let Some(init_return_type) = func_type.fn_sig(cx.tcx).output().no_bound_vars()
104+
&& let Some(init_return_type) = func_ty_to_return_type(cx, func_type)
94105
{
95106
return is_same_type(cx, ty_resolved_path, init_return_type);
96107
}
@@ -117,8 +128,17 @@ impl LateLintPass<'_> for RedundantTypeAnnotations {
117128
span_lint(cx, REDUNDANT_TYPE_ANNOTATIONS, local.span, "redundant type annotation");
118129
}
119130
},
131+
hir::ExprKind::MethodCall(_, _, _, _) => {
132+
if let Some(func_ty) = func_hir_id_to_func_ty(cx, init.hir_id)
133+
&& let Some(return_type) = func_ty_to_return_type(cx, func_ty)
134+
&& is_same_type(cx, resolved_path_ty.res, return_type)
135+
{
136+
span_lint(cx, REDUNDANT_TYPE_ANNOTATIONS, local.span, "redundant type annotation");
137+
}
138+
},
120139
// When the initialization is a path for example u32::MAX
121140
hir::ExprKind::Path(init_path) => {
141+
// TODO: check for non primty
122142
if let hir::def::Res::PrimTy(primty) = resolved_path_ty.res
123143

124144
&& let hir::QPath::TypeRelative(init_ty, _) = init_path
@@ -130,7 +150,7 @@ impl LateLintPass<'_> for RedundantTypeAnnotations {
130150
{
131151
span_lint(cx, REDUNDANT_TYPE_ANNOTATIONS, local.span, "redundant type annotation");
132152
}
133-
}
153+
},
134154
_ => ()
135155
}
136156
};

tests/ui/redundant_type_annotations.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl Pie {
6161
}
6262

6363
fn test_method_call(&self) {
64-
let v: u32 = self.return_an_int(); // This should lint but doesn't (MethodCall)
64+
let v: u32 = self.return_an_int(); // Should lint
6565
}
6666
}
6767

@@ -130,7 +130,7 @@ fn test_functions() {
130130

131131
let new_pie: Pie = Pie::new();
132132

133-
let _return: u32 = new_pie.return_an_int(); // this should lint but doesn't (MethodCall)
133+
let _return: u32 = new_pie.return_an_int();
134134

135135
let _return: String = String::from("test");
136136

tests/ui/redundant_type_annotations.stderr

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
error: redundant type annotation
2+
--> $DIR/redundant_type_annotations.rs:64:9
3+
|
4+
LL | let v: u32 = self.return_an_int(); // Should lint
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::redundant-type-annotations` implied by `-D warnings`
8+
19
error: redundant type annotation
210
--> $DIR/redundant_type_annotations.rs:121:5
311
|
412
LL | let _return: String = return_a_string();
513
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
= note: `-D clippy::redundant-type-annotations` implied by `-D warnings`
814

915
error: redundant type annotation
1016
--> $DIR/redundant_type_annotations.rs:123:5
@@ -36,6 +42,12 @@ error: redundant type annotation
3642
LL | let new_pie: Pie = Pie::new();
3743
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3844

45+
error: redundant type annotation
46+
--> $DIR/redundant_type_annotations.rs:133:5
47+
|
48+
LL | let _return: u32 = new_pie.return_an_int();
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50+
3951
error: redundant type annotation
4052
--> $DIR/redundant_type_annotations.rs:137:5
4153
|
@@ -54,5 +66,5 @@ error: redundant type annotation
5466
LL | let _var: u32 = u32::MAX;
5567
| ^^^^^^^^^^^^^^^^^^^^^^^^^
5668

57-
error: aborting due to 9 previous errors
69+
error: aborting due to 11 previous errors
5870

0 commit comments

Comments
 (0)