]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/if_let_mutex.rs
change visitor name to OppVisitor
[rust.git] / clippy_lints / src / if_let_mutex.rs
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};
7
8 declare_clippy_lint! {
9     /// **What it does:** Checks for `Mutex::lock` calls in `if let` expression
10     /// with lock calls in any of the else blocks.
11     ///
12     /// **Why is this bad?** The Mutex lock remains held for the whole
13     /// `if let ... else` block and deadlocks.
14     ///
15     /// **Known problems:** None.
16     ///
17     /// **Example:**
18     ///
19     /// ```rust,ignore
20     /// if let Ok(thing) = mutex.lock() {
21     ///     do_thing();
22     /// } else {
23     ///     mutex.lock();
24     /// }
25     /// ```
26     /// Should be written
27     /// ```rust,ignore
28     /// let locked = mutex.lock();
29     /// if let Ok(thing) = locked {
30     ///     do_thing(thing);
31     /// } else {
32     ///     use_locked(locked);
33     /// }
34     /// ```
35     pub IF_LET_MUTEX,
36     correctness,
37     "locking a `Mutex` in an `if let` block can cause deadlocks"
38 }
39
40 declare_lint_pass!(IfLetMutex => [IF_LET_MUTEX]);
41
42 impl LateLintPass<'_, '_> for IfLetMutex {
43     fn check_expr(&mut self, cx: &LateContext<'_, '_>, ex: &'_ Expr<'_>) {
44         let mut arm_visit = ArmVisitor {
45             arm_mutex: false,
46             arm_lock: false,
47             cx,
48         };
49         let mut op_visit = OppVisitor {
50             op_mutex: false,
51             op_lock: false,
52             cx,
53         };
54         if let ExprKind::Match(
55             ref op,
56             ref arms,
57             MatchSource::IfLetDesugar {
58                 contains_else_clause: true,
59             },
60         ) = ex.kind
61         {
62             op_visit.visit_expr(op);
63             if op_visit.op_mutex && op_visit.op_lock {
64                 for arm in *arms {
65                     arm_visit.visit_arm(arm);
66                 }
67
68                 if arm_visit.arm_mutex && arm_visit.arm_lock {
69                     span_lint_and_help(
70                         cx,
71                         IF_LET_MUTEX,
72                         ex.span,
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",
75                     );
76                 }
77             }
78         }
79     }
80 }
81
82 /// Checks if `Mutex::lock` is called in the `if let _ = expr.
83 pub struct OppVisitor<'tcx, 'l> {
84     pub op_mutex: bool,
85     pub op_lock: bool,
86     pub cx: &'tcx LateContext<'tcx, 'l>,
87 }
88
89 impl<'tcx, 'l> Visitor<'tcx> for OppVisitor<'tcx, 'l> {
90     type Map = Map<'tcx>;
91
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" {
95                 self.op_lock = true;
96             }
97             let ty = self.cx.tables.expr_ty(&args[0]);
98             if match_type(self.cx, ty, &paths::MUTEX) {
99                 self.op_mutex = true;
100             }
101         }
102         visit::walk_expr(self, expr);
103     }
104
105     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
106         NestedVisitorMap::None
107     }
108 }
109
110 /// Checks if `Mutex::lock` is called in any of the branches.
111 pub struct ArmVisitor<'tcx, 'l> {
112     pub arm_mutex: bool,
113     pub arm_lock: bool,
114     pub cx: &'tcx LateContext<'tcx, 'l>,
115 }
116
117 impl<'tcx, 'l> Visitor<'tcx> for ArmVisitor<'tcx, 'l> {
118     type Map = Map<'tcx>;
119
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;
124             }
125             let ty = self.cx.tables.expr_ty(&args[0]);
126             if match_type(self.cx, ty, &paths::MUTEX) {
127                 self.arm_mutex = true;
128             }
129         }
130         visit::walk_expr(self, expr);
131     }
132
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 {
136                 match stmt.kind {
137                     StmtKind::Local(loc) => {
138                         if let Some(expr) = loc.init {
139                             self.visit_expr(expr)
140                         }
141                     },
142                     StmtKind::Expr(expr) => self.visit_expr(expr),
143                     StmtKind::Semi(expr) => self.visit_expr(expr),
144                     // we don't care about `Item`
145                     _ => {},
146                 }
147             }
148         };
149         visit::walk_arm(self, arm);
150     }
151
152     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
153         NestedVisitorMap::None
154     }
155 }