Remove overlap between rustc and clippy let_underscore_lock lint

This commit is contained in:
Alex Macleod 2022-10-23 14:04:50 +00:00
parent ff893366c1
commit 9306540f61
4 changed files with 30 additions and 82 deletions

View File

@ -1,12 +1,11 @@
use clippy_utils::diagnostics::span_lint_and_help; use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::ty::{is_must_use_ty, is_type_diagnostic_item, match_type}; use clippy_utils::ty::{is_must_use_ty, match_type};
use clippy_utils::{is_must_use_func_call, paths}; use clippy_utils::{is_must_use_func_call, paths};
use rustc_hir::{Local, PatKind}; use rustc_hir::{Local, PatKind};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::subst::GenericArgKind; use rustc_middle::ty::subst::GenericArgKind;
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::{sym, Symbol};
declare_clippy_lint! { declare_clippy_lint! {
/// ### What it does /// ### What it does
@ -34,8 +33,9 @@ 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
/// This supports `mutex` and `rwlock` in `std::sync` and `parking_lot`. /// `parking_lot`. For `std` locks see the `rustc` lint
/// [`let_underscore_lock`](https://doc.rust-lang.org/nightly/rustc/lints/listing/deny-by-default.html#let-underscore-lock)
/// ///
/// ### Why is this bad? /// ### Why is this bad?
/// This statement immediately drops the lock instead of /// This statement immediately drops the lock instead of
@ -61,8 +61,6 @@ declare_clippy_lint! {
declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE, LET_UNDERSCORE_LOCK]); declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE, LET_UNDERSCORE_LOCK]);
const SYNC_GUARD_SYMS: [Symbol; 3] = [sym::MutexGuard, sym::RwLockReadGuard, sym::RwLockWriteGuard];
const SYNC_GUARD_PATHS: [&[&str]; 3] = [ const SYNC_GUARD_PATHS: [&[&str]; 3] = [
&paths::PARKING_LOT_MUTEX_GUARD, &paths::PARKING_LOT_MUTEX_GUARD,
&paths::PARKING_LOT_RWLOCK_READ_GUARD, &paths::PARKING_LOT_RWLOCK_READ_GUARD,
@ -77,13 +75,7 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
{ {
let init_ty = cx.typeck_results().expr_ty(init); let init_ty = cx.typeck_results().expr_ty(init);
let contains_sync_guard = init_ty.walk().any(|inner| match inner.unpack() { let contains_sync_guard = init_ty.walk().any(|inner| match inner.unpack() {
GenericArgKind::Type(inner_ty) => { GenericArgKind::Type(inner_ty) => SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path)),
SYNC_GUARD_SYMS
.iter()
.any(|&sym| is_type_diagnostic_item(cx, inner_ty, sym))
|| SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path))
},
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false, GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
}); });
if contains_sync_guard { if contains_sync_guard {

View File

@ -1,6 +1,7 @@
### What it does ### What it does
Checks for `let _ = sync_lock`. Checks for `let _ = sync_lock`. This supports `mutex` and `rwlock` in
This supports `mutex` and `rwlock` in `std::sync` and `parking_lot`. `parking_lot`. For `std` locks see the `rustc` lint
[`let_underscore_lock`](https://doc.rust-lang.org/nightly/rustc/lints/listing/deny-by-default.html#let-underscore-lock)
### Why is this bad? ### Why is this bad?
This statement immediately drops the lock instead of This statement immediately drops the lock instead of

View File

@ -3,20 +3,6 @@
extern crate parking_lot; extern crate parking_lot;
fn main() { fn main() {
let m = std::sync::Mutex::new(());
let rw = std::sync::RwLock::new(());
let _ = m.lock();
let _ = rw.read();
let _ = rw.write();
let _ = m.try_lock();
let _ = rw.try_read();
let _ = rw.try_write();
// These shouldn't throw an error.
let _ = m;
let _ = rw;
use parking_lot::{lock_api::RawMutex, Mutex, RwLock}; use parking_lot::{lock_api::RawMutex, Mutex, RwLock};
let p_m: Mutex<()> = Mutex::const_new(RawMutex::INIT, ()); let p_m: Mutex<()> = Mutex::const_new(RawMutex::INIT, ());
@ -34,3 +20,20 @@ fn main() {
let _ = p_m1; let _ = p_m1;
let _ = p_rw; let _ = p_rw;
} }
fn uplifted() {
// shouldn't lint std locks as they were uplifted as rustc's `let_underscore_lock`
let m = std::sync::Mutex::new(());
let rw = std::sync::RwLock::new(());
let _ = m.lock();
let _ = rw.read();
let _ = rw.write();
let _ = m.try_lock();
let _ = rw.try_read();
let _ = rw.try_write();
let _ = m;
let _ = rw;
}

View File

@ -1,62 +1,14 @@
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:9:5
| |
LL | let _ = m.lock();
| ^^^^^^^^^^^^^^^^^
|
= help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
= note: `-D clippy::let-underscore-lock` implied by `-D warnings`
error: non-binding let on a synchronization lock
--> $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: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: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: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:14:5
|
LL | let _ = rw.try_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:23:5
|
LL | let _ = p_m.lock(); LL | let _ = p_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`
= note: `-D clippy::let-underscore-lock` implied by `-D warnings`
error: non-binding let on a synchronization lock error: non-binding let on a synchronization lock
--> $DIR/let_underscore_lock.rs:26:5 --> $DIR/let_underscore_lock.rs:12:5
| |
LL | let _ = p_m1.lock(); LL | let _ = p_m1.lock();
| ^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^
@ -64,7 +16,7 @@ LL | let _ = p_m1.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:29:5 --> $DIR/let_underscore_lock.rs:15:5
| |
LL | let _ = p_rw.read(); LL | let _ = p_rw.read();
| ^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^
@ -72,12 +24,12 @@ LL | let _ = p_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:30:5 --> $DIR/let_underscore_lock.rs:16:5
| |
LL | let _ = p_rw.write(); LL | let _ = p_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: aborting due to 10 previous errors error: aborting due to 4 previous errors