Skip to content

Commit f82bf47

Browse files
committed
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
2 parents f51fb34 + 634e79c commit f82bf47

File tree

6 files changed

+64
-9
lines changed

6 files changed

+64
-9
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ itertools = "0.10"
4747
quote = "1.0"
4848
serde = { version = "1.0", features = ["derive"] }
4949
syn = { version = "1.0", features = ["full"] }
50+
parking_lot = "0.11.2"
5051

5152
[build-dependencies]
5253
rustc_tools_util = { version = "0.2", path = "rustc_tools_util" }

clippy_lints/src/let_underscore.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ declare_clippy_lint! {
3434

3535
declare_clippy_lint! {
3636
/// ### What it does
37-
/// Checks for `let _ = sync_lock`
37+
/// Checks for `let _ = sync_lock`.
38+
/// This supports `mutex` and `rwlock` in `std::sync` and `parking_lot`.
3839
///
3940
/// ### Why is this bad?
4041
/// This statement immediately drops the lock instead of
@@ -104,10 +105,12 @@ declare_clippy_lint! {
104105

105106
declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE, LET_UNDERSCORE_LOCK, LET_UNDERSCORE_DROP]);
106107

107-
const SYNC_GUARD_PATHS: [&[&str]; 3] = [
108+
const SYNC_GUARD_PATHS: [&[&str]; 5] = [
108109
&paths::MUTEX_GUARD,
109110
&paths::RWLOCK_READ_GUARD,
110111
&paths::RWLOCK_WRITE_GUARD,
112+
&paths::PARKING_LOT_RAWMUTEX,
113+
&paths::PARKING_LOT_RAWRWLOCK,
111114
];
112115

113116
impl<'tcx> LateLintPass<'tcx> for LetUnderscore {

clippy_utils/src/paths.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ pub(super) const PANICKING_PANIC: [&str; 3] = ["core", "panicking", "panic"];
110110
pub(super) const PANICKING_PANIC_FMT: [&str; 3] = ["core", "panicking", "panic_fmt"];
111111
pub(super) const PANICKING_PANIC_STR: [&str; 3] = ["core", "panicking", "panic_str"];
112112
pub(super) const PANIC_ANY: [&str; 3] = ["std", "panic", "panic_any"];
113+
pub const PARKING_LOT_RAWMUTEX: [&str; 3] = ["parking_lot", "raw_mutex", "RawMutex"];
114+
pub const PARKING_LOT_RAWRWLOCK: [&str; 3] = ["parking_lot", "raw_rwlock", "RawRwLock"];
113115
pub const PARKING_LOT_MUTEX_GUARD: [&str; 2] = ["parking_lot", "MutexGuard"];
114116
pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 2] = ["parking_lot", "RwLockReadGuard"];
115117
pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 2] = ["parking_lot", "RwLockWriteGuard"];

tests/compile-test.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ static TEST_DEPENDENCIES: &[&str] = &[
2828
"serde",
2929
"serde_derive",
3030
"syn",
31+
"parking_lot",
3132
];
3233

3334
// Test dependencies may need an `extern crate` here to ensure that they show up
@@ -41,6 +42,8 @@ extern crate if_chain;
4142
#[allow(unused_extern_crates)]
4243
extern crate itertools;
4344
#[allow(unused_extern_crates)]
45+
extern crate parking_lot;
46+
#[allow(unused_extern_crates)]
4447
extern crate quote;
4548
#[allow(unused_extern_crates)]
4649
extern crate syn;

tests/ui/let_underscore_lock.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![warn(clippy::let_underscore_lock)]
22

3+
extern crate parking_lot;
4+
35
fn main() {
46
let m = std::sync::Mutex::new(());
57
let rw = std::sync::RwLock::new(());
@@ -10,4 +12,16 @@ fn main() {
1012
let _ = m.try_lock();
1113
let _ = rw.try_read();
1214
let _ = rw.try_write();
15+
16+
use parking_lot::{lock_api::RawMutex, Mutex, RwLock};
17+
18+
let p_m: Mutex<()> = Mutex::const_new(RawMutex::INIT, ());
19+
let _ = p_m.lock();
20+
21+
let p_m1 = Mutex::new(0);
22+
let _ = p_m1.lock();
23+
24+
let p_rw = RwLock::new(0);
25+
let _ = p_rw.read();
26+
let _ = p_rw.write();
1327
}

tests/ui/let_underscore_lock.stderr

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: non-binding let on a synchronization lock
2-
--> $DIR/let_underscore_lock.rs:7:5
2+
--> $DIR/let_underscore_lock.rs:9:5
33
|
44
LL | let _ = m.lock();
55
| ^^^^^^^^^^^^^^^^^
@@ -8,44 +8,76 @@ LL | let _ = m.lock();
88
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
99

1010
error: non-binding let on a synchronization lock
11-
--> $DIR/let_underscore_lock.rs:8:5
11+
--> $DIR/let_underscore_lock.rs:10:5
1212
|
1313
LL | let _ = rw.read();
1414
| ^^^^^^^^^^^^^^^^^^
1515
|
1616
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
1717

1818
error: non-binding let on a synchronization lock
19-
--> $DIR/let_underscore_lock.rs:9:5
19+
--> $DIR/let_underscore_lock.rs:11:5
2020
|
2121
LL | let _ = rw.write();
2222
| ^^^^^^^^^^^^^^^^^^^
2323
|
2424
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
2525

2626
error: non-binding let on a synchronization lock
27-
--> $DIR/let_underscore_lock.rs:10:5
27+
--> $DIR/let_underscore_lock.rs:12:5
2828
|
2929
LL | let _ = m.try_lock();
3030
| ^^^^^^^^^^^^^^^^^^^^^
3131
|
3232
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
3333

3434
error: non-binding let on a synchronization lock
35-
--> $DIR/let_underscore_lock.rs:11:5
35+
--> $DIR/let_underscore_lock.rs:13:5
3636
|
3737
LL | let _ = rw.try_read();
3838
| ^^^^^^^^^^^^^^^^^^^^^^
3939
|
4040
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
4141

4242
error: non-binding let on a synchronization lock
43-
--> $DIR/let_underscore_lock.rs:12:5
43+
--> $DIR/let_underscore_lock.rs:14:5
4444
|
4545
LL | let _ = rw.try_write();
4646
| ^^^^^^^^^^^^^^^^^^^^^^^
4747
|
4848
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
4949

50-
error: aborting due to 6 previous errors
50+
error: non-binding let on a synchronization lock
51+
--> $DIR/let_underscore_lock.rs:19:5
52+
|
53+
LL | let _ = p_m.lock();
54+
| ^^^^^^^^^^^^^^^^^^^
55+
|
56+
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
57+
58+
error: non-binding let on a synchronization lock
59+
--> $DIR/let_underscore_lock.rs:22:5
60+
|
61+
LL | let _ = p_m1.lock();
62+
| ^^^^^^^^^^^^^^^^^^^^
63+
|
64+
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
65+
66+
error: non-binding let on a synchronization lock
67+
--> $DIR/let_underscore_lock.rs:25:5
68+
|
69+
LL | let _ = p_rw.read();
70+
| ^^^^^^^^^^^^^^^^^^^^
71+
|
72+
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
73+
74+
error: non-binding let on a synchronization lock
75+
--> $DIR/let_underscore_lock.rs:26:5
76+
|
77+
LL | let _ = p_rw.write();
78+
| ^^^^^^^^^^^^^^^^^^^^^
79+
|
80+
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
81+
82+
error: aborting due to 10 previous errors
5183

0 commit comments

Comments
 (0)