Skip to content

Commit b6cab80

Browse files
committed
Use get_diagnostic_name
1 parent 33b9b95 commit b6cab80

File tree

6 files changed

+73
-113
lines changed

6 files changed

+73
-113
lines changed

compiler/rustc_lint/src/builtin.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -2478,14 +2478,11 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
24782478
// Find calls to `mem::{uninitialized,zeroed}` methods.
24792479
if let hir::ExprKind::Path(ref qpath) = path_expr.kind {
24802480
let def_id = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id()?;
2481-
2482-
if cx.tcx.is_diagnostic_item(sym::mem_zeroed, def_id) {
2483-
return Some(InitKind::Zeroed);
2484-
} else if cx.tcx.is_diagnostic_item(sym::mem_uninitialized, def_id) {
2485-
return Some(InitKind::Uninit);
2486-
} else if cx.tcx.is_diagnostic_item(sym::transmute, def_id) && is_zero(&args[0])
2487-
{
2488-
return Some(InitKind::Zeroed);
2481+
match cx.tcx.get_diagnostic_name(def_id) {
2482+
Some(sym::mem_zeroed) => return Some(InitKind::Zeroed),
2483+
Some(sym::mem_uninitialized) => return Some(InitKind::Uninit),
2484+
Some(sym::transmute) if is_zero(&args[0]) => return Some(InitKind::Zeroed),
2485+
_ => {}
24892486
}
24902487
}
24912488
} else if let hir::ExprKind::MethodCall(_, _, ref args, _) = expr.kind {
@@ -2497,11 +2494,10 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
24972494
if let hir::ExprKind::Call(ref path_expr, _) = args[0].kind {
24982495
if let hir::ExprKind::Path(ref qpath) = path_expr.kind {
24992496
let def_id = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id()?;
2500-
2501-
if cx.tcx.is_diagnostic_item(sym::maybe_uninit_zeroed, def_id) {
2502-
return Some(InitKind::Zeroed);
2503-
} else if cx.tcx.is_diagnostic_item(sym::maybe_uninit_uninit, def_id) {
2504-
return Some(InitKind::Uninit);
2497+
match cx.tcx.get_diagnostic_name(def_id) {
2498+
Some(sym::maybe_uninit_zeroed) => return Some(InitKind::Zeroed),
2499+
Some(sym::maybe_uninit_uninit) => return Some(InitKind::Uninit),
2500+
_ => {}
25052501
}
25062502
}
25072503
}
@@ -3091,8 +3087,10 @@ impl<'tcx> LateLintPass<'tcx> for DerefNullPtr {
30913087
rustc_hir::ExprKind::Call(ref path, _) => {
30923088
if let rustc_hir::ExprKind::Path(ref qpath) = path.kind {
30933089
if let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id() {
3094-
return cx.tcx.is_diagnostic_item(sym::ptr_null, def_id)
3095-
|| cx.tcx.is_diagnostic_item(sym::ptr_null_mut, def_id);
3090+
return matches!(
3091+
cx.tcx.get_diagnostic_name(def_id),
3092+
Some(sym::ptr_null | sym::ptr_null_mut)
3093+
);
30963094
}
30973095
}
30983096
}

compiler/rustc_lint/src/internal.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,10 @@ impl LateLintPass<'_> for DefaultHashTypes {
3333
// don't lint imports, only actual usages
3434
return;
3535
}
36-
let replace = if cx.tcx.is_diagnostic_item(sym::HashMap, def_id) {
37-
"FxHashMap"
38-
} else if cx.tcx.is_diagnostic_item(sym::HashSet, def_id) {
39-
"FxHashSet"
40-
} else {
41-
return;
36+
let replace = match cx.tcx.get_diagnostic_name(def_id) {
37+
Some(sym::HashMap) => "FxHashMap",
38+
Some(sym::HashSet) => "FxHashSet",
39+
_ => return,
4240
};
4341
cx.struct_span_lint(DEFAULT_HASH_TYPES, path.span, |lint| {
4442
let msg = format!(
@@ -174,26 +172,29 @@ fn is_ty_or_ty_ctxt(cx: &LateContext<'_>, ty: &Ty<'_>) -> Option<String> {
174172
if let TyKind::Path(qpath) = &ty.kind {
175173
if let QPath::Resolved(_, path) = qpath {
176174
match path.res {
177-
Res::Def(_, did) => {
178-
if cx.tcx.is_diagnostic_item(sym::Ty, did) {
179-
return Some(format!("Ty{}", gen_args(path.segments.last().unwrap())));
180-
} else if cx.tcx.is_diagnostic_item(sym::TyCtxt, did) {
181-
return Some(format!("TyCtxt{}", gen_args(path.segments.last().unwrap())));
175+
Res::Def(_, def_id) => {
176+
if let Some(name @ (sym::Ty | sym::TyCtxt)) = cx.tcx.get_diagnostic_name(def_id)
177+
{
178+
return Some(format!(
179+
"{}{}",
180+
name,
181+
gen_args(path.segments.last().unwrap())
182+
));
182183
}
183184
}
184185
// Only lint on `&Ty` and `&TyCtxt` if it is used outside of a trait.
185186
Res::SelfTy(None, Some((did, _))) => {
186187
if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() {
187-
if cx.tcx.is_diagnostic_item(sym::Ty, adt.did) {
188+
if let Some(name @ (sym::Ty | sym::TyCtxt)) =
189+
cx.tcx.get_diagnostic_name(adt.did)
190+
{
188191
// NOTE: This path is currently unreachable as `Ty<'tcx>` is
189192
// defined as a type alias meaning that `impl<'tcx> Ty<'tcx>`
190193
// is not actually allowed.
191194
//
192195
// I(@lcnr) still kept this branch in so we don't miss this
193196
// if we ever change it in the future.
194-
return Some(format!("Ty<{}>", substs[0]));
195-
} else if cx.tcx.is_diagnostic_item(sym::TyCtxt, adt.did) {
196-
return Some(format!("TyCtxt<{}>", substs[0]));
197+
return Some(format!("{}<{}>", name, substs[0]));
197198
}
198199
}
199200
}

compiler/rustc_lint/src/non_fmt_panic.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ impl<'tcx> LateLintPass<'tcx> for NonPanicFmt {
5454
|| Some(def_id) == cx.tcx.lang_items().panic_str()
5555
{
5656
if let Some(id) = f.span.ctxt().outer_expn_data().macro_def_id {
57-
if cx.tcx.is_diagnostic_item(sym::std_panic_2015_macro, id)
58-
|| cx.tcx.is_diagnostic_item(sym::core_panic_2015_macro, id)
59-
{
57+
if matches!(
58+
cx.tcx.get_diagnostic_name(id),
59+
Some(sym::core_panic_2015_macro | sym::std_panic_2015_macro)
60+
) {
6061
check_panic(cx, f, arg);
6162
}
6263
}

compiler/rustc_lint/src/noop_method_call.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
5151
Some((DefKind::AssocFn, did)) => match cx.tcx.trait_of_item(did) {
5252
// Check that we're dealing with a trait method for one of the traits we care about.
5353
Some(trait_id)
54-
if [sym::Clone, sym::Deref, sym::Borrow]
55-
.iter()
56-
.any(|s| cx.tcx.is_diagnostic_item(*s, trait_id)) =>
54+
if matches!(
55+
cx.tcx.get_diagnostic_name(trait_id),
56+
Some(sym::Borrow | sym::Clone | sym::Deref)
57+
) =>
5758
{
5859
(trait_id, did)
5960
}

compiler/rustc_lint/src/types.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1541,8 +1541,7 @@ impl InvalidAtomicOrdering {
15411541
if let ExprKind::Call(ref func, ref args) = expr.kind;
15421542
if let ExprKind::Path(ref func_qpath) = func.kind;
15431543
if let Some(def_id) = cx.qpath_res(func_qpath, func.hir_id).opt_def_id();
1544-
if cx.tcx.is_diagnostic_item(sym::fence, def_id) ||
1545-
cx.tcx.is_diagnostic_item(sym::compiler_fence, def_id);
1544+
if matches!(cx.tcx.get_diagnostic_name(def_id), Some(sym::fence | sym::compiler_fence));
15461545
if let ExprKind::Path(ref ordering_qpath) = &args[0].kind;
15471546
if let Some(ordering_def_id) = cx.qpath_res(ordering_qpath, args[0].hir_id).opt_def_id();
15481547
if Self::matches_ordering(cx, ordering_def_id, &[sym::Relaxed]);

compiler/rustc_typeck/src/check/method/suggest.rs

+35-75
Original file line numberDiff line numberDiff line change
@@ -1047,51 +1047,41 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10471047
err: &mut DiagnosticBuilder<'_>,
10481048
unsatisfied_predicates: &Vec<(ty::Predicate<'tcx>, Option<ty::Predicate<'tcx>>)>,
10491049
) {
1050-
let derivables = [
1051-
sym::Eq,
1052-
sym::PartialEq,
1053-
sym::Ord,
1054-
sym::PartialOrd,
1055-
sym::Clone,
1056-
sym::Copy,
1057-
sym::Hash,
1058-
sym::Default,
1059-
sym::Debug,
1060-
];
1061-
let mut derives = unsatisfied_predicates
1062-
.iter()
1063-
.filter_map(|(pred, _)| {
1064-
let trait_pred =
1065-
if let ty::PredicateKind::Trait(trait_pred) = pred.kind().skip_binder() {
1066-
trait_pred
1067-
} else {
1068-
return None;
1069-
};
1070-
let trait_ref = trait_pred.trait_ref;
1071-
let adt_def = if let ty::Adt(adt_def, _) = trait_ref.self_ty().kind() {
1072-
adt_def
1073-
} else {
1074-
return None;
1075-
};
1076-
if adt_def.did.is_local() {
1077-
let diagnostic_items = self.tcx.diagnostic_items(trait_ref.def_id.krate);
1078-
return derivables.iter().find_map(|trait_derivable| {
1079-
let item_def_id = diagnostic_items.name_to_id.get(trait_derivable)?;
1080-
if item_def_id == &trait_pred.trait_ref.def_id
1081-
&& !(adt_def.is_enum() && *trait_derivable == sym::Default)
1082-
{
1083-
return Some((
1084-
format!("{}", trait_ref.self_ty()),
1085-
self.tcx.def_span(adt_def.did),
1086-
format!("{}", trait_ref.print_only_trait_name()),
1087-
));
1088-
}
1089-
None
1090-
});
1091-
}
1092-
None
1093-
})
1094-
.collect::<Vec<(String, Span, String)>>();
1050+
let mut derives = Vec::<(String, Span, String)>::new();
1051+
let mut traits = Vec::<Span>::new();
1052+
for (pred, _) in unsatisfied_predicates {
1053+
let trait_pred = match pred.kind().skip_binder() {
1054+
ty::PredicateKind::Trait(trait_pred) => trait_pred,
1055+
_ => continue,
1056+
};
1057+
let adt = match trait_pred.self_ty().ty_adt_def() {
1058+
Some(adt) if adt.did.is_local() => adt,
1059+
_ => continue,
1060+
};
1061+
let can_derive = match self.tcx.get_diagnostic_name(trait_pred.def_id()) {
1062+
Some(sym::Default) => !adt.is_enum(),
1063+
Some(
1064+
sym::Eq
1065+
| sym::PartialEq
1066+
| sym::Ord
1067+
| sym::PartialOrd
1068+
| sym::Clone
1069+
| sym::Copy
1070+
| sym::Hash
1071+
| sym::Debug,
1072+
) => true,
1073+
_ => false,
1074+
};
1075+
if can_derive {
1076+
derives.push((
1077+
format!("{}", trait_pred.self_ty()),
1078+
self.tcx.def_span(adt.did),
1079+
format!("{}", trait_pred.trait_ref.print_only_trait_name()),
1080+
));
1081+
} else {
1082+
traits.push(self.tcx.def_span(trait_pred.def_id()));
1083+
}
1084+
}
10951085
derives.sort();
10961086
let derives_grouped = derives.into_iter().fold(
10971087
Vec::<(String, Span, String)>::new(),
@@ -1106,36 +1096,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11061096
acc
11071097
},
11081098
);
1109-
let mut traits: Vec<_> = unsatisfied_predicates
1110-
.iter()
1111-
.filter_map(|(pred, _)| {
1112-
let trait_pred =
1113-
if let ty::PredicateKind::Trait(trait_pred) = pred.kind().skip_binder() {
1114-
trait_pred
1115-
} else {
1116-
return None;
1117-
};
1118-
if let ty::Adt(adt_def, _) = trait_pred.trait_ref.self_ty().kind() {
1119-
if !adt_def.did.is_local() {
1120-
return None;
1121-
}
1122-
} else {
1123-
return None;
1124-
};
1125-
1126-
let did = trait_pred.def_id();
1127-
let diagnostic_items = self.tcx.diagnostic_items(did.krate);
1128-
1129-
if !derivables
1130-
.iter()
1131-
.any(|trait_derivable| diagnostic_items.get(trait_derivable) == Some(&did))
1132-
{
1133-
Some(self.tcx.def_span(did))
1134-
} else {
1135-
None
1136-
}
1137-
})
1138-
.collect();
11391099
traits.sort();
11401100
traits.dedup();
11411101

0 commit comments

Comments
 (0)