1
1
use clippy_utils:: diagnostics:: span_lint_and_then;
2
2
use clippy_utils:: ty:: is_type_diagnostic_item;
3
3
use clippy_utils:: visitors:: for_each_expr_without_closures;
4
- use clippy_utils:: { higher , SpanlessEq } ;
4
+ use clippy_utils:: { eq_expr_value , higher } ;
5
5
use core:: ops:: ControlFlow ;
6
6
use rustc_errors:: Diag ;
7
7
use rustc_hir:: { Expr , ExprKind } ;
@@ -52,20 +52,11 @@ impl<'tcx> LateLintPass<'tcx> for IfLetMutex {
52
52
..
53
53
} ) = higher:: IfLet :: hir ( cx, expr)
54
54
{
55
- let is_mutex_lock = |e : & ' tcx Expr < ' tcx > | {
56
- if let Some ( mutex) = is_mutex_lock_call ( cx, e) {
57
- ControlFlow :: Break ( mutex)
58
- } else {
59
- ControlFlow :: Continue ( ( ) )
60
- }
61
- } ;
62
-
63
- let op_mutex = for_each_expr_without_closures ( let_expr, is_mutex_lock) ;
55
+ let op_mutex = for_each_expr_without_closures ( let_expr, |e| mutex_lock_call ( cx, e, None ) ) ;
64
56
if let Some ( op_mutex) = op_mutex {
65
- let arm_mutex = for_each_expr_without_closures ( ( if_then, if_else) , is_mutex_lock) ;
66
- if let Some ( arm_mutex) = arm_mutex
67
- && SpanlessEq :: new ( cx) . eq_expr ( op_mutex, arm_mutex)
68
- {
57
+ let arm_mutex =
58
+ for_each_expr_without_closures ( ( if_then, if_else) , |e| mutex_lock_call ( cx, e, Some ( op_mutex) ) ) ;
59
+ if let Some ( arm_mutex) = arm_mutex {
69
60
let diag = |diag : & mut Diag < ' _ , ( ) > | {
70
61
diag. span_label (
71
62
op_mutex. span ,
@@ -90,14 +81,19 @@ impl<'tcx> LateLintPass<'tcx> for IfLetMutex {
90
81
}
91
82
}
92
83
93
- fn is_mutex_lock_call < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) -> Option < & ' tcx Expr < ' tcx > > {
84
+ fn mutex_lock_call < ' tcx > (
85
+ cx : & LateContext < ' tcx > ,
86
+ expr : & ' tcx Expr < ' _ > ,
87
+ op_mutex : Option < & ' tcx Expr < ' _ > > ,
88
+ ) -> ControlFlow < & ' tcx Expr < ' tcx > > {
94
89
if let ExprKind :: MethodCall ( path, self_arg, ..) = & expr. kind
95
90
&& path. ident . as_str ( ) == "lock"
96
91
&& let ty = cx. typeck_results ( ) . expr_ty ( self_arg) . peel_refs ( )
97
92
&& is_type_diagnostic_item ( cx, ty, sym:: Mutex )
93
+ && op_mutex. map_or ( true , |op| eq_expr_value ( cx, self_arg, op) )
98
94
{
99
- Some ( self_arg)
95
+ ControlFlow :: Break ( self_arg)
100
96
} else {
101
- None
97
+ ControlFlow :: Continue ( ( ) )
102
98
}
103
99
}
0 commit comments