Skip to content

Commit efe7f8c

Browse files
committed
Use if-let chains
1 parent 5b81b7e commit efe7f8c

File tree

1 file changed

+35
-52
lines changed

1 file changed

+35
-52
lines changed

clippy_lints/src/redundant_type_annotations.rs

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,19 @@ declare_lint_pass!(RedundantTypeAnnotations => [REDUNDANT_TYPE_ANNOTATIONS]);
2828

2929
fn is_redundant_in_resolved_path<'tcx>(cx: &LateContext<'tcx>, res: hir::def::Res, func_return_type: Ty<'tcx>) -> bool {
3030
// type annotation is primitive
31-
if_chain! {
32-
if let hir::def::Res::PrimTy(primty) = res;
33-
34-
if func_return_type.is_primitive();
35-
if let Some(func_return_type_sym) = func_return_type.primitive_symbol();
36-
37-
then {
31+
if let hir::def::Res::PrimTy(primty) = res
32+
&& func_return_type.is_primitive()
33+
&& let Some(func_return_type_sym) = func_return_type.primitive_symbol()
34+
{
3835
return primty.name() == func_return_type_sym;
3936
}
40-
}
4137

4238
// type annotation is any other non generic type
43-
if_chain! {
44-
if let hir::def::Res::Def(_, defid) = res;
45-
if let Some(annotation_ty) = cx.tcx.type_of(defid).no_bound_vars();
46-
47-
then {
39+
if let hir::def::Res::Def(_, defid) = res
40+
&& let Some(annotation_ty) = cx.tcx.type_of(defid).no_bound_vars()
41+
{
4842
return annotation_ty == func_return_type
4943
}
50-
}
5144

5245
false
5346
}
@@ -64,32 +57,27 @@ fn is_redundant_in_func_call<'tcx>(
6457
match func_return_path {
6558
// let a: String = f(); where f: fn f() -> String
6659
hir::QPath::Resolved(_, resolved_path) => {
67-
if_chain! {
68-
if let hir::def::Res::Def(_, defid) = resolved_path.res;
69-
if let Some(middle_ty_init) = cx.tcx.type_of(defid).no_bound_vars();
70-
if middle_ty_init.is_fn();
71-
if let Some(init_return_type) = middle_ty_init.fn_sig(cx.tcx).output().no_bound_vars();
72-
then {
60+
if let hir::def::Res::Def(_, defid) = resolved_path.res
61+
&& let Some(middle_ty_init) = cx.tcx.type_of(defid).no_bound_vars()
62+
&& middle_ty_init.is_fn()
63+
&& let Some(init_return_type) = middle_ty_init.fn_sig(cx.tcx).output().no_bound_vars()
64+
{
7365
return is_redundant_in_resolved_path(cx, res, init_return_type);
7466
}
75-
}
7667

7768
false
7869
},
7970
// let a: String = String::new();
8071
hir::QPath::TypeRelative(func_hir_ty, _) => {
81-
if_chain! {
82-
if let hir::def::Res::Def(_, defid) = res;
83-
if let Some(annotation_ty) = cx.tcx.type_of(defid).no_bound_vars();
84-
85-
if let hir::TyKind::Path(init_ty_path) = &func_hir_ty.kind;
86-
if let hir::QPath::Resolved(_, resolved_init_ty_path) = init_ty_path;
87-
if let hir::def::Res::Def(_, init_defid) = resolved_init_ty_path.res;
88-
if let Some(init_ty) = cx.tcx.type_of(init_defid).no_bound_vars();
89-
90-
then {
72+
if let hir::def::Res::Def(_, defid) = res
73+
&& let Some(annotation_ty) = cx.tcx.type_of(defid).no_bound_vars()
74+
75+
&& let hir::TyKind::Path(init_ty_path) = &func_hir_ty.kind
76+
&& let hir::QPath::Resolved(_, resolved_init_ty_path) = init_ty_path
77+
&& let hir::def::Res::Def(_, init_defid) = resolved_init_ty_path.res
78+
&& let Some(init_ty) = cx.tcx.type_of(init_defid).no_bound_vars()
79+
{
9180
return annotation_ty == init_ty
92-
}
9381
}
9482

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

10189
impl LateLintPass<'_> for RedundantTypeAnnotations {
10290
fn check_local<'tcx>(&mut self, cx: &LateContext<'tcx>, local: &'tcx rustc_hir::Local<'_>) {
103-
if_chain! {
10491
// type annotation part
105-
if let Some(ty) = &local.ty;
106-
if let hir::TyKind::Path(ty_path) = &ty.kind;
107-
if let hir::QPath::Resolved(_, resolved_path_ty) = ty_path;
92+
if let Some(ty) = &local.ty
93+
&& let hir::TyKind::Path(ty_path) = &ty.kind
94+
&& let hir::QPath::Resolved(_, resolved_path_ty) = ty_path
10895

10996
// initialization part
110-
if let Some(init) = local.init;
111-
112-
then {
97+
&& let Some(init) = local.init
98+
{
11399
match &init.kind {
114100
// When the initialization is a call to a function
115101
hir::ExprKind::Call(init_call, _) => {
116-
if let hir::ExprKind::Path(init_path) = &init_call.kind {
117-
if is_redundant_in_func_call(cx, resolved_path_ty.res, init_path) {
102+
if let hir::ExprKind::Path(init_path) = &init_call.kind
103+
&& is_redundant_in_func_call(cx, resolved_path_ty.res, init_path)
104+
{
118105
span_lint(cx, REDUNDANT_TYPE_ANNOTATIONS, local.span, "redundant type annotation");
119106
}
120-
}
121107
},
122108
// When the initialization is a path for example u32::MAX
123109
hir::ExprKind::Path(init_path) => {
124-
if_chain! {
125-
if let hir::def::Res::PrimTy(primty) = resolved_path_ty.res;
110+
if let hir::def::Res::PrimTy(primty) = resolved_path_ty.res
126111

127-
if let hir::QPath::TypeRelative(init_ty, _) = init_path;
128-
if let hir::TyKind::Path(init_ty_path) = &init_ty.kind;
129-
if let hir::QPath::Resolved(_, resolved_init_ty_path) = init_ty_path;
130-
if let hir::def::Res::PrimTy(primty_init) = resolved_init_ty_path.res;
112+
&& let hir::QPath::TypeRelative(init_ty, _) = init_path
113+
&& let hir::TyKind::Path(init_ty_path) = &init_ty.kind
114+
&& let hir::QPath::Resolved(_, resolved_init_ty_path) = init_ty_path
115+
&& let hir::def::Res::PrimTy(primty_init) = resolved_init_ty_path.res
131116

132-
if primty == primty_init;
133-
then {
117+
&& primty == primty_init
118+
{
134119
span_lint(cx, REDUNDANT_TYPE_ANNOTATIONS, local.span, "redundant type annotation");
135120
}
136121
}
137-
}
138122
_ => ()
139-
}
140123
}
141124
};
142125
}

0 commit comments

Comments
 (0)