Skip to content

Commit

Permalink
Supporting MethodCall
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessioC31 committed Apr 17, 2023
1 parent bc66d9a commit b0405c7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
46 changes: 33 additions & 13 deletions clippy_lints/src/redundant_type_annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ fn is_same_type<'tcx>(cx: &LateContext<'tcx>, ty_resolved_path: hir::def::Res, f
false
}

fn func_hir_id_to_func_ty<'tcx>(cx: &LateContext<'tcx>, hir_id: hir::hir_id::HirId) -> Option<Ty<'tcx>> {
if let Some((defkind, func_defid)) = cx.typeck_results().type_dependent_def(hir_id)
&& defkind == hir::def::DefKind::AssocFn
&& let Some(init_ty) = cx.tcx.type_of(func_defid).no_bound_vars()
{
Some(init_ty)
} else {
None
}
}

fn func_ty_to_return_type<'tcx>(cx: &LateContext<'tcx>, func_ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
if func_ty.is_fn()
&& let Some(return_type) = func_ty.fn_sig(cx.tcx).output().no_bound_vars()
{
Some(return_type)
} else {
None
}
}

/// Extracts the fn Ty, e.g. `fn() -> std::string::String {f}`
fn extract_fn_ty<'tcx>(
cx: &LateContext<'tcx>,
Expand All @@ -66,16 +87,7 @@ fn extract_fn_ty<'tcx>(
// Associated functions like
// let a: String = String::new();
// let a: String = String::get_string();
hir::QPath::TypeRelative(..) => {
if let Some((defkind, func_defid)) = cx.typeck_results().type_dependent_def(call.hir_id)
&& defkind == hir::def::DefKind::AssocFn
&& let Some(init_ty) = cx.tcx.type_of(func_defid).no_bound_vars()
{
Some(init_ty)
} else {
None
}
},
hir::QPath::TypeRelative(..) => func_hir_id_to_func_ty(cx, call.hir_id),
hir::QPath::LangItem(..) => None,
}
}
Expand All @@ -89,8 +101,7 @@ fn is_redundant_in_func_call<'tcx>(
let func_type = extract_fn_ty(cx, call, init_path);

if let Some(func_type) = func_type
&& func_type.is_fn()
&& let Some(init_return_type) = func_type.fn_sig(cx.tcx).output().no_bound_vars()
&& let Some(init_return_type) = func_ty_to_return_type(cx, func_type)
{
return is_same_type(cx, ty_resolved_path, init_return_type);
}
Expand All @@ -117,8 +128,17 @@ impl LateLintPass<'_> for RedundantTypeAnnotations {
span_lint(cx, REDUNDANT_TYPE_ANNOTATIONS, local.span, "redundant type annotation");
}
},
hir::ExprKind::MethodCall(_, _, _, _) => {
if let Some(func_ty) = func_hir_id_to_func_ty(cx, init.hir_id)
&& let Some(return_type) = func_ty_to_return_type(cx, func_ty)
&& is_same_type(cx, resolved_path_ty.res, return_type)
{
span_lint(cx, REDUNDANT_TYPE_ANNOTATIONS, local.span, "redundant type annotation");
}
},
// When the initialization is a path for example u32::MAX
hir::ExprKind::Path(init_path) => {
// TODO: check for non primty
if let hir::def::Res::PrimTy(primty) = resolved_path_ty.res

&& let hir::QPath::TypeRelative(init_ty, _) = init_path
Expand All @@ -130,7 +150,7 @@ impl LateLintPass<'_> for RedundantTypeAnnotations {
{
span_lint(cx, REDUNDANT_TYPE_ANNOTATIONS, local.span, "redundant type annotation");
}
}
},
_ => ()
}
};
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/redundant_type_annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Pie {
}

fn test_method_call(&self) {
let v: u32 = self.return_an_int(); // This should lint but doesn't (MethodCall)
let v: u32 = self.return_an_int(); // Should lint
}
}

Expand Down Expand Up @@ -130,7 +130,7 @@ fn test_functions() {

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

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

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

Expand Down
18 changes: 15 additions & 3 deletions tests/ui/redundant_type_annotations.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:64:9
|
LL | let v: u32 = self.return_an_int(); // Should lint
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::redundant-type-annotations` implied by `-D warnings`

error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:121:5
|
LL | let _return: String = return_a_string();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::redundant-type-annotations` implied by `-D warnings`

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

error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:133:5
|
LL | let _return: u32 = new_pie.return_an_int();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:137:5
|
Expand All @@ -54,5 +66,5 @@ error: redundant type annotation
LL | let _var: u32 = u32::MAX;
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 9 previous errors
error: aborting due to 11 previous errors

0 comments on commit b0405c7

Please sign in to comment.