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