Rollup merge of #90985 - camsteffen:diag-name-usage, r=jackh726

Use `get_diagnostic_name` more
This commit is contained in:
Matthias Krüger 2021-12-01 10:50:18 +01:00 committed by GitHub
commit a4f46742c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 73 additions and 74 deletions

View file

@ -738,15 +738,13 @@ impl BorrowedContentSource<'tcx> {
BorrowedContentSource::DerefRawPointer => "a raw pointer".to_string(), BorrowedContentSource::DerefRawPointer => "a raw pointer".to_string(),
BorrowedContentSource::DerefSharedRef => "a shared reference".to_string(), BorrowedContentSource::DerefSharedRef => "a shared reference".to_string(),
BorrowedContentSource::DerefMutableRef => "a mutable reference".to_string(), BorrowedContentSource::DerefMutableRef => "a mutable reference".to_string(),
BorrowedContentSource::OverloadedDeref(ty) => match ty.kind() { BorrowedContentSource::OverloadedDeref(ty) => ty
ty::Adt(def, _) if tcx.is_diagnostic_item(sym::Rc, def.did) => { .ty_adt_def()
"an `Rc`".to_string() .and_then(|adt| match tcx.get_diagnostic_name(adt.did)? {
} name @ (sym::Rc | sym::Arc) => Some(format!("an `{}`", name)),
ty::Adt(def, _) if tcx.is_diagnostic_item(sym::Arc, def.did) => { _ => None,
"an `Arc`".to_string() })
} .unwrap_or_else(|| format!("dereference of `{}`", ty)),
_ => format!("dereference of `{}`", ty),
},
BorrowedContentSource::OverloadedIndex(ty) => format!("index of `{}`", ty), BorrowedContentSource::OverloadedIndex(ty) => format!("index of `{}`", ty),
} }
} }
@ -770,15 +768,13 @@ impl BorrowedContentSource<'tcx> {
BorrowedContentSource::DerefMutableRef => { BorrowedContentSource::DerefMutableRef => {
bug!("describe_for_immutable_place: DerefMutableRef isn't immutable") bug!("describe_for_immutable_place: DerefMutableRef isn't immutable")
} }
BorrowedContentSource::OverloadedDeref(ty) => match ty.kind() { BorrowedContentSource::OverloadedDeref(ty) => ty
ty::Adt(def, _) if tcx.is_diagnostic_item(sym::Rc, def.did) => { .ty_adt_def()
"an `Rc`".to_string() .and_then(|adt| match tcx.get_diagnostic_name(adt.did)? {
} name @ (sym::Rc | sym::Arc) => Some(format!("an `{}`", name)),
ty::Adt(def, _) if tcx.is_diagnostic_item(sym::Arc, def.did) => { _ => None,
"an `Arc`".to_string() })
} .unwrap_or_else(|| format!("dereference of `{}`", ty)),
_ => format!("a dereference of `{}`", ty),
},
BorrowedContentSource::OverloadedIndex(ty) => format!("an index of `{}`", ty), BorrowedContentSource::OverloadedIndex(ty) => format!("an index of `{}`", ty),
} }
} }
@ -960,8 +956,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
_ => None, _ => None,
}); });
let is_option_or_result = parent_self_ty.map_or(false, |def_id| { let is_option_or_result = parent_self_ty.map_or(false, |def_id| {
tcx.is_diagnostic_item(sym::Option, def_id) matches!(tcx.get_diagnostic_name(def_id), Some(sym::Option | sym::Result))
|| tcx.is_diagnostic_item(sym::Result, def_id)
}); });
FnSelfUseKind::Normal { self_arg, implicit_into_iter, is_option_or_result } FnSelfUseKind::Normal { self_arg, implicit_into_iter, is_option_or_result }
}); });

View file

@ -91,16 +91,14 @@ fn enforce_mem_variant_count(cx: &LateContext<'_>, func_expr: &hir::Expr<'_>, sp
impl<'tcx> LateLintPass<'tcx> for EnumIntrinsicsNonEnums { impl<'tcx> LateLintPass<'tcx> for EnumIntrinsicsNonEnums {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) { fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
if let hir::ExprKind::Call(ref func, ref args) = expr.kind { let hir::ExprKind::Call(func, args) = &expr.kind else { return };
if let hir::ExprKind::Path(ref qpath) = func.kind { let hir::ExprKind::Path(qpath) = &func.kind else { return };
if let Some(def_id) = cx.qpath_res(qpath, func.hir_id).opt_def_id() { let Some(def_id) = cx.qpath_res(qpath, func.hir_id).opt_def_id() else { return };
if cx.tcx.is_diagnostic_item(sym::mem_discriminant, def_id) { let Some(name) = cx.tcx.get_diagnostic_name(def_id) else { return };
enforce_mem_discriminant(cx, func, expr.span, args[0].span); match name {
} else if cx.tcx.is_diagnostic_item(sym::mem_variant_count, def_id) { sym::mem_discriminant => enforce_mem_discriminant(cx, func, expr.span, args[0].span),
enforce_mem_variant_count(cx, func, expr.span); sym::mem_variant_count => enforce_mem_variant_count(cx, func, expr.span),
} _ => {}
}
}
} }
} }
} }

View file

@ -33,6 +33,7 @@
#![cfg_attr(bootstrap, feature(format_args_capture))] #![cfg_attr(bootstrap, feature(format_args_capture))]
#![feature(iter_order_by)] #![feature(iter_order_by)]
#![feature(iter_zip)] #![feature(iter_zip)]
#![feature(let_else)]
#![feature(never_type)] #![feature(never_type)]
#![feature(nll)] #![feature(nll)]
#![feature(control_flow_enum)] #![feature(control_flow_enum)]

View file

@ -309,14 +309,21 @@ fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span,
// Unwrap more levels of macro expansion, as panic_2015!() // Unwrap more levels of macro expansion, as panic_2015!()
// was likely expanded from panic!() and possibly from // was likely expanded from panic!() and possibly from
// [debug_]assert!(). // [debug_]assert!().
for &i in loop {
&[sym::std_panic_macro, sym::core_panic_macro, sym::assert_macro, sym::debug_assert_macro]
{
let parent = expn.call_site.ctxt().outer_expn_data(); let parent = expn.call_site.ctxt().outer_expn_data();
if parent.macro_def_id.map_or(false, |id| cx.tcx.is_diagnostic_item(i, id)) { let Some(id) = parent.macro_def_id else { break };
expn = parent; let Some(name) = cx.tcx.get_diagnostic_name(id) else { break };
panic_macro = i; if !matches!(
name,
sym::core_panic_macro
| sym::std_panic_macro
| sym::assert_macro
| sym::debug_assert_macro
) {
break;
} }
expn = parent;
panic_macro = name;
} }
let macro_symbol = let macro_symbol =

View file

@ -75,8 +75,13 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
_ => return, _ => return,
}; };
// (Re)check that it implements the noop diagnostic. // (Re)check that it implements the noop diagnostic.
for s in [sym::noop_method_clone, sym::noop_method_deref, sym::noop_method_borrow].iter() { let Some(name) = cx.tcx.get_diagnostic_name(i.def_id()) else { return };
if cx.tcx.is_diagnostic_item(*s, i.def_id()) { if !matches!(
name,
sym::noop_method_borrow | sym::noop_method_clone | sym::noop_method_deref
) {
return;
}
let method = &call.ident.name; let method = &call.ident.name;
let receiver = &elements[0]; let receiver = &elements[0];
let receiver_ty = cx.typeck_results().expr_ty(receiver); let receiver_ty = cx.typeck_results().expr_ty(receiver);
@ -97,16 +102,9 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
let span = expr_span.with_lo(receiver.span.hi()); let span = expr_span.with_lo(receiver.span.hi());
cx.struct_span_lint(NOOP_METHOD_CALL, span, |lint| { cx.struct_span_lint(NOOP_METHOD_CALL, span, |lint| {
let method = &call.ident.name; let method = &call.ident.name;
let message = format!( let message =
"call to `.{}()` on a reference in this situation does nothing", format!("call to `.{}()` on a reference in this situation does nothing", &method,);
&method, lint.build(&message).span_label(span, "unnecessary method call").note(&note).emit()
);
lint.build(&message)
.span_label(span, "unnecessary method call")
.note(&note)
.emit()
}); });
} }
}
}
} }

View file

@ -539,11 +539,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
// is otherwise overwhelming and unhelpful (see #85844 for an // is otherwise overwhelming and unhelpful (see #85844 for an
// example). // example).
let trait_is_debug =
self.tcx.is_diagnostic_item(sym::Debug, trait_ref.def_id());
let trait_is_display =
self.tcx.is_diagnostic_item(sym::Display, trait_ref.def_id());
let in_std_macro = let in_std_macro =
match obligation.cause.span.ctxt().outer_expn_data().macro_def_id { match obligation.cause.span.ctxt().outer_expn_data().macro_def_id {
Some(macro_def_id) => { Some(macro_def_id) => {
@ -553,7 +548,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
None => false, None => false,
}; };
if in_std_macro && (trait_is_debug || trait_is_display) { if in_std_macro
&& matches!(
self.tcx.get_diagnostic_name(trait_ref.def_id()),
Some(sym::Debug | sym::Display)
)
{
err.emit(); err.emit();
return; return;
} }