mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-18 18:53:04 +00:00
Switch to matching against full paths instead of just the last element of the path
This commit is contained in:
parent
6c25c3c381
commit
2dc8c083f5
@ -1,10 +1,10 @@
|
||||
use crate::utils::span_lint_and_note;
|
||||
use if_chain::if_chain;
|
||||
use crate::utils::{match_def_path, paths, span_lint_and_note};
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_hir::intravisit::FnKind;
|
||||
use rustc_hir::{Body, FnDecl, HirId, IsAsync};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
use rustc_span::{Span, Symbol};
|
||||
use rustc_span::Span;
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// **What it does:** Checks for calls to await while holding a MutexGuard.
|
||||
@ -44,8 +44,6 @@ declare_clippy_lint! {
|
||||
"Inside an async function, holding a MutexGuard while calling await"
|
||||
}
|
||||
|
||||
const MUTEX_GUARD_TYPES: [&str; 3] = ["MutexGuard", "RwLockReadGuard", "RwLockWriteGuard"];
|
||||
|
||||
declare_lint_pass!(AwaitHoldingLock => [AWAIT_HOLDING_LOCK]);
|
||||
|
||||
impl LateLintPass<'_, '_> for AwaitHoldingLock {
|
||||
@ -62,21 +60,18 @@ impl LateLintPass<'_, '_> for AwaitHoldingLock {
|
||||
return;
|
||||
}
|
||||
|
||||
for ty_clause in &cx.tables.generator_interior_types {
|
||||
if_chain! {
|
||||
if let rustc_middle::ty::Adt(adt, _) = ty_clause.ty.kind;
|
||||
if let Some(&sym) = cx.get_def_path(adt.did).iter().last();
|
||||
if is_symbol_mutex_guard(sym);
|
||||
then {
|
||||
span_lint_and_note(
|
||||
cx,
|
||||
AWAIT_HOLDING_LOCK,
|
||||
ty_clause.span,
|
||||
"this MutexGuard is held across an 'await' point",
|
||||
ty_clause.scope_span.unwrap_or(span),
|
||||
"these are all the await points this lock is held through"
|
||||
for ty_cause in &cx.tables.generator_interior_types {
|
||||
if let rustc_middle::ty::Adt(adt, _) = ty_cause.ty.kind {
|
||||
if is_mutex_guard(cx, adt.did) {
|
||||
span_lint_and_note(
|
||||
cx,
|
||||
AWAIT_HOLDING_LOCK,
|
||||
ty_cause.span,
|
||||
"this MutexGuard is held across an 'await' point",
|
||||
ty_cause.scope_span.unwrap_or(span),
|
||||
"these are all the await points this lock is held through",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -89,12 +84,11 @@ fn is_async_fn(fn_kind: FnKind<'_>) -> bool {
|
||||
})
|
||||
}
|
||||
|
||||
fn is_symbol_mutex_guard(sym: Symbol) -> bool {
|
||||
let sym_str = sym.as_str();
|
||||
for ty in &MUTEX_GUARD_TYPES {
|
||||
if sym_str == *ty {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
fn is_mutex_guard(cx: &LateContext<'_, '_>, def_id: DefId) -> bool {
|
||||
match_def_path(cx, def_id, &paths::MUTEX_GUARD)
|
||||
|| match_def_path(cx, def_id, &paths::RWLOCK_READ_GUARD)
|
||||
|| match_def_path(cx, def_id, &paths::RWLOCK_WRITE_GUARD)
|
||||
|| match_def_path(cx, def_id, &paths::PARKING_LOT_MUTEX_GUARD)
|
||||
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_READ_GUARD)
|
||||
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_WRITE_GUARD)
|
||||
}
|
||||
|
@ -72,6 +72,9 @@ pub const ORD: [&str; 3] = ["core", "cmp", "Ord"];
|
||||
pub const OS_STRING: [&str; 4] = ["std", "ffi", "os_str", "OsString"];
|
||||
pub const OS_STRING_AS_OS_STR: [&str; 5] = ["std", "ffi", "os_str", "OsString", "as_os_str"];
|
||||
pub const OS_STR_TO_OS_STRING: [&str; 5] = ["std", "ffi", "os_str", "OsStr", "to_os_string"];
|
||||
pub const PARKING_LOT_MUTEX_GUARD: [&str; 2] = ["parking_lot", "MutexGuard"];
|
||||
pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 2] = ["parking_lot", "RwLockReadGuard"];
|
||||
pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 2] = ["parking_lot", "RwLockWriteGuard"];
|
||||
pub const PATH: [&str; 3] = ["std", "path", "Path"];
|
||||
pub const PATH_BUF: [&str; 3] = ["std", "path", "PathBuf"];
|
||||
pub const PATH_BUF_AS_PATH: [&str; 4] = ["std", "path", "PathBuf", "as_path"];
|
||||
|
Loading…
Reference in New Issue
Block a user