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
This commit is contained in:
surechen 2021-11-11 09:59:31 +08:00
parent ed71addee7
commit 634e79c655
6 changed files with 64 additions and 9 deletions

View file

@ -47,6 +47,7 @@ itertools = "0.10"
quote = "1.0" quote = "1.0"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
syn = { version = "1.0", features = ["full"] } syn = { version = "1.0", features = ["full"] }
parking_lot = "0.11.2"
[build-dependencies] [build-dependencies]
rustc_tools_util = { version = "0.2", path = "rustc_tools_util" } rustc_tools_util = { version = "0.2", path = "rustc_tools_util" }

View file

@ -33,7 +33,8 @@ declare_clippy_lint! {
declare_clippy_lint! { declare_clippy_lint! {
/// ### What it does /// ### 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? /// ### Why is this bad?
/// This statement immediately drops the lock instead of /// 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]); 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::MUTEX_GUARD,
&paths::RWLOCK_READ_GUARD, &paths::RWLOCK_READ_GUARD,
&paths::RWLOCK_WRITE_GUARD, &paths::RWLOCK_WRITE_GUARD,
&paths::PARKING_LOT_RAWMUTEX,
&paths::PARKING_LOT_RAWRWLOCK,
]; ];
impl<'tcx> LateLintPass<'tcx> for LetUnderscore { impl<'tcx> LateLintPass<'tcx> for LetUnderscore {

View file

@ -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_FMT: [&str; 3] = ["core", "panicking", "panic_fmt"];
pub(super) const PANICKING_PANIC_STR: [&str; 3] = ["core", "panicking", "panic_str"]; pub(super) const PANICKING_PANIC_STR: [&str; 3] = ["core", "panicking", "panic_str"];
pub(super) const PANIC_ANY: [&str; 3] = ["std", "panic", "panic_any"]; 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_MUTEX_GUARD: [&str; 2] = ["parking_lot", "MutexGuard"];
pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 2] = ["parking_lot", "RwLockReadGuard"]; 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 PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 2] = ["parking_lot", "RwLockWriteGuard"];

View file

@ -28,6 +28,7 @@ static TEST_DEPENDENCIES: &[&str] = &[
"serde", "serde",
"serde_derive", "serde_derive",
"syn", "syn",
"parking_lot",
]; ];
// Test dependencies may need an `extern crate` here to ensure that they show up // 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)] #[allow(unused_extern_crates)]
extern crate itertools; extern crate itertools;
#[allow(unused_extern_crates)] #[allow(unused_extern_crates)]
extern crate parking_lot;
#[allow(unused_extern_crates)]
extern crate quote; extern crate quote;
#[allow(unused_extern_crates)] #[allow(unused_extern_crates)]
extern crate syn; extern crate syn;

View file

@ -1,5 +1,7 @@
#![warn(clippy::let_underscore_lock)] #![warn(clippy::let_underscore_lock)]
extern crate parking_lot;
fn main() { fn main() {
let m = std::sync::Mutex::new(()); let m = std::sync::Mutex::new(());
let rw = std::sync::RwLock::new(()); let rw = std::sync::RwLock::new(());
@ -10,4 +12,16 @@ fn main() {
let _ = m.try_lock(); let _ = m.try_lock();
let _ = rw.try_read(); let _ = rw.try_read();
let _ = rw.try_write(); 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();
} }

View file

@ -1,5 +1,5 @@
error: non-binding let on a synchronization lock 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(); 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` = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
error: non-binding let on a synchronization lock 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(); 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` = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
error: non-binding let on a synchronization lock 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(); 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` = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
error: non-binding let on a synchronization lock 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(); 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` = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
error: non-binding let on a synchronization lock 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(); 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` = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
error: non-binding let on a synchronization lock 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(); LL | let _ = rw.try_write();
| ^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^
| |
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop` = 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