about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAndy Weiss <dragonbear@google.com>2020-04-09 21:50:23 -0700
committerAndy Weiss <dragonbear@google.com>2020-04-21 21:07:43 -0700
commit2dc8c083f54454ca87bb09d691577eada2d23539 (patch)
treee074101fff3db9b85a2500e5d20469076642c396
parent6c25c3c381ccfa0bf9b7bc7386a09cc4421fd790 (diff)
downloadrust-2dc8c083f54454ca87bb09d691577eada2d23539.tar.gz
rust-2dc8c083f54454ca87bb09d691577eada2d23539.zip
Switch to matching against full paths instead of just the last element of the path
-rw-r--r--clippy_lints/src/await_holding_lock.rs48
-rw-r--r--clippy_lints/src/utils/paths.rs3
2 files changed, 24 insertions, 27 deletions
diff --git a/clippy_lints/src/await_holding_lock.rs b/clippy_lints/src/await_holding_lock.rs
index ae4c5bfc9f7..8e3e0ed0430 100644
--- a/clippy_lints/src/await_holding_lock.rs
+++ b/clippy_lints/src/await_holding_lock.rs
@@ -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)
 }
diff --git a/clippy_lints/src/utils/paths.rs b/clippy_lints/src/utils/paths.rs
index f85845be56d..7ad09eabec1 100644
--- a/clippy_lints/src/utils/paths.rs
+++ b/clippy_lints/src/utils/paths.rs
@@ -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"];