@@ -45,21 +45,23 @@ declare_lint_pass!(IfLetMutex => [IF_LET_MUTEX]);
4545
4646impl < ' tcx > LateLintPass < ' tcx > for IfLetMutex {
4747 fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' tcx > ) {
48- let mut arm_visit = ArmVisitor { found_mutex : None , cx } ;
49- let mut op_visit = OppVisitor { found_mutex : None , cx } ;
48+ let mut arm_visit = ArmVisitor { cx } ;
49+ let mut op_visit = OppVisitor { cx } ;
5050 if let Some ( higher:: IfLet {
5151 let_expr,
5252 if_then,
5353 if_else : Some ( if_else) ,
5454 ..
5555 } ) = higher:: IfLet :: hir ( cx, expr)
5656 {
57- op_visit. visit_expr ( let_expr) ;
58- if let Some ( op_mutex) = op_visit. found_mutex {
59- arm_visit. visit_expr ( if_then) ;
60- arm_visit. visit_expr ( if_else) ;
57+ let found_op_mutex = op_visit. visit_expr ( let_expr) . break_value ( ) ;
58+ if let Some ( op_mutex) = found_op_mutex {
59+ let mut found_mutex = arm_visit. visit_expr ( if_then) . break_value ( ) ;
60+ if found_mutex. is_none ( ) {
61+ found_mutex = arm_visit. visit_expr ( if_else) . break_value ( ) ;
62+ } ;
6163
62- if let Some ( arm_mutex) = arm_visit. found_mutex_if_same_as ( op_mutex) {
64+ if let Some ( arm_mutex) = arm_visit. found_mutex_if_same_as ( op_mutex, found_mutex ) {
6365 let diag = |diag : & mut Diag < ' _ , ( ) > | {
6466 diag. span_label (
6567 op_mutex. span ,
@@ -86,43 +88,37 @@ impl<'tcx> LateLintPass<'tcx> for IfLetMutex {
8688
8789/// Checks if `Mutex::lock` is called in the `if let` expr.
8890pub struct OppVisitor < ' a , ' tcx > {
89- found_mutex : Option < & ' tcx Expr < ' tcx > > ,
9091 cx : & ' a LateContext < ' tcx > ,
9192}
9293
9394impl < ' tcx > Visitor < ' tcx > for OppVisitor < ' _ , ' tcx > {
94- type Result = ControlFlow < ( ) > ;
95- fn visit_expr ( & mut self , expr : & ' tcx Expr < ' _ > ) -> ControlFlow < ( ) > {
95+ type Result = ControlFlow < & ' tcx Expr < ' tcx > > ;
96+ fn visit_expr ( & mut self , expr : & ' tcx Expr < ' _ > ) -> ControlFlow < & ' tcx Expr < ' tcx > > {
9697 if let Some ( mutex) = is_mutex_lock_call ( self . cx , expr) {
97- self . found_mutex = Some ( mutex) ;
98- return ControlFlow :: Break ( ( ) ) ;
98+ return ControlFlow :: Break ( mutex) ;
9999 }
100- visit:: walk_expr ( self , expr) ;
101- ControlFlow :: Continue ( ( ) )
100+ visit:: walk_expr ( self , expr)
102101 }
103102}
104103
105104/// Checks if `Mutex::lock` is called in any of the branches.
106105pub struct ArmVisitor < ' a , ' tcx > {
107- found_mutex : Option < & ' tcx Expr < ' tcx > > ,
108106 cx : & ' a LateContext < ' tcx > ,
109107}
110108
111109impl < ' tcx > Visitor < ' tcx > for ArmVisitor < ' _ , ' tcx > {
112- type Result = ControlFlow < ( ) > ;
113- fn visit_expr ( & mut self , expr : & ' tcx Expr < ' tcx > ) -> ControlFlow < ( ) > {
110+ type Result = ControlFlow < & ' tcx Expr < ' tcx > > ;
111+ fn visit_expr ( & mut self , expr : & ' tcx Expr < ' tcx > ) -> ControlFlow < & ' tcx Expr < ' tcx > > {
114112 if let Some ( mutex) = is_mutex_lock_call ( self . cx , expr) {
115- self . found_mutex = Some ( mutex) ;
116- return ControlFlow :: Break ( ( ) ) ;
113+ return ControlFlow :: Break ( mutex) ;
117114 }
118- visit:: walk_expr ( self , expr) ;
119- ControlFlow :: Continue ( ( ) )
115+ visit:: walk_expr ( self , expr)
120116 }
121117}
122118
123119impl < ' tcx , ' l > ArmVisitor < ' tcx , ' l > {
124- fn found_mutex_if_same_as ( & self , op_mutex : & Expr < ' _ > ) -> Option < & Expr < ' _ > > {
125- self . found_mutex . and_then ( |arm_mutex| {
120+ fn found_mutex_if_same_as ( & self , op_mutex : & Expr < ' _ > , found_mutex : Option < & ' tcx Expr < ' tcx > > ) -> Option < & Expr < ' _ > > {
121+ found_mutex. and_then ( |arm_mutex| {
126122 SpanlessEq :: new ( self . cx )
127123 . eq_expr ( op_mutex, arm_mutex)
128124 . then_some ( arm_mutex)
0 commit comments