1
+ use if_chain:: if_chain;
1
2
use crate :: utils:: { match_type, paths, span_lint_and_help} ;
2
3
use rustc_hir:: intravisit:: { self as visit, NestedVisitorMap , Visitor } ;
3
4
use rustc_hir:: { Arm , Expr , ExprKind , MatchSource , StmtKind } ;
@@ -42,13 +43,11 @@ declare_lint_pass!(IfLetMutex => [IF_LET_MUTEX]);
42
43
impl LateLintPass < ' _ , ' _ > for IfLetMutex {
43
44
fn check_expr ( & mut self , cx : & LateContext < ' _ , ' _ > , ex : & ' _ Expr < ' _ > ) {
44
45
let mut arm_visit = ArmVisitor {
45
- arm_mutex : false ,
46
- arm_lock : false ,
46
+ mutex_lock_called : false ,
47
47
cx,
48
48
} ;
49
49
let mut op_visit = OppVisitor {
50
- op_mutex : false ,
51
- op_lock : false ,
50
+ mutex_lock_called : false ,
52
51
cx,
53
52
} ;
54
53
if let ExprKind :: Match (
@@ -60,12 +59,12 @@ impl LateLintPass<'_, '_> for IfLetMutex {
60
59
) = ex. kind
61
60
{
62
61
op_visit. visit_expr ( op) ;
63
- if op_visit. op_mutex && op_visit . op_lock {
62
+ if op_visit. mutex_lock_called {
64
63
for arm in * arms {
65
64
arm_visit. visit_arm ( arm) ;
66
65
}
67
66
68
- if arm_visit. arm_mutex && arm_visit . arm_lock {
67
+ if arm_visit. mutex_lock_called {
69
68
span_lint_and_help (
70
69
cx,
71
70
IF_LET_MUTEX ,
@@ -81,22 +80,22 @@ impl LateLintPass<'_, '_> for IfLetMutex {
81
80
82
81
/// Checks if `Mutex::lock` is called in the `if let _ = expr.
83
82
pub struct OppVisitor < ' tcx , ' l > {
84
- pub op_mutex : bool ,
85
- pub op_lock : bool ,
83
+ pub mutex_lock_called : bool ,
86
84
pub cx : & ' tcx LateContext < ' tcx , ' l > ,
87
85
}
88
86
89
87
impl < ' tcx , ' l > Visitor < ' tcx > for OppVisitor < ' tcx , ' l > {
90
88
type Map = Map < ' tcx > ;
91
89
92
90
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" ;
97
94
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 ;
100
99
}
101
100
}
102
101
visit:: walk_expr ( self , expr) ;
@@ -109,22 +108,22 @@ impl<'tcx, 'l> Visitor<'tcx> for OppVisitor<'tcx, 'l> {
109
108
110
109
/// Checks if `Mutex::lock` is called in any of the branches.
111
110
pub struct ArmVisitor < ' tcx , ' l > {
112
- pub arm_mutex : bool ,
113
- pub arm_lock : bool ,
111
+ pub mutex_lock_called : bool ,
114
112
pub cx : & ' tcx LateContext < ' tcx , ' l > ,
115
113
}
116
114
117
115
impl < ' tcx , ' l > Visitor < ' tcx > for ArmVisitor < ' tcx , ' l > {
118
116
type Map = Map < ' tcx > ;
119
117
120
118
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" ;
125
122
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 ;
128
127
}
129
128
}
130
129
visit:: walk_expr ( self , expr) ;
0 commit comments