]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/if_let_mutex.rs
Rollup merge of #91482 - JosephTLyons:update-HashMap-and-BTreeMap-documentation,...
[rust.git] / src / tools / clippy / clippy_lints / src / if_let_mutex.rs
1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::higher;
3 use clippy_utils::ty::is_type_diagnostic_item;
4 use clippy_utils::SpanlessEq;
5 use if_chain::if_chain;
6 use rustc_hir::intravisit::{self as visit, NestedVisitorMap, Visitor};
7 use rustc_hir::{Expr, ExprKind};
8 use rustc_lint::{LateContext, LateLintPass};
9 use rustc_middle::hir::map::Map;
10 use rustc_session::{declare_lint_pass, declare_tool_lint};
11 use rustc_span::sym;
12
13 declare_clippy_lint! {
14     /// ### What it does
15     /// Checks for `Mutex::lock` calls in `if let` expression
16     /// with lock calls in any of the else blocks.
17     ///
18     /// ### Why is this bad?
19     /// The Mutex lock remains held for the whole
20     /// `if let ... else` block and deadlocks.
21     ///
22     /// ### Example
23     /// ```rust,ignore
24     /// if let Ok(thing) = mutex.lock() {
25     ///     do_thing();
26     /// } else {
27     ///     mutex.lock();
28     /// }
29     /// ```
30     /// Should be written
31     /// ```rust,ignore
32     /// let locked = mutex.lock();
33     /// if let Ok(thing) = locked {
34     ///     do_thing(thing);
35     /// } else {
36     ///     use_locked(locked);
37     /// }
38     /// ```
39     #[clippy::version = "1.45.0"]
40     pub IF_LET_MUTEX,
41     correctness,
42     "locking a `Mutex` in an `if let` block can cause deadlocks"
43 }
44
45 declare_lint_pass!(IfLetMutex => [IF_LET_MUTEX]);
46
47 impl<'tcx> LateLintPass<'tcx> for IfLetMutex {
48     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
49         let mut arm_visit = ArmVisitor {
50             mutex_lock_called: false,
51             found_mutex: None,
52             cx,
53         };
54         let mut op_visit = OppVisitor {
55             mutex_lock_called: false,
56             found_mutex: None,
57             cx,
58         };
59         if let Some(higher::IfLet {
60             let_expr,
61             if_then,
62             if_else: Some(if_else),
63             ..
64         }) = higher::IfLet::hir(cx, expr)
65         {
66             op_visit.visit_expr(let_expr);
67             if op_visit.mutex_lock_called {
68                 arm_visit.visit_expr(if_then);
69                 arm_visit.visit_expr(if_else);
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                         expr.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 let Some(mutex) = is_mutex_lock_call(self.cx, expr) {
98             self.found_mutex = Some(mutex);
99             self.mutex_lock_called = true;
100             return;
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<'a, 'tcx> {
112     mutex_lock_called: bool,
113     found_mutex: Option<&'tcx Expr<'tcx>>,
114     cx: &'a LateContext<'tcx>,
115 }
116
117 impl<'tcx> Visitor<'tcx> for ArmVisitor<'_, 'tcx> {
118     type Map = Map<'tcx>;
119
120     fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
121         if let Some(mutex) = is_mutex_lock_call(self.cx, expr) {
122             self.found_mutex = Some(mutex);
123             self.mutex_lock_called = true;
124             return;
125         }
126         visit::walk_expr(self, expr);
127     }
128
129     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
130         NestedVisitorMap::None
131     }
132 }
133
134 impl<'tcx, 'l> ArmVisitor<'tcx, 'l> {
135     fn same_mutex(&self, cx: &LateContext<'_>, op_mutex: &Expr<'_>) -> bool {
136         self.found_mutex
137             .map_or(false, |arm_mutex| SpanlessEq::new(cx).eq_expr(op_mutex, arm_mutex))
138     }
139 }
140
141 fn is_mutex_lock_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
142     if_chain! {
143         if let ExprKind::MethodCall(path, _span, [self_arg, ..], _) = &expr.kind;
144         if path.ident.as_str() == "lock";
145         let ty = cx.typeck_results().expr_ty(self_arg);
146         if is_type_diagnostic_item(cx, ty, sym::Mutex);
147         then {
148             Some(self_arg)
149         } else {
150             None
151         }
152     }
153 }