about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-11-15 00:50:28 +0000
committerbors <bors@rust-lang.org>2021-11-15 00:50:28 +0000
commitf82bf470d029cc4c998bfbe15acec8ec701c2aab (patch)
tree14afb966ff8321143a477ea3246f8fc9cfe4ac09
parentf51fb341dd3ff79f400ee816dceb81eb2d0c5106 (diff)
parent634e79c65574269a6a6dc2fec3b78848ddf2fdd0 (diff)
downloadrust-f82bf470d029cc4c998bfbe15acec8ec701c2aab.tar.gz
rust-f82bf470d029cc4c998bfbe15acec8ec701c2aab.zip
Auto merge of #7957 - surechen:fix_for_7854, r=giraffate
Support suggestion for #7854

I think the detection of parking_lot's mutex and rwlock is valuable, so submit this pr, please help judge and review, thank you.

Make let_underscore_lock support parking_lot.(Fixes #7854)

changelog: Make let_underscore_lock support parking_lot
-rw-r--r--Cargo.toml1
-rw-r--r--clippy_lints/src/let_underscore.rs7
-rw-r--r--clippy_utils/src/paths.rs2
-rw-r--r--tests/compile-test.rs3
-rw-r--r--tests/ui/let_underscore_lock.rs14
-rw-r--r--tests/ui/let_underscore_lock.stderr46
6 files changed, 64 insertions, 9 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 602877bb9d6..fdafdffca64 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -47,6 +47,7 @@ itertools = "0.10"
 quote = "1.0"
 serde = { version = "1.0", features = ["derive"] }
 syn = { version = "1.0", features = ["full"] }
+parking_lot = "0.11.2"
 
 [build-dependencies]
 rustc_tools_util = { version = "0.2", path = "rustc_tools_util" }
diff --git a/clippy_lints/src/let_underscore.rs b/clippy_lints/src/let_underscore.rs
index 557051fb8d2..d03276f7f98 100644
--- a/clippy_lints/src/let_underscore.rs
+++ b/clippy_lints/src/let_underscore.rs
@@ -34,7 +34,8 @@ declare_clippy_lint! {
 
 declare_clippy_lint! {
     /// ### What it does
-    /// Checks for `let _ = sync_lock`
+    /// Checks for `let _ = sync_lock`.
+    /// This supports `mutex` and `rwlock` in `std::sync` and `parking_lot`.
     ///
     /// ### Why is this bad?
     /// This statement immediately drops the lock instead of
@@ -104,10 +105,12 @@ declare_clippy_lint! {
 
 declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE, LET_UNDERSCORE_LOCK, LET_UNDERSCORE_DROP]);
 
-const SYNC_GUARD_PATHS: [&[&str]; 3] = [
+const SYNC_GUARD_PATHS: [&[&str]; 5] = [
     &paths::MUTEX_GUARD,
     &paths::RWLOCK_READ_GUARD,
     &paths::RWLOCK_WRITE_GUARD,
+    &paths::PARKING_LOT_RAWMUTEX,
+    &paths::PARKING_LOT_RAWRWLOCK,
 ];
 
 impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
diff --git a/clippy_utils/src/paths.rs b/clippy_utils/src/paths.rs
index 501b08a47f1..480923bbb02 100644
--- a/clippy_utils/src/paths.rs
+++ b/clippy_utils/src/paths.rs
@@ -110,6 +110,8 @@ pub(super) const PANICKING_PANIC: [&str; 3] = ["core", "panicking", "panic"];
 pub(super) const PANICKING_PANIC_FMT: [&str; 3] = ["core", "panicking", "panic_fmt"];
 pub(super) const PANICKING_PANIC_STR: [&str; 3] = ["core", "panicking", "panic_str"];
 pub(super) const PANIC_ANY: [&str; 3] = ["std", "panic", "panic_any"];
+pub const PARKING_LOT_RAWMUTEX: [&str; 3] = ["parking_lot", "raw_mutex", "RawMutex"];
+pub const PARKING_LOT_RAWRWLOCK: [&str; 3] = ["parking_lot", "raw_rwlock", "RawRwLock"];
 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"];
diff --git a/tests/compile-test.rs b/tests/compile-test.rs
index f25cf1d3ef5..a2d58491872 100644
--- a/tests/compile-test.rs
+++ b/tests/compile-test.rs
@@ -28,6 +28,7 @@ static TEST_DEPENDENCIES: &[&str] = &[
     "serde",
     "serde_derive",
     "syn",
+    "parking_lot",
 ];
 
 // Test dependencies may need an `extern crate` here to ensure that they show up
@@ -41,6 +42,8 @@ extern crate if_chain;
 #[allow(unused_extern_crates)]
 extern crate itertools;
 #[allow(unused_extern_crates)]
+extern crate parking_lot;
+#[allow(unused_extern_crates)]
 extern crate quote;
 #[allow(unused_extern_crates)]
 extern crate syn;
diff --git a/tests/ui/let_underscore_lock.rs b/tests/ui/let_underscore_lock.rs
index 88fb216a743..539d74d1d4c 100644
--- a/tests/ui/let_underscore_lock.rs
+++ b/tests/ui/let_underscore_lock.rs
@@ -1,5 +1,7 @@
 #![warn(clippy::let_underscore_lock)]
 
+extern crate parking_lot;
+
 fn main() {
     let m = std::sync::Mutex::new(());
     let rw = std::sync::RwLock::new(());
@@ -10,4 +12,16 @@ fn main() {
     let _ = m.try_lock();
     let _ = rw.try_read();
     let _ = rw.try_write();
+
+    use parking_lot::{lock_api::RawMutex, Mutex, RwLock};
+
+    let p_m: Mutex<()> = Mutex::const_new(RawMutex::INIT, ());
+    let _ = p_m.lock();
+
+    let p_m1 = Mutex::new(0);
+    let _ = p_m1.lock();
+
+    let p_rw = RwLock::new(0);
+    let _ = p_rw.read();
+    let _ = p_rw.write();
 }
diff --git a/tests/ui/let_underscore_lock.stderr b/tests/ui/let_underscore_lock.stderr
index 5d5f6059ef1..3a2bc17bf7b 100644
--- a/tests/ui/let_underscore_lock.stderr
+++ b/tests/ui/let_underscore_lock.stderr
@@ -1,5 +1,5 @@
 error: non-binding let on a synchronization lock
-  --> $DIR/let_underscore_lock.rs:7:5
+  --> $DIR/let_underscore_lock.rs:9:5
    |
 LL |     let _ = m.lock();
    |     ^^^^^^^^^^^^^^^^^
@@ -8,7 +8,7 @@ LL |     let _ = m.lock();
    = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
 
 error: non-binding let on a synchronization lock
-  --> $DIR/let_underscore_lock.rs:8:5
+  --> $DIR/let_underscore_lock.rs:10:5
    |
 LL |     let _ = rw.read();
    |     ^^^^^^^^^^^^^^^^^^
@@ -16,7 +16,7 @@ LL |     let _ = rw.read();
    = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
 
 error: non-binding let on a synchronization lock
-  --> $DIR/let_underscore_lock.rs:9:5
+  --> $DIR/let_underscore_lock.rs:11:5
    |
 LL |     let _ = rw.write();
    |     ^^^^^^^^^^^^^^^^^^^
@@ -24,7 +24,7 @@ LL |     let _ = rw.write();
    = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
 
 error: non-binding let on a synchronization lock
-  --> $DIR/let_underscore_lock.rs:10:5
+  --> $DIR/let_underscore_lock.rs:12:5
    |
 LL |     let _ = m.try_lock();
    |     ^^^^^^^^^^^^^^^^^^^^^
@@ -32,7 +32,7 @@ LL |     let _ = m.try_lock();
    = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
 
 error: non-binding let on a synchronization lock
-  --> $DIR/let_underscore_lock.rs:11:5
+  --> $DIR/let_underscore_lock.rs:13:5
    |
 LL |     let _ = rw.try_read();
    |     ^^^^^^^^^^^^^^^^^^^^^^
@@ -40,12 +40,44 @@ LL |     let _ = rw.try_read();
    = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
 
 error: non-binding let on a synchronization lock
-  --> $DIR/let_underscore_lock.rs:12:5
+  --> $DIR/let_underscore_lock.rs:14:5
    |
 LL |     let _ = rw.try_write();
    |     ^^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
 
-error: aborting due to 6 previous errors
+error: non-binding let on a synchronization lock
+  --> $DIR/let_underscore_lock.rs:19:5
+   |
+LL |     let _ = p_m.lock();
+   |     ^^^^^^^^^^^^^^^^^^^
+   |
+   = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
+
+error: non-binding let on a synchronization lock
+  --> $DIR/let_underscore_lock.rs:22:5
+   |
+LL |     let _ = p_m1.lock();
+   |     ^^^^^^^^^^^^^^^^^^^^
+   |
+   = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
+
+error: non-binding let on a synchronization lock
+  --> $DIR/let_underscore_lock.rs:25:5
+   |
+LL |     let _ = p_rw.read();
+   |     ^^^^^^^^^^^^^^^^^^^^
+   |
+   = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
+
+error: non-binding let on a synchronization lock
+  --> $DIR/let_underscore_lock.rs:26:5
+   |
+LL |     let _ = p_rw.write();
+   |     ^^^^^^^^^^^^^^^^^^^^^
+   |
+   = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
+
+error: aborting due to 10 previous errors