|
| 1 | +use crate::utils::span_lint_and_note; |
| 2 | +use if_chain::if_chain; |
| 3 | +use rustc_hir::intravisit::FnKind; |
| 4 | +use rustc_hir::{Body, FnDecl, HirId, IsAsync}; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 7 | +use rustc_span::{Span, Symbol}; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// **What it does:** Checks for calls to await while holding a MutexGuard. |
| 11 | + /// |
| 12 | + /// **Why is this bad?** This is almost certainly an error which can result |
| 13 | + /// in a deadlock because the reactor will invoke code not visible to the |
| 14 | + /// currently visible scope. |
| 15 | + /// |
| 16 | + /// **Known problems:** Detects only specifically named guard types: |
| 17 | + /// MutexGuard, RwLockReadGuard, and RwLockWriteGuard. |
| 18 | + /// |
| 19 | + /// **Example:** |
| 20 | + /// |
| 21 | + /// ```rust |
| 22 | + /// use std::sync::Mutex; |
| 23 | + /// |
| 24 | + /// async fn foo(x: &Mutex<u32>) { |
| 25 | + /// let guard = x.lock().unwrap(); |
| 26 | + /// *guard += 1; |
| 27 | + /// bar.await; |
| 28 | + /// } |
| 29 | + /// ``` |
| 30 | + /// Use instead: |
| 31 | + /// ```rust |
| 32 | + /// use std::sync::Mutex; |
| 33 | + /// |
| 34 | + /// async fn foo(x: &Mutex<u32>) { |
| 35 | + /// { |
| 36 | + /// let guard = x.lock().unwrap(); |
| 37 | + /// *guard += 1; |
| 38 | + /// } |
| 39 | + /// bar.await; |
| 40 | + /// } |
| 41 | + /// ``` |
| 42 | + pub AWAIT_HOLDING_LOCK, |
| 43 | + pedantic, |
| 44 | + "Inside an async function, holding a MutexGuard while calling await" |
| 45 | +} |
| 46 | + |
| 47 | +const MUTEX_GUARD_TYPES: [&str; 3] = ["MutexGuard", "RwLockReadGuard", "RwLockWriteGuard"]; |
| 48 | + |
| 49 | +declare_lint_pass!(AwaitHoldingLock => [AWAIT_HOLDING_LOCK]); |
| 50 | + |
| 51 | +impl LateLintPass<'_, '_> for AwaitHoldingLock { |
| 52 | + fn check_fn( |
| 53 | + &mut self, |
| 54 | + cx: &LateContext<'_, '_>, |
| 55 | + fn_kind: FnKind<'_>, |
| 56 | + _: &FnDecl<'_>, |
| 57 | + _: &Body<'_>, |
| 58 | + span: Span, |
| 59 | + _: HirId, |
| 60 | + ) { |
| 61 | + if !is_async_fn(fn_kind) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + for ty_clause in &cx.tables.generator_interior_types { |
| 66 | + if_chain! { |
| 67 | + if let rustc_middle::ty::Adt(adt, _) = ty_clause.ty.kind; |
| 68 | + if let Some(&sym) = cx.get_def_path(adt.did).iter().last(); |
| 69 | + if is_symbol_mutex_guard(sym); |
| 70 | + then { |
| 71 | + span_lint_and_note( |
| 72 | + cx, |
| 73 | + AWAIT_HOLDING_LOCK, |
| 74 | + ty_clause.span, |
| 75 | + "this MutexGuard is held across an 'await' point", |
| 76 | + ty_clause.scope_span.unwrap_or(span), |
| 77 | + "these are all the await points this lock is held through" |
| 78 | + ); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +fn is_async_fn(fn_kind: FnKind<'_>) -> bool { |
| 86 | + fn_kind.header().map_or(false, |h| match h.asyncness { |
| 87 | + IsAsync::Async => true, |
| 88 | + IsAsync::NotAsync => false, |
| 89 | + }) |
| 90 | +} |
| 91 | + |
| 92 | +fn is_symbol_mutex_guard(sym: Symbol) -> bool { |
| 93 | + let sym_str = sym.as_str(); |
| 94 | + for ty in &MUTEX_GUARD_TYPES { |
| 95 | + if sym_str == *ty { |
| 96 | + return true; |
| 97 | + } |
| 98 | + } |
| 99 | + false |
| 100 | +} |
0 commit comments