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