]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/if_let_mutex.rs
Merge commit '0e87918536b9833bbc6c683d1f9d51ee2bf03ef1' into clippyup
[rust.git] / clippy_lints / src / if_let_mutex.rs
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};
10
11 declare_clippy_lint! {
12     /// **What it does:** Checks for `Mutex::lock` calls in `if let` expression
13     /// with lock calls in any of the else blocks.
14     ///
15     /// **Why is this bad?** The Mutex lock remains held for the whole
16     /// `if let ... else` block and deadlocks.
17     ///
18     /// **Known problems:** None.
19     ///
20     /// **Example:**
21     ///
22     /// ```rust,ignore
23     /// if let Ok(thing) = mutex.lock() {
24     ///     do_thing();
25     /// } else {
26     ///     mutex.lock();
27     /// }
28     /// ```
29     /// Should be written
30     /// ```rust,ignore
31     /// let locked = mutex.lock();
32     /// if let Ok(thing) = locked {
33     ///     do_thing(thing);
34     /// } else {
35     ///     use_locked(locked);
36     /// }
37     /// ```
38     pub IF_LET_MUTEX,
39     correctness,
40     "locking a `Mutex` in an `if let` block can cause deadlocks"
41 }
42
43 declare_lint_pass!(IfLetMutex => [IF_LET_MUTEX]);
44
45 impl<'tcx> LateLintPass<'tcx> for IfLetMutex {
46     fn check_expr(&mut self, cx: &LateContext<'tcx>, ex: &'tcx Expr<'tcx>) {
47         let mut arm_visit = ArmVisitor {
48             mutex_lock_called: false,
49             found_mutex: None,
50             cx,
51         };
52         let mut op_visit = OppVisitor {
53             mutex_lock_called: false,
54             found_mutex: None,
55             cx,
56         };
57         if let ExprKind::Match(
58             ref op,
59             ref arms,
60             MatchSource::IfLetDesugar {
61                 contains_else_clause: true,
62             },
63         ) = ex.kind
64         {
65             op_visit.visit_expr(op);
66             if op_visit.mutex_lock_called {
67                 for arm in *arms {
68                     arm_visit.visit_arm(arm);
69                 }
70
71                 if arm_visit.mutex_lock_called && arm_visit.same_mutex(cx, op_visit.found_mutex.unwrap()) {
72                     span_lint_and_help(
73                         cx,
74                         IF_LET_MUTEX,
75                         ex.span,
76                         "calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a deadlock",
77                         None,
78                         "move the lock call outside of the `if let ...` expression",
79                     );
80                 }
81             }
82         }
83     }
84 }
85
86 /// Checks if `Mutex::lock` is called in the `if let _ = expr.
87 pub struct OppVisitor<'a, 'tcx> {
88     mutex_lock_called: bool,
89     found_mutex: Option<&'tcx Expr<'tcx>>,
90     cx: &'a LateContext<'tcx>,
91 }
92
93 impl<'tcx> Visitor<'tcx> for OppVisitor<'_, 'tcx> {
94     type Map = Map<'tcx>;
95
96     fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
97         if_chain! {
98             if let Some(mutex) = is_mutex_lock_call(self.cx, expr);
99             then {
100                 self.found_mutex = Some(mutex);
101                 self.mutex_lock_called = true;
102                 return;
103             }
104         }
105         visit::walk_expr(self, expr);
106     }
107
108     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
109         NestedVisitorMap::None
110     }
111 }
112
113 /// Checks if `Mutex::lock` is called in any of the branches.
114 pub struct ArmVisitor<'a, 'tcx> {
115     mutex_lock_called: bool,
116     found_mutex: Option<&'tcx Expr<'tcx>>,
117     cx: &'a LateContext<'tcx>,
118 }
119
120 impl<'tcx> Visitor<'tcx> for ArmVisitor<'_, 'tcx> {
121     type Map = Map<'tcx>;
122
123     fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
124         if_chain! {
125             if let Some(mutex) = is_mutex_lock_call(self.cx, expr);
126             then {
127                 self.found_mutex = Some(mutex);
128                 self.mutex_lock_called = true;
129                 return;
130             }
131         }
132         visit::walk_expr(self, expr);
133     }
134
135     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
136         NestedVisitorMap::None
137     }
138 }
139
140 impl<'tcx, 'l> ArmVisitor<'tcx, 'l> {
141     fn same_mutex(&self, cx: &LateContext<'_>, op_mutex: &Expr<'_>) -> bool {
142         self.found_mutex
143             .map_or(false, |arm_mutex| SpanlessEq::new(cx).eq_expr(op_mutex, arm_mutex))
144     }
145 }
146
147 fn is_mutex_lock_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
148     if_chain! {
149         if let ExprKind::MethodCall(path, _span, args, _) = &expr.kind;
150         if path.ident.as_str() == "lock";
151         let ty = cx.typeck_results().expr_ty(&args[0]);
152         if is_type_diagnostic_item(cx, ty, sym!(mutex_type));
153         then {
154             Some(&args[0])
155         } else {
156             None
157         }
158     }
159 }