1 use crate::utils::{match_type, paths, span_lint_and_help};
2 use rustc::hir::map::Map;
3 use rustc_hir::intravisit::{self as visit, NestedVisitorMap, Visitor};
4 use rustc_hir::{Arm, Expr, ExprKind, MatchSource, StmtKind};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
9 /// **What it does:** Checks for `Mutex::lock` calls in `if let` expression
10 /// with lock calls in any of the else blocks.
12 /// **Why is this bad?** The Mutex lock remains held for the whole
13 /// `if let ... else` block and deadlocks.
15 /// **Known problems:** None.
20 /// if let Ok(thing) = mutex.lock() {
28 /// let locked = mutex.lock();
29 /// if let Ok(thing) = locked {
32 /// use_locked(locked);
37 "locking a `Mutex` in an `if let` block can cause deadlocks"
40 declare_lint_pass!(IfLetMutex => [IF_LET_MUTEX]);
42 impl LateLintPass<'_, '_> for IfLetMutex {
43 fn check_expr(&mut self, cx: &LateContext<'_, '_>, ex: &'_ Expr<'_>) {
44 let mut arm_visit = ArmVisitor {
49 let mut op_visit = OppVisitor {
54 if let ExprKind::Match(
57 MatchSource::IfLetDesugar {
58 contains_else_clause: true,
62 op_visit.visit_expr(op);
63 if op_visit.op_mutex && op_visit.op_lock {
65 arm_visit.visit_arm(arm);
68 if arm_visit.arm_mutex && arm_visit.arm_lock {
73 "calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a deadlock",
74 "move the lock call outside of the `if let ...` expression",
82 /// Checks if `Mutex::lock` is called in the `if let _ = expr.
83 pub struct OppVisitor<'tcx, 'l> {
86 pub cx: &'tcx LateContext<'tcx, 'l>,
89 impl<'tcx, 'l> Visitor<'tcx> for OppVisitor<'tcx, 'l> {
92 fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
93 if let ExprKind::MethodCall(path, _span, args) = &expr.kind {
94 if path.ident.to_string() == "lock" {
97 let ty = self.cx.tables.expr_ty(&args[0]);
98 if match_type(self.cx, ty, &paths::MUTEX) {
102 visit::walk_expr(self, expr);
105 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
106 NestedVisitorMap::None
110 /// Checks if `Mutex::lock` is called in any of the branches.
111 pub struct ArmVisitor<'tcx, 'l> {
114 pub cx: &'tcx LateContext<'tcx, 'l>,
117 impl<'tcx, 'l> Visitor<'tcx> for ArmVisitor<'tcx, 'l> {
118 type Map = Map<'tcx>;
120 fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
121 if let ExprKind::MethodCall(path, _span, args) = &expr.kind {
122 if path.ident.to_string() == "lock" {
123 self.arm_lock = true;
125 let ty = self.cx.tables.expr_ty(&args[0]);
126 if match_type(self.cx, ty, &paths::MUTEX) {
127 self.arm_mutex = true;
130 visit::walk_expr(self, expr);
133 fn visit_arm(&mut self, arm: &'tcx Arm<'_>) {
134 if let ExprKind::Block(ref block, _l) = arm.body.kind {
135 for stmt in block.stmts {
137 StmtKind::Local(loc) => {
138 if let Some(expr) = loc.init {
139 self.visit_expr(expr)
142 StmtKind::Expr(expr) => self.visit_expr(expr),
143 StmtKind::Semi(expr) => self.visit_expr(expr),
144 // we don't care about `Item`
149 visit::walk_arm(self, arm);
152 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
153 NestedVisitorMap::None