Skip to content

Commit 6342b58

Browse files
committed
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.
1 parent e6b6678 commit 6342b58

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

compiler/rustc_lint/src/let_underscore.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_errors::Applicability;
33
use rustc_hir as hir;
44
use rustc_middle::{
55
lint::LintDiagnosticBuilder,
6-
ty::{self, subst::GenericArgKind, Ty},
6+
ty::{self, Ty},
77
};
88
use rustc_span::Symbol;
99

@@ -114,12 +114,10 @@ declare_lint! {
114114

115115
declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_DROP, LET_UNDERSCORE_LOCK, LET_UNDERSCORE_MUST_USE]);
116116

117-
const SYNC_GUARD_PATHS: [&[&str]; 5] = [
118-
&["std", "sync", "mutex", "MutexGuard"],
119-
&["std", "sync", "rwlock", "RwLockReadGuard"],
120-
&["std", "sync", "rwlock", "RwLockWriteGuard"],
121-
&["parking_lot", "raw_mutex", "RawMutex"],
122-
&["parking_lot", "raw_rwlock", "RawRwLock"],
117+
const SYNC_GUARD_SYMBOLS: [Symbol; 3] = [
118+
rustc_span::sym::MutexGuard,
119+
rustc_span::sym::RwLockReadGuard,
120+
rustc_span::sym::RwLockWriteGuard,
123121
];
124122

125123
impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
@@ -134,19 +132,12 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
134132
if !init_ty.needs_drop(cx.tcx, cx.param_env) {
135133
return;
136134
}
137-
let is_sync_lock = init_ty.walk().any(|inner| match inner.unpack() {
138-
GenericArgKind::Type(inner_ty) => {
139-
SYNC_GUARD_PATHS.iter().any(|guard_path| match inner_ty.kind() {
140-
ty::Adt(adt, _) => {
141-
let ty_path = cx.get_def_path(adt.did());
142-
guard_path.iter().map(|x| Symbol::intern(x)).eq(ty_path.iter().copied())
143-
}
144-
_ => false,
145-
})
146-
}
147-
148-
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
149-
});
135+
let is_sync_lock = match init_ty.kind() {
136+
ty::Adt(adt, _) => SYNC_GUARD_SYMBOLS
137+
.iter()
138+
.any(|guard_symbol| cx.tcx.is_diagnostic_item(*guard_symbol, adt.did())),
139+
_ => false,
140+
};
150141
let is_must_use_ty = is_must_use_ty(cx, cx.typeck_results().expr_ty(init));
151142
let is_must_use_func_call = is_must_use_func_call(cx, init);
152143

compiler/rustc_span/src/symbol.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ symbols! {
213213
LinkedList,
214214
LintPass,
215215
Mutex,
216+
MutexGuard,
216217
N,
217218
None,
218219
Ok,
@@ -250,6 +251,8 @@ symbols! {
250251
Right,
251252
RustcDecodable,
252253
RustcEncodable,
254+
RwLockReadGuard,
255+
RwLockWriteGuard,
253256
Send,
254257
SeqCst,
255258
SliceIndex,

0 commit comments

Comments
 (0)