@@ -28,26 +28,19 @@ declare_lint_pass!(RedundantTypeAnnotations => [REDUNDANT_TYPE_ANNOTATIONS]);
28
28
29
29
fn is_redundant_in_resolved_path < ' tcx > ( cx : & LateContext < ' tcx > , res : hir:: def:: Res , func_return_type : Ty < ' tcx > ) -> bool {
30
30
// 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
+ {
38
35
return primty. name ( ) == func_return_type_sym;
39
36
}
40
- }
41
37
42
38
// 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
+ {
48
42
return annotation_ty == func_return_type
49
43
}
50
- }
51
44
52
45
false
53
46
}
@@ -64,32 +57,27 @@ fn is_redundant_in_func_call<'tcx>(
64
57
match func_return_path {
65
58
// let a: String = f(); where f: fn f() -> String
66
59
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
+ {
73
65
return is_redundant_in_resolved_path ( cx, res, init_return_type) ;
74
66
}
75
- }
76
67
77
68
false
78
69
} ,
79
70
// let a: String = String::new();
80
71
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
+ {
91
80
return annotation_ty == init_ty
92
- }
93
81
}
94
82
95
83
false
@@ -100,43 +88,38 @@ fn is_redundant_in_func_call<'tcx>(
100
88
101
89
impl LateLintPass < ' _ > for RedundantTypeAnnotations {
102
90
fn check_local < ' tcx > ( & mut self , cx : & LateContext < ' tcx > , local : & ' tcx rustc_hir:: Local < ' _ > ) {
103
- if_chain ! {
104
91
// 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
108
95
109
96
// initialization part
110
- if let Some ( init) = local. init;
111
-
112
- then {
97
+ && let Some ( init) = local. init
98
+ {
113
99
match & init. kind {
114
100
// When the initialization is a call to a function
115
101
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
+ {
118
105
span_lint ( cx, REDUNDANT_TYPE_ANNOTATIONS , local. span , "redundant type annotation" ) ;
119
106
}
120
- }
121
107
} ,
122
108
// When the initialization is a path for example u32::MAX
123
109
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
126
111
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
131
116
132
- if primty == primty_init;
133
- then {
117
+ && primty == primty_init
118
+ {
134
119
span_lint ( cx, REDUNDANT_TYPE_ANNOTATIONS , local. span , "redundant type annotation" ) ;
135
120
}
136
121
}
137
- }
138
122
_ => ( )
139
- }
140
123
}
141
124
} ;
142
125
}
0 commit comments