11use clippy_utils:: diagnostics:: span_lint_and_then;
22use clippy_utils:: ty:: is_type_diagnostic_item;
33use clippy_utils:: visitors:: for_each_expr_without_closures;
4- use clippy_utils:: { higher , SpanlessEq } ;
4+ use clippy_utils:: { eq_expr_value , higher } ;
55use core:: ops:: ControlFlow ;
66use rustc_errors:: Diag ;
77use rustc_hir:: { Expr , ExprKind } ;
@@ -52,20 +52,11 @@ impl<'tcx> LateLintPass<'tcx> for IfLetMutex {
5252 ..
5353 } ) = higher:: IfLet :: hir ( cx, expr)
5454 {
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 ) ) ;
6456 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 {
6960 let diag = |diag : & mut Diag < ' _ , ( ) > | {
7061 diag. span_label (
7162 op_mutex. span ,
@@ -90,14 +81,19 @@ impl<'tcx> LateLintPass<'tcx> for IfLetMutex {
9081 }
9182}
9283
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 > > {
9489 if let ExprKind :: MethodCall ( path, self_arg, ..) = & expr. kind
9590 && path. ident . as_str ( ) == "lock"
9691 && let ty = cx. typeck_results ( ) . expr_ty ( self_arg) . peel_refs ( )
9792 && is_type_diagnostic_item ( cx, ty, sym:: Mutex )
93+ && op_mutex. map_or ( true , |op| eq_expr_value ( cx, self_arg, op) )
9894 {
99- Some ( self_arg)
95+ ControlFlow :: Break ( self_arg)
10096 } else {
101- None
97+ ControlFlow :: Continue ( ( ) )
10298 }
10399}
0 commit comments