1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::ty::is_type_diagnostic_item;
3 use clippy_utils::SpanlessEq;
4 use if_chain::if_chain;
5 use rustc_hir::intravisit::{self as visit, NestedVisitorMap, Visitor};
6 use rustc_hir::{Expr, ExprKind, MatchSource};
7 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_middle::hir::map::Map;
9 use rustc_session::{declare_lint_pass, declare_tool_lint};
11 declare_clippy_lint! {
13 /// Checks for `Mutex::lock` calls in `if let` expression
14 /// with lock calls in any of the else blocks.
16 /// ### Why is this bad?
17 /// The Mutex lock remains held for the whole
18 /// `if let ... else` block and deadlocks.
22 /// if let Ok(thing) = mutex.lock() {
30 /// let locked = mutex.lock();
31 /// if let Ok(thing) = locked {
34 /// use_locked(locked);
39 "locking a `Mutex` in an `if let` block can cause deadlocks"
42 declare_lint_pass!(IfLetMutex => [IF_LET_MUTEX]);
44 impl<'tcx> LateLintPass<'tcx> for IfLetMutex {
45 fn check_expr(&mut self, cx: &LateContext<'tcx>, ex: &'tcx Expr<'tcx>) {
46 let mut arm_visit = ArmVisitor {
47 mutex_lock_called: false,
51 let mut op_visit = OppVisitor {
52 mutex_lock_called: false,
56 if let ExprKind::Match(
59 MatchSource::IfLetDesugar {
60 contains_else_clause: true,
64 op_visit.visit_expr(op);
65 if op_visit.mutex_lock_called {
67 arm_visit.visit_arm(arm);
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> {
95 fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
96 if let Some(mutex) = is_mutex_lock_call(self.cx, expr) {
97 self.found_mutex = Some(mutex);
98 self.mutex_lock_called = true;
101 visit::walk_expr(self, expr);
104 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
105 NestedVisitorMap::None
109 /// Checks if `Mutex::lock` is called in any of the branches.
110 pub struct ArmVisitor<'a, 'tcx> {
111 mutex_lock_called: bool,
112 found_mutex: Option<&'tcx Expr<'tcx>>,
113 cx: &'a LateContext<'tcx>,
116 impl<'tcx> Visitor<'tcx> for ArmVisitor<'_, 'tcx> {
117 type Map = Map<'tcx>;
119 fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
120 if let Some(mutex) = is_mutex_lock_call(self.cx, expr) {
121 self.found_mutex = Some(mutex);
122 self.mutex_lock_called = true;
125 visit::walk_expr(self, expr);
128 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
129 NestedVisitorMap::None
133 impl<'tcx, 'l> ArmVisitor<'tcx, 'l> {
134 fn same_mutex(&self, cx: &LateContext<'_>, op_mutex: &Expr<'_>) -> bool {
136 .map_or(false, |arm_mutex| SpanlessEq::new(cx).eq_expr(op_mutex, arm_mutex))
140 fn is_mutex_lock_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
142 if let ExprKind::MethodCall(path, _span, args, _) = &expr.kind;
143 if path.ident.as_str() == "lock";
144 let ty = cx.typeck_results().expr_ty(&args[0]);
145 if is_type_diagnostic_item(cx, ty, sym!(mutex_type));