1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::higher;
3 use clippy_utils::ty::is_type_diagnostic_item;
4 use clippy_utils::SpanlessEq;
5 use if_chain::if_chain;
6 use rustc_hir::intravisit::{self as visit, Visitor};
7 use rustc_hir::{Expr, ExprKind};
8 use rustc_lint::{LateContext, LateLintPass};
9 use rustc_session::{declare_lint_pass, declare_tool_lint};
12 declare_clippy_lint! {
14 /// Checks for `Mutex::lock` calls in `if let` expression
15 /// with lock calls in any of the else blocks.
17 /// ### Why is this bad?
18 /// The Mutex lock remains held for the whole
19 /// `if let ... else` block and deadlocks.
23 /// if let Ok(thing) = mutex.lock() {
31 /// let locked = mutex.lock();
32 /// if let Ok(thing) = locked {
35 /// use_locked(locked);
38 #[clippy::version = "1.45.0"]
41 "locking a `Mutex` in an `if let` block can cause deadlocks"
44 declare_lint_pass!(IfLetMutex => [IF_LET_MUTEX]);
46 impl<'tcx> LateLintPass<'tcx> for IfLetMutex {
47 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
48 let mut arm_visit = ArmVisitor {
49 mutex_lock_called: false,
53 let mut op_visit = OppVisitor {
54 mutex_lock_called: false,
58 if let Some(higher::IfLet {
61 if_else: Some(if_else),
63 }) = higher::IfLet::hir(cx, expr)
65 op_visit.visit_expr(let_expr);
66 if op_visit.mutex_lock_called {
67 arm_visit.visit_expr(if_then);
68 arm_visit.visit_expr(if_else);
70 if arm_visit.mutex_lock_called && arm_visit.same_mutex(cx, op_visit.found_mutex.unwrap()) {
75 "calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a deadlock",
77 "move the lock call outside of the `if let ...` expression",
85 /// Checks if `Mutex::lock` is called in the `if let` expr.
86 pub struct OppVisitor<'a, 'tcx> {
87 mutex_lock_called: bool,
88 found_mutex: Option<&'tcx Expr<'tcx>>,
89 cx: &'a LateContext<'tcx>,
92 impl<'tcx> Visitor<'tcx> for OppVisitor<'_, 'tcx> {
93 fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
94 if let Some(mutex) = is_mutex_lock_call(self.cx, expr) {
95 self.found_mutex = Some(mutex);
96 self.mutex_lock_called = true;
99 visit::walk_expr(self, expr);
103 /// Checks if `Mutex::lock` is called in any of the branches.
104 pub struct ArmVisitor<'a, 'tcx> {
105 mutex_lock_called: bool,
106 found_mutex: Option<&'tcx Expr<'tcx>>,
107 cx: &'a LateContext<'tcx>,
110 impl<'tcx> Visitor<'tcx> for ArmVisitor<'_, 'tcx> {
111 fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
112 if let Some(mutex) = is_mutex_lock_call(self.cx, expr) {
113 self.found_mutex = Some(mutex);
114 self.mutex_lock_called = true;
117 visit::walk_expr(self, expr);
121 impl<'tcx, 'l> ArmVisitor<'tcx, 'l> {
122 fn same_mutex(&self, cx: &LateContext<'_>, op_mutex: &Expr<'_>) -> bool {
124 .map_or(false, |arm_mutex| SpanlessEq::new(cx).eq_expr(op_mutex, arm_mutex))
128 fn is_mutex_lock_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
130 if let ExprKind::MethodCall(path, [self_arg, ..], _) = &expr.kind;
131 if path.ident.as_str() == "lock";
132 let ty = cx.typeck_results().expr_ty(self_arg);
133 if is_type_diagnostic_item(cx, ty, sym::Mutex);