1+ use if_chain:: if_chain;
12use crate :: utils:: { match_type, paths, span_lint_and_help} ;
23use rustc_hir:: intravisit:: { self as visit, NestedVisitorMap , Visitor } ;
34use rustc_hir:: { Arm , Expr , ExprKind , MatchSource , StmtKind } ;
@@ -42,13 +43,11 @@ declare_lint_pass!(IfLetMutex => [IF_LET_MUTEX]);
4243impl LateLintPass < ' _ , ' _ > for IfLetMutex {
4344 fn check_expr ( & mut self , cx : & LateContext < ' _ , ' _ > , ex : & ' _ Expr < ' _ > ) {
4445 let mut arm_visit = ArmVisitor {
45- arm_mutex : false ,
46- arm_lock : false ,
46+ mutex_lock_called : false ,
4747 cx,
4848 } ;
4949 let mut op_visit = OppVisitor {
50- op_mutex : false ,
51- op_lock : false ,
50+ mutex_lock_called : false ,
5251 cx,
5352 } ;
5453 if let ExprKind :: Match (
@@ -60,12 +59,12 @@ impl LateLintPass<'_, '_> for IfLetMutex {
6059 ) = ex. kind
6160 {
6261 op_visit. visit_expr ( op) ;
63- if op_visit. op_mutex && op_visit . op_lock {
62+ if op_visit. mutex_lock_called {
6463 for arm in * arms {
6564 arm_visit. visit_arm ( arm) ;
6665 }
6766
68- if arm_visit. arm_mutex && arm_visit . arm_lock {
67+ if arm_visit. mutex_lock_called {
6968 span_lint_and_help (
7069 cx,
7170 IF_LET_MUTEX ,
@@ -81,22 +80,22 @@ impl LateLintPass<'_, '_> for IfLetMutex {
8180
8281/// Checks if `Mutex::lock` is called in the `if let _ = expr.
8382pub struct OppVisitor < ' tcx , ' l > {
84- pub op_mutex : bool ,
85- pub op_lock : bool ,
83+ pub mutex_lock_called : bool ,
8684 pub cx : & ' tcx LateContext < ' tcx , ' l > ,
8785}
8886
8987impl < ' tcx , ' l > Visitor < ' tcx > for OppVisitor < ' tcx , ' l > {
9088 type Map = Map < ' tcx > ;
9189
9290 fn visit_expr ( & mut self , expr : & ' tcx Expr < ' _ > ) {
93- if let ExprKind :: MethodCall ( path, _span, args) = & expr. kind {
94- if path. ident . to_string ( ) == "lock" {
95- self . op_lock = true ;
96- }
91+ if_chain ! {
92+ if let ExprKind :: MethodCall ( path, _span, args) = & expr. kind;
93+ if path. ident. to_string( ) == "lock" ;
9794 let ty = self . cx. tables. expr_ty( & args[ 0 ] ) ;
98- if match_type ( self . cx , ty, & paths:: MUTEX ) {
99- self . op_mutex = true ;
95+ if match_type( self . cx, ty, & paths:: MUTEX ) ;
96+ then {
97+ self . mutex_lock_called = true ;
98+ return ;
10099 }
101100 }
102101 visit:: walk_expr ( self , expr) ;
@@ -109,22 +108,22 @@ impl<'tcx, 'l> Visitor<'tcx> for OppVisitor<'tcx, 'l> {
109108
110109/// Checks if `Mutex::lock` is called in any of the branches.
111110pub struct ArmVisitor < ' tcx , ' l > {
112- pub arm_mutex : bool ,
113- pub arm_lock : bool ,
111+ pub mutex_lock_called : bool ,
114112 pub cx : & ' tcx LateContext < ' tcx , ' l > ,
115113}
116114
117115impl < ' tcx , ' l > Visitor < ' tcx > for ArmVisitor < ' tcx , ' l > {
118116 type Map = Map < ' tcx > ;
119117
120118 fn visit_expr ( & mut self , expr : & ' tcx Expr < ' _ > ) {
121- if let ExprKind :: MethodCall ( path, _span, args) = & expr. kind {
122- if path. ident . to_string ( ) == "lock" {
123- self . arm_lock = true ;
124- }
119+ if_chain ! {
120+ if let ExprKind :: MethodCall ( path, _span, args) = & expr. kind;
121+ if path. ident. to_string( ) == "lock" ;
125122 let ty = self . cx. tables. expr_ty( & args[ 0 ] ) ;
126- if match_type ( self . cx , ty, & paths:: MUTEX ) {
127- self . arm_mutex = true ;
123+ if match_type( self . cx, ty, & paths:: MUTEX ) ;
124+ then {
125+ self . mutex_lock_called = true ;
126+ return ;
128127 }
129128 }
130129 visit:: walk_expr ( self , expr) ;
0 commit comments