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
This commit is contained in:
bors 2021-11-15 00:50:28 +00:00
commit f82bf470d0
6 changed files with 64 additions and 9 deletions

View File

@ -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" }

View File

@ -34,7 +34,8 @@
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_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 {

View File

@ -110,6 +110,8 @@
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"];

View File

@ -28,6 +28,7 @@
"serde",
"serde_derive",
"syn",
"parking_lot",
];
// Test dependencies may need an `extern crate` here to ensure that they show up
@ -41,6 +42,8 @@
#[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;

View File

@ -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();
}

View File

@ -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