Skip to content

Commit 9306540

Browse files
committed
Remove overlap between rustc and clippy let_underscore_lock lint
1 parent ff89336 commit 9306540

File tree

4 files changed

+30
-82
lines changed

4 files changed

+30
-82
lines changed

clippy_lints/src/let_underscore.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::ty::{is_must_use_ty, is_type_diagnostic_item, match_type};
2+
use clippy_utils::ty::{is_must_use_ty, match_type};
33
use clippy_utils::{is_must_use_func_call, paths};
44
use rustc_hir::{Local, PatKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_middle::lint::in_external_macro;
77
use rustc_middle::ty::subst::GenericArgKind;
88
use rustc_session::{declare_lint_pass, declare_tool_lint};
9-
use rustc_span::{sym, Symbol};
109

1110
declare_clippy_lint! {
1211
/// ### What it does
@@ -34,8 +33,9 @@ declare_clippy_lint! {
3433

3534
declare_clippy_lint! {
3635
/// ### What it does
37-
/// Checks for `let _ = sync_lock`.
38-
/// This supports `mutex` and `rwlock` in `std::sync` and `parking_lot`.
36+
/// Checks for `let _ = sync_lock`. This supports `mutex` and `rwlock` in
37+
/// `parking_lot`. For `std` locks see the `rustc` lint
38+
/// [`let_underscore_lock`](https://doc.rust-lang.org/nightly/rustc/lints/listing/deny-by-default.html#let-underscore-lock)
3939
///
4040
/// ### Why is this bad?
4141
/// This statement immediately drops the lock instead of
@@ -61,8 +61,6 @@ declare_clippy_lint! {
6161

6262
declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE, LET_UNDERSCORE_LOCK]);
6363

64-
const SYNC_GUARD_SYMS: [Symbol; 3] = [sym::MutexGuard, sym::RwLockReadGuard, sym::RwLockWriteGuard];
65-
6664
const SYNC_GUARD_PATHS: [&[&str]; 3] = [
6765
&paths::PARKING_LOT_MUTEX_GUARD,
6866
&paths::PARKING_LOT_RWLOCK_READ_GUARD,
@@ -77,13 +75,7 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
7775
{
7876
let init_ty = cx.typeck_results().expr_ty(init);
7977
let contains_sync_guard = init_ty.walk().any(|inner| match inner.unpack() {
80-
GenericArgKind::Type(inner_ty) => {
81-
SYNC_GUARD_SYMS
82-
.iter()
83-
.any(|&sym| is_type_diagnostic_item(cx, inner_ty, sym))
84-
|| SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path))
85-
},
86-
78+
GenericArgKind::Type(inner_ty) => SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path)),
8779
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
8880
});
8981
if contains_sync_guard {

src/docs/let_underscore_lock.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### What it does
2-
Checks for `let _ = sync_lock`.
3-
This supports `mutex` and `rwlock` in `std::sync` and `parking_lot`.
2+
Checks for `let _ = sync_lock`. This supports `mutex` and `rwlock` in
3+
`parking_lot`. For `std` locks see the `rustc` lint
4+
[`let_underscore_lock`](https://doc.rust-lang.org/nightly/rustc/lints/listing/deny-by-default.html#let-underscore-lock)
45

56
### Why is this bad?
67
This statement immediately drops the lock instead of

tests/ui/let_underscore_lock.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,6 @@
33
extern crate parking_lot;
44

55
fn main() {
6-
let m = std::sync::Mutex::new(());
7-
let rw = std::sync::RwLock::new(());
8-
9-
let _ = m.lock();
10-
let _ = rw.read();
11-
let _ = rw.write();
12-
let _ = m.try_lock();
13-
let _ = rw.try_read();
14-
let _ = rw.try_write();
15-
16-
// These shouldn't throw an error.
17-
let _ = m;
18-
let _ = rw;
19-
206
use parking_lot::{lock_api::RawMutex, Mutex, RwLock};
217

228
let p_m: Mutex<()> = Mutex::const_new(RawMutex::INIT, ());
@@ -34,3 +20,20 @@ fn main() {
3420
let _ = p_m1;
3521
let _ = p_rw;
3622
}
23+
24+
fn uplifted() {
25+
// shouldn't lint std locks as they were uplifted as rustc's `let_underscore_lock`
26+
27+
let m = std::sync::Mutex::new(());
28+
let rw = std::sync::RwLock::new(());
29+
30+
let _ = m.lock();
31+
let _ = rw.read();
32+
let _ = rw.write();
33+
let _ = m.try_lock();
34+
let _ = rw.try_read();
35+
let _ = rw.try_write();
36+
37+
let _ = m;
38+
let _ = rw;
39+
}

tests/ui/let_underscore_lock.stderr

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,35 @@
11
error: non-binding let on a synchronization lock
22
--> $DIR/let_underscore_lock.rs:9:5
33
|
4-
LL | let _ = m.lock();
5-
| ^^^^^^^^^^^^^^^^^
6-
|
7-
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
8-
= note: `-D clippy::let-underscore-lock` implied by `-D warnings`
9-
10-
error: non-binding let on a synchronization lock
11-
--> $DIR/let_underscore_lock.rs:10:5
12-
|
13-
LL | let _ = rw.read();
14-
| ^^^^^^^^^^^^^^^^^^
15-
|
16-
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
17-
18-
error: non-binding let on a synchronization lock
19-
--> $DIR/let_underscore_lock.rs:11:5
20-
|
21-
LL | let _ = rw.write();
22-
| ^^^^^^^^^^^^^^^^^^^
23-
|
24-
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
25-
26-
error: non-binding let on a synchronization lock
27-
--> $DIR/let_underscore_lock.rs:12:5
28-
|
29-
LL | let _ = m.try_lock();
30-
| ^^^^^^^^^^^^^^^^^^^^^
31-
|
32-
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
33-
34-
error: non-binding let on a synchronization lock
35-
--> $DIR/let_underscore_lock.rs:13:5
36-
|
37-
LL | let _ = rw.try_read();
38-
| ^^^^^^^^^^^^^^^^^^^^^^
39-
|
40-
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
41-
42-
error: non-binding let on a synchronization lock
43-
--> $DIR/let_underscore_lock.rs:14:5
44-
|
45-
LL | let _ = rw.try_write();
46-
| ^^^^^^^^^^^^^^^^^^^^^^^
47-
|
48-
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
49-
50-
error: non-binding let on a synchronization lock
51-
--> $DIR/let_underscore_lock.rs:23:5
52-
|
534
LL | let _ = p_m.lock();
545
| ^^^^^^^^^^^^^^^^^^^
556
|
567
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
8+
= note: `-D clippy::let-underscore-lock` implied by `-D warnings`
579

5810
error: non-binding let on a synchronization lock
59-
--> $DIR/let_underscore_lock.rs:26:5
11+
--> $DIR/let_underscore_lock.rs:12:5
6012
|
6113
LL | let _ = p_m1.lock();
6214
| ^^^^^^^^^^^^^^^^^^^^
6315
|
6416
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
6517

6618
error: non-binding let on a synchronization lock
67-
--> $DIR/let_underscore_lock.rs:29:5
19+
--> $DIR/let_underscore_lock.rs:15:5
6820
|
6921
LL | let _ = p_rw.read();
7022
| ^^^^^^^^^^^^^^^^^^^^
7123
|
7224
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
7325

7426
error: non-binding let on a synchronization lock
75-
--> $DIR/let_underscore_lock.rs:30:5
27+
--> $DIR/let_underscore_lock.rs:16:5
7628
|
7729
LL | let _ = p_rw.write();
7830
| ^^^^^^^^^^^^^^^^^^^^^
7931
|
8032
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
8133

82-
error: aborting due to 10 previous errors
34+
error: aborting due to 4 previous errors
8335

0 commit comments

Comments
 (0)