Skip to content

Commit f35c203

Browse files
use get_diagnostic_item in even more places (#15528)
continuation of rust-lang/rust-clippy#15519, see there for the commit structure AFAICT this finally resolves rust-lang/rust-clippy#7784 changelog: none
2 parents e24bd58 + 3c3dc6d commit f35c203

File tree

10 files changed

+34
-36
lines changed

10 files changed

+34
-36
lines changed

clippy_lints/src/entry.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,9 @@ struct InsertExpr<'tcx> {
314314
fn try_parse_insert<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<InsertExpr<'tcx>> {
315315
if let ExprKind::MethodCall(_, map, [key, value], _) = expr.kind {
316316
let id = cx.typeck_results().type_dependent_def_id(expr.hir_id)?;
317-
if cx.tcx.is_diagnostic_item(sym::btreemap_insert, id) || cx.tcx.is_diagnostic_item(sym::hashmap_insert, id) {
317+
if let Some(insert) = cx.tcx.get_diagnostic_name(id)
318+
&& matches!(insert, sym::btreemap_insert | sym::hashmap_insert)
319+
{
318320
Some(InsertExpr { map, key, value })
319321
} else {
320322
None

clippy_lints/src/mem_replace.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
215215
&& let ExprKind::Path(ref repl_func_qpath) = repl_func.kind
216216
&& let Some(repl_def_id) = cx.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id()
217217
{
218-
if cx.tcx.is_diagnostic_item(sym::mem_uninitialized, repl_def_id) {
218+
let repl_name = cx.tcx.get_diagnostic_name(repl_def_id);
219+
if repl_name == Some(sym::mem_uninitialized) {
219220
let Some(top_crate) = std_or_core(cx) else { return };
220221
let mut applicability = Applicability::MachineApplicable;
221222
span_lint_and_sugg(
@@ -230,9 +231,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
230231
),
231232
applicability,
232233
);
233-
} else if cx.tcx.is_diagnostic_item(sym::mem_zeroed, repl_def_id)
234-
&& !cx.typeck_results().expr_ty(src).is_primitive()
235-
{
234+
} else if repl_name == Some(sym::mem_zeroed) && !cx.typeck_results().expr_ty(src).is_primitive() {
236235
span_lint_and_help(
237236
cx,
238237
MEM_REPLACE_WITH_UNINIT,

clippy_lints/src/methods/filter_map.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,16 @@ impl<'tcx> OffendingFilterExpr<'tcx> {
233233
// the latter only calls `effect` once
234234
let side_effect_expr_span = receiver.can_have_side_effects().then_some(receiver.span);
235235

236-
if cx.tcx.is_diagnostic_item(sym::Option, recv_ty.did()) && path.ident.name == sym::is_some {
237-
Some(Self::IsSome {
236+
match (cx.tcx.get_diagnostic_name(recv_ty.did()), path.ident.name) {
237+
(Some(sym::Option), sym::is_some) => Some(Self::IsSome {
238238
receiver,
239239
side_effect_expr_span,
240-
})
241-
} else if cx.tcx.is_diagnostic_item(sym::Result, recv_ty.did()) && path.ident.name == sym::is_ok {
242-
Some(Self::IsOk {
240+
}),
241+
(Some(sym::Result), sym::is_ok) => Some(Self::IsOk {
243242
receiver,
244243
side_effect_expr_span,
245-
})
246-
} else {
247-
None
244+
}),
245+
_ => None,
248246
}
249247
} else if matching_root_macro_call(cx, expr.span, sym::matches_macro).is_some()
250248
// we know for a fact that the wildcard pattern is the second arm

clippy_lints/src/methods/map_flatten.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ fn try_get_caller_ty_name_and_method_name(
5050
}
5151
} else {
5252
if let ty::Adt(adt, _) = cx.typeck_results().expr_ty(caller_expr).kind() {
53-
if cx.tcx.is_diagnostic_item(sym::Option, adt.did()) {
54-
return Some(("Option", "and_then"));
55-
} else if cx.tcx.is_diagnostic_item(sym::Result, adt.did()) {
56-
return Some(("Result", "and_then"));
53+
match cx.tcx.get_diagnostic_name(adt.did()) {
54+
Some(sym::Option) => return Some(("Option", "and_then")),
55+
Some(sym::Result) => return Some(("Result", "and_then")),
56+
_ => {},
5757
}
5858
}
5959
None

clippy_lints/src/methods/single_char_add_str.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use rustc_span::sym;
55

66
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, receiver: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
77
if let Some(fn_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) {
8-
if cx.tcx.is_diagnostic_item(sym::string_push_str, fn_def_id) {
9-
single_char_push_string::check(cx, expr, receiver, args);
10-
} else if cx.tcx.is_diagnostic_item(sym::string_insert_str, fn_def_id) {
11-
single_char_insert_string::check(cx, expr, receiver, args);
8+
match cx.tcx.get_diagnostic_name(fn_def_id) {
9+
Some(sym::string_push_str) => single_char_push_string::check(cx, expr, receiver, args),
10+
Some(sym::string_insert_str) => single_char_insert_string::check(cx, expr, receiver, args),
11+
_ => {},
1212
}
1313
}
1414
}

clippy_lints/src/operators/float_equality_without_abs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ pub(crate) fn check<'tcx>(
3737
// right hand side matches either f32::EPSILON or f64::EPSILON
3838
&& let ExprKind::Path(ref epsilon_path) = rhs.kind
3939
&& let Res::Def(DefKind::AssocConst, def_id) = cx.qpath_res(epsilon_path, rhs.hir_id)
40-
&& ([sym::f32_epsilon, sym::f64_epsilon].into_iter().any(|sym| cx.tcx.is_diagnostic_item(sym, def_id)))
40+
&& let Some(epsilon) = cx.tcx.get_diagnostic_name(def_id)
41+
&& matches!(epsilon, sym::f32_epsilon| sym::f64_epsilon)
4142

4243
// values of the subtractions on the left hand side are of the type float
4344
&& let t_val_l = cx.typeck_results().expr_ty(val_l)

clippy_lints/src/unused_io_amount.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ impl<'tcx> LateLintPass<'tcx> for UnusedIoAmount {
8989
{
9090
// We don't want to lint inside io::Read or io::Write implementations, as the author has more
9191
// information about their trait implementation than our lint, see https://github.com/rust-lang/rust-clippy/issues/4836
92-
if cx.tcx.is_diagnostic_item(sym::IoRead, trait_id) || cx.tcx.is_diagnostic_item(sym::IoWrite, trait_id) {
92+
if let Some(trait_name) = cx.tcx.get_diagnostic_name(trait_id)
93+
&& matches!(trait_name, sym::IoRead | sym::IoWrite)
94+
{
9395
return;
9496
}
9597

clippy_lints/src/useless_conversion.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,13 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
386386

387387
ExprKind::Call(path, [arg]) => {
388388
if let ExprKind::Path(ref qpath) = path.kind
389-
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
390389
&& !is_ty_alias(qpath)
390+
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
391+
&& let Some(name) = cx.tcx.get_diagnostic_name(def_id)
391392
{
392393
let a = cx.typeck_results().expr_ty(e);
393394
let b = cx.typeck_results().expr_ty(arg);
394-
if cx.tcx.is_diagnostic_item(sym::try_from_fn, def_id)
395+
if name == sym::try_from_fn
395396
&& is_type_diagnostic_item(cx, a, sym::Result)
396397
&& let ty::Adt(_, args) = a.kind()
397398
&& let Some(a_type) = args.types().next()
@@ -406,9 +407,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
406407
None,
407408
hint,
408409
);
409-
}
410-
411-
if cx.tcx.is_diagnostic_item(sym::from_fn, def_id) && same_type_and_consts(a, b) {
410+
} else if name == sym::from_fn && same_type_and_consts(a, b) {
412411
let mut app = Applicability::MachineApplicable;
413412
let sugg = Sugg::hir_with_context(cx, arg, e.span.ctxt(), "<expr>", &mut app).maybe_paren();
414413
let sugg_msg = format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));

clippy_utils/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,8 @@ fn is_default_equivalent_ctor(cx: &LateContext<'_>, def_id: DefId, path: &QPath<
641641
&& let Some(impl_did) = cx.tcx.impl_of_assoc(def_id)
642642
&& let Some(adt) = cx.tcx.type_of(impl_did).instantiate_identity().ty_adt_def()
643643
{
644-
return std_types_symbols.iter().any(|&symbol| {
645-
cx.tcx.is_diagnostic_item(symbol, adt.did()) || Some(adt.did()) == cx.tcx.lang_items().string()
646-
});
644+
return Some(adt.did()) == cx.tcx.lang_items().string()
645+
|| (cx.tcx.get_diagnostic_name(adt.did())).is_some_and(|adt_name| std_types_symbols.contains(&adt_name));
647646
}
648647
false
649648
}

clippy_utils/src/usage.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,9 @@ impl<'tcx> Visitor<'tcx> for BindingUsageFinder<'_, 'tcx> {
144144

145145
/// Checks if the given expression is a macro call to `todo!()` or `unimplemented!()`.
146146
pub fn is_todo_unimplemented_macro(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
147-
root_macro_call_first_node(cx, expr).is_some_and(|macro_call| {
148-
[sym::todo_macro, sym::unimplemented_macro]
149-
.iter()
150-
.any(|&sym| cx.tcx.is_diagnostic_item(sym, macro_call.def_id))
151-
})
147+
root_macro_call_first_node(cx, expr)
148+
.and_then(|macro_call| cx.tcx.get_diagnostic_name(macro_call.def_id))
149+
.is_some_and(|macro_name| matches!(macro_name, sym::todo_macro | sym::unimplemented_macro))
152150
}
153151

154152
/// Checks if the given expression is a stub, i.e., a `todo!()` or `unimplemented!()` expression,

0 commit comments

Comments
 (0)