From 6342b58ef0f71ca0284dced4b1d22f9726c1c74a Mon Sep 17 00:00:00 2001 From: Aaron Kofsky Date: Sat, 4 Jun 2022 22:27:32 -0400 Subject: [PATCH] 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. --- compiler/rustc_lint/src/let_underscore.rs | 31 ++++++++--------------- compiler/rustc_span/src/symbol.rs | 3 +++ 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_lint/src/let_underscore.rs b/compiler/rustc_lint/src/let_underscore.rs index 1e4565a226c..2ad09312d9d 100644 --- a/compiler/rustc_lint/src/let_underscore.rs +++ b/compiler/rustc_lint/src/let_underscore.rs @@ -3,7 +3,7 @@ use rustc_errors::Applicability; use rustc_hir as hir; use rustc_middle::{ lint::LintDiagnosticBuilder, - ty::{self, subst::GenericArgKind, Ty}, + ty::{self, Ty}, }; use rustc_span::Symbol; @@ -114,12 +114,10 @@ declare_lint! { declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_DROP, LET_UNDERSCORE_LOCK, LET_UNDERSCORE_MUST_USE]); -const SYNC_GUARD_PATHS: [&[&str]; 5] = [ - &["std", "sync", "mutex", "MutexGuard"], - &["std", "sync", "rwlock", "RwLockReadGuard"], - &["std", "sync", "rwlock", "RwLockWriteGuard"], - &["parking_lot", "raw_mutex", "RawMutex"], - &["parking_lot", "raw_rwlock", "RawRwLock"], +const SYNC_GUARD_SYMBOLS: [Symbol; 3] = [ + rustc_span::sym::MutexGuard, + rustc_span::sym::RwLockReadGuard, + rustc_span::sym::RwLockWriteGuard, ]; 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) { return; } - let is_sync_lock = init_ty.walk().any(|inner| match inner.unpack() { - GenericArgKind::Type(inner_ty) => { - SYNC_GUARD_PATHS.iter().any(|guard_path| match inner_ty.kind() { - ty::Adt(adt, _) => { - let ty_path = cx.get_def_path(adt.did()); - guard_path.iter().map(|x| Symbol::intern(x)).eq(ty_path.iter().copied()) - } - _ => false, - }) - } - - GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false, - }); + let is_sync_lock = match init_ty.kind() { + ty::Adt(adt, _) => SYNC_GUARD_SYMBOLS + .iter() + .any(|guard_symbol| cx.tcx.is_diagnostic_item(*guard_symbol, adt.did())), + _ => false, + }; 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); diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 5c9c16350e4..bc9f5c910df 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -213,6 +213,7 @@ symbols! { LinkedList, LintPass, Mutex, + MutexGuard, N, None, Ok, @@ -250,6 +251,8 @@ symbols! { Right, RustcDecodable, RustcEncodable, + RwLockReadGuard, + RwLockWriteGuard, Send, SeqCst, SliceIndex,