Skip to content

Commit 2dc8c08

Browse files
committed
Switch to matching against full paths instead of just the last element of the path
1 parent 6c25c3c commit 2dc8c08

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

clippy_lints/src/await_holding_lock.rs

+21-27
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use crate::utils::span_lint_and_note;
2-
use if_chain::if_chain;
1+
use crate::utils::{match_def_path, paths, span_lint_and_note};
2+
use rustc_hir::def_id::DefId;
33
use rustc_hir::intravisit::FnKind;
44
use rustc_hir::{Body, FnDecl, HirId, IsAsync};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
7-
use rustc_span::{Span, Symbol};
7+
use rustc_span::Span;
88

99
declare_clippy_lint! {
1010
/// **What it does:** Checks for calls to await while holding a MutexGuard.
@@ -44,8 +44,6 @@ declare_clippy_lint! {
4444
"Inside an async function, holding a MutexGuard while calling await"
4545
}
4646

47-
const MUTEX_GUARD_TYPES: [&str; 3] = ["MutexGuard", "RwLockReadGuard", "RwLockWriteGuard"];
48-
4947
declare_lint_pass!(AwaitHoldingLock => [AWAIT_HOLDING_LOCK]);
5048

5149
impl LateLintPass<'_, '_> for AwaitHoldingLock {
@@ -62,21 +60,18 @@ impl LateLintPass<'_, '_> for AwaitHoldingLock {
6260
return;
6361
}
6462

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"
63+
for ty_cause in &cx.tables.generator_interior_types {
64+
if let rustc_middle::ty::Adt(adt, _) = ty_cause.ty.kind {
65+
if is_mutex_guard(cx, adt.did) {
66+
span_lint_and_note(
67+
cx,
68+
AWAIT_HOLDING_LOCK,
69+
ty_cause.span,
70+
"this MutexGuard is held across an 'await' point",
71+
ty_cause.scope_span.unwrap_or(span),
72+
"these are all the await points this lock is held through",
7873
);
79-
}
74+
}
8075
}
8176
}
8277
}
@@ -89,12 +84,11 @@ fn is_async_fn(fn_kind: FnKind<'_>) -> bool {
8984
})
9085
}
9186

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
87+
fn is_mutex_guard(cx: &LateContext<'_, '_>, def_id: DefId) -> bool {
88+
match_def_path(cx, def_id, &paths::MUTEX_GUARD)
89+
|| match_def_path(cx, def_id, &paths::RWLOCK_READ_GUARD)
90+
|| match_def_path(cx, def_id, &paths::RWLOCK_WRITE_GUARD)
91+
|| match_def_path(cx, def_id, &paths::PARKING_LOT_MUTEX_GUARD)
92+
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_READ_GUARD)
93+
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_WRITE_GUARD)
10094
}

clippy_lints/src/utils/paths.rs

+3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ pub const ORD: [&str; 3] = ["core", "cmp", "Ord"];
7272
pub const OS_STRING: [&str; 4] = ["std", "ffi", "os_str", "OsString"];
7373
pub const OS_STRING_AS_OS_STR: [&str; 5] = ["std", "ffi", "os_str", "OsString", "as_os_str"];
7474
pub const OS_STR_TO_OS_STRING: [&str; 5] = ["std", "ffi", "os_str", "OsStr", "to_os_string"];
75+
pub const PARKING_LOT_MUTEX_GUARD: [&str; 2] = ["parking_lot", "MutexGuard"];
76+
pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 2] = ["parking_lot", "RwLockReadGuard"];
77+
pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 2] = ["parking_lot", "RwLockWriteGuard"];
7578
pub const PATH: [&str; 3] = ["std", "path", "Path"];
7679
pub const PATH_BUF: [&str; 3] = ["std", "path", "PathBuf"];
7780
pub const PATH_BUF_AS_PATH: [&str; 4] = ["std", "path", "PathBuf", "as_path"];

0 commit comments

Comments
 (0)