From 634e79c65574269a6a6dc2fec3b78848ddf2fdd0 Mon Sep 17 00:00:00 2001 From: surechen Date: Thu, 11 Nov 2021 09:59:31 +0800 Subject: [PATCH] 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. changelog: Make let_underscore_lock support parking_lot --- Cargo.toml | 1 + clippy_lints/src/let_underscore.rs | 7 +++-- clippy_utils/src/paths.rs | 2 ++ tests/compile-test.rs | 3 ++ tests/ui/let_underscore_lock.rs | 14 +++++++++ tests/ui/let_underscore_lock.stderr | 46 ++++++++++++++++++++++++----- 6 files changed, 64 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ed7fb1440139f..a5514d05eefdf 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 9efd7aba7e83b..c9693115e3373 100644 --- a/clippy_lints/src/let_underscore.rs +++ b/clippy_lints/src/let_underscore.rs @@ -33,7 +33,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 @@ -101,10 +102,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 501b08a47f161..480923bbb02da 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 c15835ef29956..2e87db39b5713 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 88fb216a74329..539d74d1d4c9a 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 5d5f6059ef13e..3a2bc17bf7b20 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