1
1
use clippy_utils:: diagnostics:: span_lint_and_then;
2
- use clippy_utils:: visitors:: LocalUsedVisitor ;
2
+ use clippy_utils:: visitors:: is_local_used ;
3
3
use clippy_utils:: { higher, is_lang_ctor, is_unit_expr, path_to_local, peel_ref_operators, SpanlessEq } ;
4
4
use if_chain:: if_chain;
5
5
use rustc_hir:: LangItem :: OptionNone ;
@@ -56,11 +56,11 @@ impl<'tcx> LateLintPass<'tcx> for CollapsibleMatch {
56
56
check_arm ( cx, true , arm. pat , arm. body , arm. guard . as_ref ( ) , Some ( els_arm. body ) ) ;
57
57
}
58
58
}
59
- }
59
+ } ,
60
60
Some ( IfLetOrMatch :: IfLet ( _, pat, body, els) ) => {
61
61
check_arm ( cx, false , pat, body, None , els) ;
62
- }
63
- None => { }
62
+ } ,
63
+ None => { } ,
64
64
}
65
65
}
66
66
}
@@ -71,7 +71,7 @@ fn check_arm<'tcx>(
71
71
outer_pat : & ' tcx Pat < ' tcx > ,
72
72
outer_then_body : & ' tcx Expr < ' tcx > ,
73
73
outer_guard : Option < & ' tcx Guard < ' tcx > > ,
74
- outer_else_body : Option < & ' tcx Expr < ' tcx > >
74
+ outer_else_body : Option < & ' tcx Expr < ' tcx > > ,
75
75
) {
76
76
let inner_expr = strip_singleton_blocks ( outer_then_body) ;
77
77
if_chain ! {
@@ -106,14 +106,13 @@ fn check_arm<'tcx>(
106
106
( Some ( a) , Some ( b) ) => SpanlessEq :: new( cx) . eq_expr( a, b) ,
107
107
} ;
108
108
// the binding must not be used in the if guard
109
- let mut used_visitor = LocalUsedVisitor :: new( cx, binding_id) ;
110
- if outer_guard. map_or( true , |( Guard :: If ( e) | Guard :: IfLet ( _, e) ) | !used_visitor. check_expr( e) ) ;
109
+ if outer_guard. map_or( true , |( Guard :: If ( e) | Guard :: IfLet ( _, e) ) | !is_local_used( cx, * e, binding_id) ) ;
111
110
// ...or anywhere in the inner expression
112
111
if match inner {
113
112
IfLetOrMatch :: IfLet ( _, _, body, els) => {
114
- !used_visitor . check_expr ( body) && els. map_or( true , |e| !used_visitor . check_expr ( e ) )
113
+ !is_local_used ( cx , body, binding_id ) && els. map_or( true , |e| !is_local_used ( cx , e , binding_id ) )
115
114
} ,
116
- IfLetOrMatch :: Match ( _, arms, ..) => !arms. iter( ) . any( |arm| used_visitor . check_arm ( arm) ) ,
115
+ IfLetOrMatch :: Match ( _, arms, ..) => !arms. iter( ) . any( |arm| is_local_used ( cx , arm, binding_id ) ) ,
117
116
} ;
118
117
then {
119
118
let msg = format!(
@@ -154,16 +153,26 @@ fn strip_singleton_blocks<'hir>(mut expr: &'hir Expr<'hir>) -> &'hir Expr<'hir>
154
153
enum IfLetOrMatch < ' hir > {
155
154
Match ( & ' hir Expr < ' hir > , & ' hir [ Arm < ' hir > ] , MatchSource ) ,
156
155
/// scrutinee, pattern, then block, else block
157
- IfLet ( & ' hir Expr < ' hir > , & ' hir Pat < ' hir > , & ' hir Expr < ' hir > , Option < & ' hir Expr < ' hir > > ) ,
156
+ IfLet (
157
+ & ' hir Expr < ' hir > ,
158
+ & ' hir Pat < ' hir > ,
159
+ & ' hir Expr < ' hir > ,
160
+ Option < & ' hir Expr < ' hir > > ,
161
+ ) ,
158
162
}
159
163
160
164
impl < ' hir > IfLetOrMatch < ' hir > {
161
165
fn parse ( cx : & LateContext < ' _ > , expr : & Expr < ' hir > ) -> Option < Self > {
162
166
match expr. kind {
163
167
ExprKind :: Match ( expr, arms, source) => Some ( Self :: Match ( expr, arms, source) ) ,
164
- _ => higher:: IfLet :: hir ( cx, expr) . map ( |higher:: IfLet { let_expr, let_pat, if_then, if_else } | {
165
- Self :: IfLet ( let_expr, let_pat, if_then, if_else)
166
- } )
168
+ _ => higher:: IfLet :: hir ( cx, expr) . map (
169
+ |higher:: IfLet {
170
+ let_expr,
171
+ let_pat,
172
+ if_then,
173
+ if_else,
174
+ } | { Self :: IfLet ( let_expr, let_pat, if_then, if_else) } ,
175
+ ) ,
167
176
}
168
177
}
169
178
}
0 commit comments