Skip to content

Commit 5abee43

Browse files
committed
other uses of get_diagnostic_name
1 parent 804c9ac commit 5abee43

File tree

8 files changed

+28
-28
lines changed

8 files changed

+28
-28
lines changed

clippy_lints/src/entry.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,9 @@ struct InsertExpr<'tcx> {
304304
fn try_parse_insert<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<InsertExpr<'tcx>> {
305305
if let ExprKind::MethodCall(_, map, [key, value], _) = expr.kind {
306306
let id = cx.typeck_results().type_dependent_def_id(expr.hir_id)?;
307-
if cx.tcx.is_diagnostic_item(sym::btreemap_insert, id) || cx.tcx.is_diagnostic_item(sym::hashmap_insert, id) {
307+
if let Some(insert) = cx.tcx.get_diagnostic_name(id)
308+
&& matches!(insert, sym::btreemap_insert | sym::hashmap_insert)
309+
{
308310
Some(InsertExpr { map, key, value })
309311
} else {
310312
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_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)