1
Fork 0

Use get_diagnostic_name

This commit is contained in:
Cameron Steffen 2021-10-04 16:11:22 -05:00
parent 33b9b95305
commit b6cab80c18
6 changed files with 73 additions and 113 deletions

View file

@ -2478,14 +2478,11 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
// Find calls to `mem::{uninitialized,zeroed}` methods. // Find calls to `mem::{uninitialized,zeroed}` methods.
if let hir::ExprKind::Path(ref qpath) = path_expr.kind { if let hir::ExprKind::Path(ref qpath) = path_expr.kind {
let def_id = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id()?; let def_id = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id()?;
match cx.tcx.get_diagnostic_name(def_id) {
if cx.tcx.is_diagnostic_item(sym::mem_zeroed, def_id) { Some(sym::mem_zeroed) => return Some(InitKind::Zeroed),
return Some(InitKind::Zeroed); Some(sym::mem_uninitialized) => return Some(InitKind::Uninit),
} else if cx.tcx.is_diagnostic_item(sym::mem_uninitialized, def_id) { Some(sym::transmute) if is_zero(&args[0]) => return Some(InitKind::Zeroed),
return Some(InitKind::Uninit); _ => {}
} else if cx.tcx.is_diagnostic_item(sym::transmute, def_id) && is_zero(&args[0])
{
return Some(InitKind::Zeroed);
} }
} }
} else if let hir::ExprKind::MethodCall(_, _, ref args, _) = expr.kind { } else if let hir::ExprKind::MethodCall(_, _, ref args, _) = expr.kind {
@ -2497,11 +2494,10 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
if let hir::ExprKind::Call(ref path_expr, _) = args[0].kind { if let hir::ExprKind::Call(ref path_expr, _) = args[0].kind {
if let hir::ExprKind::Path(ref qpath) = path_expr.kind { if let hir::ExprKind::Path(ref qpath) = path_expr.kind {
let def_id = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id()?; let def_id = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id()?;
match cx.tcx.get_diagnostic_name(def_id) {
if cx.tcx.is_diagnostic_item(sym::maybe_uninit_zeroed, def_id) { Some(sym::maybe_uninit_zeroed) => return Some(InitKind::Zeroed),
return Some(InitKind::Zeroed); Some(sym::maybe_uninit_uninit) => return Some(InitKind::Uninit),
} else if cx.tcx.is_diagnostic_item(sym::maybe_uninit_uninit, def_id) { _ => {}
return Some(InitKind::Uninit);
} }
} }
} }
@ -3091,8 +3087,10 @@ impl<'tcx> LateLintPass<'tcx> for DerefNullPtr {
rustc_hir::ExprKind::Call(ref path, _) => { rustc_hir::ExprKind::Call(ref path, _) => {
if let rustc_hir::ExprKind::Path(ref qpath) = path.kind { if let rustc_hir::ExprKind::Path(ref qpath) = path.kind {
if let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id() { if let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id() {
return cx.tcx.is_diagnostic_item(sym::ptr_null, def_id) return matches!(
|| cx.tcx.is_diagnostic_item(sym::ptr_null_mut, def_id); cx.tcx.get_diagnostic_name(def_id),
Some(sym::ptr_null | sym::ptr_null_mut)
);
} }
} }
} }

View file

@ -33,12 +33,10 @@ impl LateLintPass<'_> for DefaultHashTypes {
// don't lint imports, only actual usages // don't lint imports, only actual usages
return; return;
} }
let replace = if cx.tcx.is_diagnostic_item(sym::HashMap, def_id) { let replace = match cx.tcx.get_diagnostic_name(def_id) {
"FxHashMap" Some(sym::HashMap) => "FxHashMap",
} else if cx.tcx.is_diagnostic_item(sym::HashSet, def_id) { Some(sym::HashSet) => "FxHashSet",
"FxHashSet" _ => return,
} else {
return;
}; };
cx.struct_span_lint(DEFAULT_HASH_TYPES, path.span, |lint| { cx.struct_span_lint(DEFAULT_HASH_TYPES, path.span, |lint| {
let msg = format!( let msg = format!(
@ -174,26 +172,29 @@ fn is_ty_or_ty_ctxt(cx: &LateContext<'_>, ty: &Ty<'_>) -> Option<String> {
if let TyKind::Path(qpath) = &ty.kind { if let TyKind::Path(qpath) = &ty.kind {
if let QPath::Resolved(_, path) = qpath { if let QPath::Resolved(_, path) = qpath {
match path.res { match path.res {
Res::Def(_, did) => { Res::Def(_, def_id) => {
if cx.tcx.is_diagnostic_item(sym::Ty, did) { if let Some(name @ (sym::Ty | sym::TyCtxt)) = cx.tcx.get_diagnostic_name(def_id)
return Some(format!("Ty{}", gen_args(path.segments.last().unwrap()))); {
} else if cx.tcx.is_diagnostic_item(sym::TyCtxt, did) { return Some(format!(
return Some(format!("TyCtxt{}", gen_args(path.segments.last().unwrap()))); "{}{}",
name,
gen_args(path.segments.last().unwrap())
));
} }
} }
// Only lint on `&Ty` and `&TyCtxt` if it is used outside of a trait. // Only lint on `&Ty` and `&TyCtxt` if it is used outside of a trait.
Res::SelfTy(None, Some((did, _))) => { Res::SelfTy(None, Some((did, _))) => {
if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() { if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() {
if cx.tcx.is_diagnostic_item(sym::Ty, adt.did) { if let Some(name @ (sym::Ty | sym::TyCtxt)) =
cx.tcx.get_diagnostic_name(adt.did)
{
// NOTE: This path is currently unreachable as `Ty<'tcx>` is // NOTE: This path is currently unreachable as `Ty<'tcx>` is
// defined as a type alias meaning that `impl<'tcx> Ty<'tcx>` // defined as a type alias meaning that `impl<'tcx> Ty<'tcx>`
// is not actually allowed. // is not actually allowed.
// //
// I(@lcnr) still kept this branch in so we don't miss this // I(@lcnr) still kept this branch in so we don't miss this
// if we ever change it in the future. // if we ever change it in the future.
return Some(format!("Ty<{}>", substs[0])); return Some(format!("{}<{}>", name, substs[0]));
} else if cx.tcx.is_diagnostic_item(sym::TyCtxt, adt.did) {
return Some(format!("TyCtxt<{}>", substs[0]));
} }
} }
} }

View file

@ -54,9 +54,10 @@ impl<'tcx> LateLintPass<'tcx> for NonPanicFmt {
|| Some(def_id) == cx.tcx.lang_items().panic_str() || Some(def_id) == cx.tcx.lang_items().panic_str()
{ {
if let Some(id) = f.span.ctxt().outer_expn_data().macro_def_id { if let Some(id) = f.span.ctxt().outer_expn_data().macro_def_id {
if cx.tcx.is_diagnostic_item(sym::std_panic_2015_macro, id) if matches!(
|| cx.tcx.is_diagnostic_item(sym::core_panic_2015_macro, id) cx.tcx.get_diagnostic_name(id),
{ Some(sym::core_panic_2015_macro | sym::std_panic_2015_macro)
) {
check_panic(cx, f, arg); check_panic(cx, f, arg);
} }
} }

View file

@ -51,9 +51,10 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
Some((DefKind::AssocFn, did)) => match cx.tcx.trait_of_item(did) { Some((DefKind::AssocFn, did)) => match cx.tcx.trait_of_item(did) {
// Check that we're dealing with a trait method for one of the traits we care about. // Check that we're dealing with a trait method for one of the traits we care about.
Some(trait_id) Some(trait_id)
if [sym::Clone, sym::Deref, sym::Borrow] if matches!(
.iter() cx.tcx.get_diagnostic_name(trait_id),
.any(|s| cx.tcx.is_diagnostic_item(*s, trait_id)) => Some(sym::Borrow | sym::Clone | sym::Deref)
) =>
{ {
(trait_id, did) (trait_id, did)
} }

View file

@ -1541,8 +1541,7 @@ impl InvalidAtomicOrdering {
if let ExprKind::Call(ref func, ref args) = expr.kind; if let ExprKind::Call(ref func, ref args) = expr.kind;
if let ExprKind::Path(ref func_qpath) = func.kind; if let ExprKind::Path(ref func_qpath) = func.kind;
if let Some(def_id) = cx.qpath_res(func_qpath, func.hir_id).opt_def_id(); if let Some(def_id) = cx.qpath_res(func_qpath, func.hir_id).opt_def_id();
if cx.tcx.is_diagnostic_item(sym::fence, def_id) || if matches!(cx.tcx.get_diagnostic_name(def_id), Some(sym::fence | sym::compiler_fence));
cx.tcx.is_diagnostic_item(sym::compiler_fence, def_id);
if let ExprKind::Path(ref ordering_qpath) = &args[0].kind; if let ExprKind::Path(ref ordering_qpath) = &args[0].kind;
if let Some(ordering_def_id) = cx.qpath_res(ordering_qpath, args[0].hir_id).opt_def_id(); if let Some(ordering_def_id) = cx.qpath_res(ordering_qpath, args[0].hir_id).opt_def_id();
if Self::matches_ordering(cx, ordering_def_id, &[sym::Relaxed]); if Self::matches_ordering(cx, ordering_def_id, &[sym::Relaxed]);

View file

@ -1047,51 +1047,41 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err: &mut DiagnosticBuilder<'_>, err: &mut DiagnosticBuilder<'_>,
unsatisfied_predicates: &Vec<(ty::Predicate<'tcx>, Option<ty::Predicate<'tcx>>)>, unsatisfied_predicates: &Vec<(ty::Predicate<'tcx>, Option<ty::Predicate<'tcx>>)>,
) { ) {
let derivables = [ let mut derives = Vec::<(String, Span, String)>::new();
sym::Eq, let mut traits = Vec::<Span>::new();
sym::PartialEq, for (pred, _) in unsatisfied_predicates {
sym::Ord, let trait_pred = match pred.kind().skip_binder() {
sym::PartialOrd, ty::PredicateKind::Trait(trait_pred) => trait_pred,
sym::Clone, _ => continue,
sym::Copy, };
sym::Hash, let adt = match trait_pred.self_ty().ty_adt_def() {
sym::Default, Some(adt) if adt.did.is_local() => adt,
sym::Debug, _ => continue,
]; };
let mut derives = unsatisfied_predicates let can_derive = match self.tcx.get_diagnostic_name(trait_pred.def_id()) {
.iter() Some(sym::Default) => !adt.is_enum(),
.filter_map(|(pred, _)| { Some(
let trait_pred = sym::Eq
if let ty::PredicateKind::Trait(trait_pred) = pred.kind().skip_binder() { | sym::PartialEq
trait_pred | sym::Ord
} else { | sym::PartialOrd
return None; | sym::Clone
}; | sym::Copy
let trait_ref = trait_pred.trait_ref; | sym::Hash
let adt_def = if let ty::Adt(adt_def, _) = trait_ref.self_ty().kind() { | sym::Debug,
adt_def ) => true,
} else { _ => false,
return None; };
}; if can_derive {
if adt_def.did.is_local() { derives.push((
let diagnostic_items = self.tcx.diagnostic_items(trait_ref.def_id.krate); format!("{}", trait_pred.self_ty()),
return derivables.iter().find_map(|trait_derivable| { self.tcx.def_span(adt.did),
let item_def_id = diagnostic_items.name_to_id.get(trait_derivable)?; format!("{}", trait_pred.trait_ref.print_only_trait_name()),
if item_def_id == &trait_pred.trait_ref.def_id ));
&& !(adt_def.is_enum() && *trait_derivable == sym::Default) } else {
{ traits.push(self.tcx.def_span(trait_pred.def_id()));
return Some(( }
format!("{}", trait_ref.self_ty()), }
self.tcx.def_span(adt_def.did),
format!("{}", trait_ref.print_only_trait_name()),
));
}
None
});
}
None
})
.collect::<Vec<(String, Span, String)>>();
derives.sort(); derives.sort();
let derives_grouped = derives.into_iter().fold( let derives_grouped = derives.into_iter().fold(
Vec::<(String, Span, String)>::new(), Vec::<(String, Span, String)>::new(),
@ -1106,36 +1096,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
acc acc
}, },
); );
let mut traits: Vec<_> = unsatisfied_predicates
.iter()
.filter_map(|(pred, _)| {
let trait_pred =
if let ty::PredicateKind::Trait(trait_pred) = pred.kind().skip_binder() {
trait_pred
} else {
return None;
};
if let ty::Adt(adt_def, _) = trait_pred.trait_ref.self_ty().kind() {
if !adt_def.did.is_local() {
return None;
}
} else {
return None;
};
let did = trait_pred.def_id();
let diagnostic_items = self.tcx.diagnostic_items(did.krate);
if !derivables
.iter()
.any(|trait_derivable| diagnostic_items.get(trait_derivable) == Some(&did))
{
Some(self.tcx.def_span(did))
} else {
None
}
})
.collect();
traits.sort(); traits.sort();
traits.dedup(); traits.dedup();