Skip to content

Commit dec4053

Browse files
committed
Use get_diagnostic_name more
1 parent d914f17 commit dec4053

File tree

6 files changed

+73
-74
lines changed

6 files changed

+73
-74
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

+15-20
Original file line numberDiff line numberDiff line change
@@ -742,15 +742,13 @@ impl BorrowedContentSource<'tcx> {
742742
BorrowedContentSource::DerefRawPointer => "a raw pointer".to_string(),
743743
BorrowedContentSource::DerefSharedRef => "a shared reference".to_string(),
744744
BorrowedContentSource::DerefMutableRef => "a mutable reference".to_string(),
745-
BorrowedContentSource::OverloadedDeref(ty) => match ty.kind() {
746-
ty::Adt(def, _) if tcx.is_diagnostic_item(sym::Rc, def.did) => {
747-
"an `Rc`".to_string()
748-
}
749-
ty::Adt(def, _) if tcx.is_diagnostic_item(sym::Arc, def.did) => {
750-
"an `Arc`".to_string()
751-
}
752-
_ => format!("dereference of `{}`", ty),
753-
},
745+
BorrowedContentSource::OverloadedDeref(ty) => ty
746+
.ty_adt_def()
747+
.and_then(|adt| match tcx.get_diagnostic_name(adt.did)? {
748+
name @ (sym::Rc | sym::Arc) => Some(format!("an `{}`", name)),
749+
_ => None,
750+
})
751+
.unwrap_or_else(|| format!("dereference of `{}`", ty)),
754752
BorrowedContentSource::OverloadedIndex(ty) => format!("index of `{}`", ty),
755753
}
756754
}
@@ -774,15 +772,13 @@ impl BorrowedContentSource<'tcx> {
774772
BorrowedContentSource::DerefMutableRef => {
775773
bug!("describe_for_immutable_place: DerefMutableRef isn't immutable")
776774
}
777-
BorrowedContentSource::OverloadedDeref(ty) => match ty.kind() {
778-
ty::Adt(def, _) if tcx.is_diagnostic_item(sym::Rc, def.did) => {
779-
"an `Rc`".to_string()
780-
}
781-
ty::Adt(def, _) if tcx.is_diagnostic_item(sym::Arc, def.did) => {
782-
"an `Arc`".to_string()
783-
}
784-
_ => format!("a dereference of `{}`", ty),
785-
},
775+
BorrowedContentSource::OverloadedDeref(ty) => ty
776+
.ty_adt_def()
777+
.and_then(|adt| match tcx.get_diagnostic_name(adt.did)? {
778+
name @ (sym::Rc | sym::Arc) => Some(format!("an `{}`", name)),
779+
_ => None,
780+
})
781+
.unwrap_or_else(|| format!("dereference of `{}`", ty)),
786782
BorrowedContentSource::OverloadedIndex(ty) => format!("an index of `{}`", ty),
787783
}
788784
}
@@ -966,8 +962,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
966962
_ => None,
967963
});
968964
let is_option_or_result = parent_self_ty.map_or(false, |def_id| {
969-
tcx.is_diagnostic_item(sym::Option, def_id)
970-
|| tcx.is_diagnostic_item(sym::Result, def_id)
965+
matches!(tcx.get_diagnostic_name(def_id), Some(sym::Option | sym::Result))
971966
});
972967
FnSelfUseKind::Normal { self_arg, implicit_into_iter, is_option_or_result }
973968
});

compiler/rustc_lint/src/enum_intrinsics_non_enums.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,14 @@ fn enforce_mem_variant_count(cx: &LateContext<'_>, func_expr: &hir::Expr<'_>, sp
9191

9292
impl<'tcx> LateLintPass<'tcx> for EnumIntrinsicsNonEnums {
9393
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
94-
if let hir::ExprKind::Call(ref func, ref args) = expr.kind {
95-
if let hir::ExprKind::Path(ref qpath) = func.kind {
96-
if let Some(def_id) = cx.qpath_res(qpath, func.hir_id).opt_def_id() {
97-
if cx.tcx.is_diagnostic_item(sym::mem_discriminant, def_id) {
98-
enforce_mem_discriminant(cx, func, expr.span, args[0].span);
99-
} else if cx.tcx.is_diagnostic_item(sym::mem_variant_count, def_id) {
100-
enforce_mem_variant_count(cx, func, expr.span);
101-
}
102-
}
103-
}
94+
let hir::ExprKind::Call(func, args) = &expr.kind else { return };
95+
let hir::ExprKind::Path(qpath) = &func.kind else { return };
96+
let Some(def_id) = cx.qpath_res(qpath, func.hir_id).opt_def_id() else { return };
97+
let Some(name) = cx.tcx.get_diagnostic_name(def_id) else { return };
98+
match name {
99+
sym::mem_discriminant => enforce_mem_discriminant(cx, func, expr.span, args[0].span),
100+
sym::mem_variant_count => enforce_mem_variant_count(cx, func, expr.span),
101+
_ => {}
104102
}
105103
}
106104
}

compiler/rustc_lint/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#![cfg_attr(bootstrap, feature(format_args_capture))]
3434
#![feature(iter_order_by)]
3535
#![feature(iter_zip)]
36+
#![feature(let_else)]
3637
#![feature(never_type)]
3738
#![feature(nll)]
3839
#![feature(control_flow_enum)]

compiler/rustc_lint/src/non_fmt_panic.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,21 @@ fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span,
309309
// Unwrap more levels of macro expansion, as panic_2015!()
310310
// was likely expanded from panic!() and possibly from
311311
// [debug_]assert!().
312-
for &i in
313-
&[sym::std_panic_macro, sym::core_panic_macro, sym::assert_macro, sym::debug_assert_macro]
314-
{
312+
loop {
315313
let parent = expn.call_site.ctxt().outer_expn_data();
316-
if parent.macro_def_id.map_or(false, |id| cx.tcx.is_diagnostic_item(i, id)) {
317-
expn = parent;
318-
panic_macro = i;
314+
let Some(id) = parent.macro_def_id else { break };
315+
let Some(name) = cx.tcx.get_diagnostic_name(id) else { break };
316+
if !matches!(
317+
name,
318+
sym::core_panic_macro
319+
| sym::std_panic_macro
320+
| sym::assert_macro
321+
| sym::debug_assert_macro
322+
) {
323+
break;
319324
}
325+
expn = parent;
326+
panic_macro = name;
320327
}
321328

322329
let macro_symbol =

compiler/rustc_lint/src/noop_method_call.rs

+30-32
Original file line numberDiff line numberDiff line change
@@ -75,38 +75,36 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
7575
_ => return,
7676
};
7777
// (Re)check that it implements the noop diagnostic.
78-
for s in [sym::noop_method_clone, sym::noop_method_deref, sym::noop_method_borrow].iter() {
79-
if cx.tcx.is_diagnostic_item(*s, i.def_id()) {
80-
let method = &call.ident.name;
81-
let receiver = &elements[0];
82-
let receiver_ty = cx.typeck_results().expr_ty(receiver);
83-
let expr_ty = cx.typeck_results().expr_ty_adjusted(expr);
84-
if receiver_ty != expr_ty {
85-
// This lint will only trigger if the receiver type and resulting expression \
86-
// type are the same, implying that the method call is unnecessary.
87-
return;
88-
}
89-
let expr_span = expr.span;
90-
let note = format!(
91-
"the type `{:?}` which `{}` is being called on is the same as \
92-
the type returned from `{}`, so the method call does not do \
93-
anything and can be removed",
94-
receiver_ty, method, method,
95-
);
96-
97-
let span = expr_span.with_lo(receiver.span.hi());
98-
cx.struct_span_lint(NOOP_METHOD_CALL, span, |lint| {
99-
let method = &call.ident.name;
100-
let message = format!(
101-
"call to `.{}()` on a reference in this situation does nothing",
102-
&method,
103-
);
104-
lint.build(&message)
105-
.span_label(span, "unnecessary method call")
106-
.note(&note)
107-
.emit()
108-
});
109-
}
78+
let Some(name) = cx.tcx.get_diagnostic_name(i.def_id()) else { return };
79+
if !matches!(
80+
name,
81+
sym::noop_method_borrow | sym::noop_method_clone | sym::noop_method_deref
82+
) {
83+
return;
11084
}
85+
let method = &call.ident.name;
86+
let receiver = &elements[0];
87+
let receiver_ty = cx.typeck_results().expr_ty(receiver);
88+
let expr_ty = cx.typeck_results().expr_ty_adjusted(expr);
89+
if receiver_ty != expr_ty {
90+
// This lint will only trigger if the receiver type and resulting expression \
91+
// type are the same, implying that the method call is unnecessary.
92+
return;
93+
}
94+
let expr_span = expr.span;
95+
let note = format!(
96+
"the type `{:?}` which `{}` is being called on is the same as \
97+
the type returned from `{}`, so the method call does not do \
98+
anything and can be removed",
99+
receiver_ty, method, method,
100+
);
101+
102+
let span = expr_span.with_lo(receiver.span.hi());
103+
cx.struct_span_lint(NOOP_METHOD_CALL, span, |lint| {
104+
let method = &call.ident.name;
105+
let message =
106+
format!("call to `.{}()` on a reference in this situation does nothing", &method,);
107+
lint.build(&message).span_label(span, "unnecessary method call").note(&note).emit()
108+
});
111109
}
112110
}

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -539,11 +539,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
539539
// is otherwise overwhelming and unhelpful (see #85844 for an
540540
// example).
541541

542-
let trait_is_debug =
543-
self.tcx.is_diagnostic_item(sym::Debug, trait_ref.def_id());
544-
let trait_is_display =
545-
self.tcx.is_diagnostic_item(sym::Display, trait_ref.def_id());
546-
547542
let in_std_macro =
548543
match obligation.cause.span.ctxt().outer_expn_data().macro_def_id {
549544
Some(macro_def_id) => {
@@ -553,7 +548,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
553548
None => false,
554549
};
555550

556-
if in_std_macro && (trait_is_debug || trait_is_display) {
551+
if in_std_macro
552+
&& matches!(
553+
self.tcx.get_diagnostic_name(trait_ref.def_id()),
554+
Some(sym::Debug | sym::Display)
555+
)
556+
{
557557
err.emit();
558558
return;
559559
}

0 commit comments

Comments
 (0)