Skip to content

Commit

Permalink
Use if-let chains
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessioC31 committed Apr 4, 2023
1 parent 86d9b26 commit 616a7d0
Showing 1 changed file with 35 additions and 52 deletions.
87 changes: 35 additions & 52 deletions clippy_lints/src/redundant_type_annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,19 @@ declare_lint_pass!(RedundantTypeAnnotations => [REDUNDANT_TYPE_ANNOTATIONS]);

fn is_redundant_in_resolved_path<'tcx>(cx: &LateContext<'tcx>, res: hir::def::Res, func_return_type: Ty<'tcx>) -> bool {
// type annotation is primitive
if_chain! {
if let hir::def::Res::PrimTy(primty) = res;

if func_return_type.is_primitive();
if let Some(func_return_type_sym) = func_return_type.primitive_symbol();

then {
if let hir::def::Res::PrimTy(primty) = res
&& func_return_type.is_primitive()
&& let Some(func_return_type_sym) = func_return_type.primitive_symbol()
{
return primty.name() == func_return_type_sym;
}
}

// type annotation is any other non generic type
if_chain! {
if let hir::def::Res::Def(_, defid) = res;
if let Some(annotation_ty) = cx.tcx.type_of(defid).no_bound_vars();

then {
if let hir::def::Res::Def(_, defid) = res
&& let Some(annotation_ty) = cx.tcx.type_of(defid).no_bound_vars()
{
return annotation_ty == func_return_type
}
}

false
}
Expand All @@ -64,32 +57,27 @@ fn is_redundant_in_func_call<'tcx>(
match func_return_path {
// let a: String = f(); where f: fn f() -> String
hir::QPath::Resolved(_, resolved_path) => {
if_chain! {
if let hir::def::Res::Def(_, defid) = resolved_path.res;
if let Some(middle_ty_init) = cx.tcx.type_of(defid).no_bound_vars();
if middle_ty_init.is_fn();
if let Some(init_return_type) = middle_ty_init.fn_sig(cx.tcx).output().no_bound_vars();
then {
if let hir::def::Res::Def(_, defid) = resolved_path.res
&& let Some(middle_ty_init) = cx.tcx.type_of(defid).no_bound_vars()
&& middle_ty_init.is_fn()
&& let Some(init_return_type) = middle_ty_init.fn_sig(cx.tcx).output().no_bound_vars()
{
return is_redundant_in_resolved_path(cx, res, init_return_type);
}
}

false
},
// let a: String = String::new();
hir::QPath::TypeRelative(func_hir_ty, _) => {
if_chain! {
if let hir::def::Res::Def(_, defid) = res;
if let Some(annotation_ty) = cx.tcx.type_of(defid).no_bound_vars();

if let hir::TyKind::Path(init_ty_path) = &func_hir_ty.kind;
if let hir::QPath::Resolved(_, resolved_init_ty_path) = init_ty_path;
if let hir::def::Res::Def(_, init_defid) = resolved_init_ty_path.res;
if let Some(init_ty) = cx.tcx.type_of(init_defid).no_bound_vars();

then {
if let hir::def::Res::Def(_, defid) = res
&& let Some(annotation_ty) = cx.tcx.type_of(defid).no_bound_vars()

&& let hir::TyKind::Path(init_ty_path) = &func_hir_ty.kind
&& let hir::QPath::Resolved(_, resolved_init_ty_path) = init_ty_path
&& let hir::def::Res::Def(_, init_defid) = resolved_init_ty_path.res
&& let Some(init_ty) = cx.tcx.type_of(init_defid).no_bound_vars()
{
return annotation_ty == init_ty
}
}

false
Expand All @@ -100,43 +88,38 @@ fn is_redundant_in_func_call<'tcx>(

impl LateLintPass<'_> for RedundantTypeAnnotations {
fn check_local<'tcx>(&mut self, cx: &LateContext<'tcx>, local: &'tcx rustc_hir::Local<'_>) {
if_chain! {
// type annotation part
if let Some(ty) = &local.ty;
if let hir::TyKind::Path(ty_path) = &ty.kind;
if let hir::QPath::Resolved(_, resolved_path_ty) = ty_path;
if let Some(ty) = &local.ty
&& let hir::TyKind::Path(ty_path) = &ty.kind
&& let hir::QPath::Resolved(_, resolved_path_ty) = ty_path

// initialization part
if let Some(init) = local.init;

then {
&& let Some(init) = local.init
{
match &init.kind {
// When the initialization is a call to a function
hir::ExprKind::Call(init_call, _) => {
if let hir::ExprKind::Path(init_path) = &init_call.kind {
if is_redundant_in_func_call(cx, resolved_path_ty.res, init_path) {
if let hir::ExprKind::Path(init_path) = &init_call.kind
&& is_redundant_in_func_call(cx, resolved_path_ty.res, init_path)
{
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) => {
if_chain! {
if let hir::def::Res::PrimTy(primty) = resolved_path_ty.res;
if let hir::def::Res::PrimTy(primty) = resolved_path_ty.res

if let hir::QPath::TypeRelative(init_ty, _) = init_path;
if let hir::TyKind::Path(init_ty_path) = &init_ty.kind;
if let hir::QPath::Resolved(_, resolved_init_ty_path) = init_ty_path;
if let hir::def::Res::PrimTy(primty_init) = resolved_init_ty_path.res;
&& let hir::QPath::TypeRelative(init_ty, _) = init_path
&& let hir::TyKind::Path(init_ty_path) = &init_ty.kind
&& let hir::QPath::Resolved(_, resolved_init_ty_path) = init_ty_path
&& let hir::def::Res::PrimTy(primty_init) = resolved_init_ty_path.res

if primty == primty_init;
then {
&& primty == primty_init
{
span_lint(cx, REDUNDANT_TYPE_ANNOTATIONS, local.span, "redundant type annotation");
}
}
}
_ => ()
}
}
};
}
Expand Down

0 comments on commit 616a7d0

Please sign in to comment.