1 use crate::utils::{is_type_diagnostic_item, span_lint_and_help, SpanlessEq};
2 use if_chain::if_chain;
3 use rustc_hir::intravisit::{self as visit, NestedVisitorMap, Visitor};
4 use rustc_hir::{Expr, ExprKind, MatchSource};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_middle::hir::map::Map;
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 /// **What it does:** Checks for `Mutex::lock` calls in `if let` expression
11 /// with lock calls in any of the else blocks.
13 /// **Why is this bad?** The Mutex lock remains held for the whole
14 /// `if let ... else` block and deadlocks.
16 /// **Known problems:** None.
21 /// if let Ok(thing) = mutex.lock() {
29 /// let locked = mutex.lock();
30 /// if let Ok(thing) = locked {
33 /// use_locked(locked);
38 "locking a `Mutex` in an `if let` block can cause deadlocks"
41 declare_lint_pass!(IfLetMutex => [IF_LET_MUTEX]);
43 impl LateLintPass<'_, '_> for IfLetMutex {
44 fn check_expr(&mut self, cx: &LateContext<'_, '_>, ex: &'_ Expr<'_>) {
45 let mut arm_visit = ArmVisitor {
46 mutex_lock_called: false,
50 let mut op_visit = OppVisitor {
51 mutex_lock_called: false,
55 if let ExprKind::Match(
58 MatchSource::IfLetDesugar {
59 contains_else_clause: true,
63 op_visit.visit_expr(op);
64 if op_visit.mutex_lock_called {
66 arm_visit.visit_arm(arm);
69 if arm_visit.mutex_lock_called && arm_visit.same_mutex(cx, op_visit.found_mutex.unwrap()) {
74 "calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a deadlock",
76 "move the lock call outside of the `if let ...` expression",
84 /// Checks if `Mutex::lock` is called in the `if let _ = expr.
85 pub struct OppVisitor<'tcx, 'l> {
86 mutex_lock_called: bool,
87 found_mutex: Option<&'tcx Expr<'tcx>>,
88 cx: &'tcx LateContext<'tcx, 'l>,
91 impl<'tcx, 'l> Visitor<'tcx> for OppVisitor<'tcx, 'l> {
94 fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
96 if let Some(mutex) = is_mutex_lock_call(self.cx, expr);
98 self.found_mutex = Some(mutex);
99 self.mutex_lock_called = true;
103 visit::walk_expr(self, expr);
106 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
107 NestedVisitorMap::None
111 /// Checks if `Mutex::lock` is called in any of the branches.
112 pub struct ArmVisitor<'tcx, 'l> {
113 mutex_lock_called: bool,
114 found_mutex: Option<&'tcx Expr<'tcx>>,
115 cx: &'tcx LateContext<'tcx, 'l>,
118 impl<'tcx, 'l> Visitor<'tcx> for ArmVisitor<'tcx, 'l> {
119 type Map = Map<'tcx>;
121 fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
123 if let Some(mutex) = is_mutex_lock_call(self.cx, expr);
125 self.found_mutex = Some(mutex);
126 self.mutex_lock_called = true;
130 visit::walk_expr(self, expr);
133 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
134 NestedVisitorMap::None
138 impl<'tcx, 'l> ArmVisitor<'tcx, 'l> {
139 fn same_mutex(&self, cx: &LateContext<'_, '_>, op_mutex: &Expr<'_>) -> bool {
140 if let Some(arm_mutex) = self.found_mutex {
141 SpanlessEq::new(cx).eq_expr(op_mutex, arm_mutex)
148 fn is_mutex_lock_call<'a>(cx: &LateContext<'a, '_>, expr: &'a Expr<'_>) -> Option<&'a Expr<'a>> {
150 if let ExprKind::MethodCall(path, _span, args, _) = &expr.kind;
151 if path.ident.to_string() == "lock";
152 let ty = cx.tables.expr_ty(&args[0]);
153 if is_type_diagnostic_item(cx, ty, sym!(mutex_type));