Skip to content

Commit

Permalink
Switch to matching against full paths instead of just the last elemen…
Browse files Browse the repository at this point in the history
…t of the path
  • Loading branch information
rokob committed Apr 22, 2020
1 parent 6c25c3c commit 2dc8c08
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
48 changes: 21 additions & 27 deletions clippy_lints/src/await_holding_lock.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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",
);
}
}
}
}
}
Expand All @@ -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)
}
3 changes: 3 additions & 0 deletions clippy_lints/src/utils/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down

0 comments on commit 2dc8c08

Please sign in to comment.