1
Fork 0

Use diagnostic items instead of hard coded paths for let_underscore_lock

Using diagnostic items avoids having to update the paths if the guard
types ever get moved around for some reason. Additionally, it also greatly
simplifies the `is_sync_lock` check.
This commit is contained in:
Aaron Kofsky 2022-06-04 22:27:32 -04:00
parent e6b66784ac
commit 6342b58ef0
2 changed files with 14 additions and 20 deletions

View file

@ -3,7 +3,7 @@ use rustc_errors::Applicability;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_middle::{ use rustc_middle::{
lint::LintDiagnosticBuilder, lint::LintDiagnosticBuilder,
ty::{self, subst::GenericArgKind, Ty}, ty::{self, Ty},
}; };
use rustc_span::Symbol; use rustc_span::Symbol;
@ -114,12 +114,10 @@ declare_lint! {
declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_DROP, LET_UNDERSCORE_LOCK, LET_UNDERSCORE_MUST_USE]); declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_DROP, LET_UNDERSCORE_LOCK, LET_UNDERSCORE_MUST_USE]);
const SYNC_GUARD_PATHS: [&[&str]; 5] = [ const SYNC_GUARD_SYMBOLS: [Symbol; 3] = [
&["std", "sync", "mutex", "MutexGuard"], rustc_span::sym::MutexGuard,
&["std", "sync", "rwlock", "RwLockReadGuard"], rustc_span::sym::RwLockReadGuard,
&["std", "sync", "rwlock", "RwLockWriteGuard"], rustc_span::sym::RwLockWriteGuard,
&["parking_lot", "raw_mutex", "RawMutex"],
&["parking_lot", "raw_rwlock", "RawRwLock"],
]; ];
impl<'tcx> LateLintPass<'tcx> for LetUnderscore { impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
@ -134,19 +132,12 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
if !init_ty.needs_drop(cx.tcx, cx.param_env) { if !init_ty.needs_drop(cx.tcx, cx.param_env) {
return; return;
} }
let is_sync_lock = init_ty.walk().any(|inner| match inner.unpack() { let is_sync_lock = match init_ty.kind() {
GenericArgKind::Type(inner_ty) => { ty::Adt(adt, _) => SYNC_GUARD_SYMBOLS
SYNC_GUARD_PATHS.iter().any(|guard_path| match inner_ty.kind() { .iter()
ty::Adt(adt, _) => { .any(|guard_symbol| cx.tcx.is_diagnostic_item(*guard_symbol, adt.did())),
let ty_path = cx.get_def_path(adt.did());
guard_path.iter().map(|x| Symbol::intern(x)).eq(ty_path.iter().copied())
}
_ => false, _ => false,
}) };
}
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
});
let is_must_use_ty = is_must_use_ty(cx, cx.typeck_results().expr_ty(init)); let is_must_use_ty = is_must_use_ty(cx, cx.typeck_results().expr_ty(init));
let is_must_use_func_call = is_must_use_func_call(cx, init); let is_must_use_func_call = is_must_use_func_call(cx, init);

View file

@ -213,6 +213,7 @@ symbols! {
LinkedList, LinkedList,
LintPass, LintPass,
Mutex, Mutex,
MutexGuard,
N, N,
None, None,
Ok, Ok,
@ -250,6 +251,8 @@ symbols! {
Right, Right,
RustcDecodable, RustcDecodable,
RustcEncodable, RustcEncodable,
RwLockReadGuard,
RwLockWriteGuard,
Send, Send,
SeqCst, SeqCst,
SliceIndex, SliceIndex,