Skip to content

Support suggestion for #7854 #7957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
7 changes: 5 additions & 2 deletions clippy_lints/src/let_underscore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions clippy_utils/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down
3 changes: 3 additions & 0 deletions tests/compile-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/let_underscore_lock.rs
Original file line number Diff line number Diff line change
@@ -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(());
Expand All @@ -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();
}
46 changes: 39 additions & 7 deletions tests/ui/let_underscore_lock.stderr
Original file line number Diff line number Diff line change
@@ -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();
| ^^^^^^^^^^^^^^^^^
Expand All @@ -8,44 +8,76 @@ 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();
| ^^^^^^^^^^^^^^^^^^
|
= 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();
| ^^^^^^^^^^^^^^^^^^^
|
= 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();
| ^^^^^^^^^^^^^^^^^^^^^
|
= 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();
| ^^^^^^^^^^^^^^^^^^^^^^
|
= 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