Skip to content

Commit 35fb26f

Browse files
get_diagnostic_item in a bunch more places (#15519)
For rust-lang/rust-clippy#7784 The first commit is for all the places with rather trivial changes: - replacing nested `is_diagnostic_item` `if`s with a match on `get_diagnostic_item` - storing the result of `get_diagnostic_item` in a variable and reusing it The rest of commits are for more involved changes, and for places where changing to `get_diagnostic_item` allowed further simplifications -- in the latter case, the follow-up commits are marked as `misc: ` This is roughly one evening's worth of changes, so I hope the amount won't be overwhelming^^ changelog: none
2 parents 0744c88 + c987818 commit 35fb26f

20 files changed

+145
-169
lines changed

clippy_lints/src/casts/ptr_as_ptr.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,10 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Msrv)
6262
&& let ExprKind::Path(ref qpath @ QPath::Resolved(None, path)) = func.kind
6363
&& let Some(method_defid) = path.res.opt_def_id()
6464
{
65-
if cx.tcx.is_diagnostic_item(sym::ptr_null, method_defid) {
66-
OmitFollowedCastReason::Null(qpath)
67-
} else if cx.tcx.is_diagnostic_item(sym::ptr_null_mut, method_defid) {
68-
OmitFollowedCastReason::NullMut(qpath)
69-
} else {
70-
OmitFollowedCastReason::None
65+
match cx.tcx.get_diagnostic_name(method_defid) {
66+
Some(sym::ptr_null) => OmitFollowedCastReason::Null(qpath),
67+
Some(sym::ptr_null_mut) => OmitFollowedCastReason::NullMut(qpath),
68+
_ => OmitFollowedCastReason::None,
7169
}
7270
} else {
7371
OmitFollowedCastReason::None

clippy_lints/src/entry.rs

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -264,35 +264,28 @@ fn try_parse_contains<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'_>) -> Optio
264264
_ => None,
265265
});
266266

267-
match expr.kind {
268-
ExprKind::MethodCall(
269-
_,
267+
if let ExprKind::MethodCall(_, map, [arg], _) = expr.kind
268+
&& let Expr {
269+
kind: ExprKind::AddrOf(_, _, key),
270+
span: key_span,
271+
..
272+
} = arg
273+
&& key_span.eq_ctxt(expr.span)
274+
{
275+
let id = cx.typeck_results().type_dependent_def_id(expr.hir_id)?;
276+
let expr = ContainsExpr {
277+
negated,
270278
map,
271-
[
272-
Expr {
273-
kind: ExprKind::AddrOf(_, _, key),
274-
span: key_span,
275-
..
276-
},
277-
],
278-
_,
279-
) if key_span.eq_ctxt(expr.span) => {
280-
let id = cx.typeck_results().type_dependent_def_id(expr.hir_id)?;
281-
let expr = ContainsExpr {
282-
negated,
283-
map,
284-
key,
285-
call_ctxt: expr.span.ctxt(),
286-
};
287-
if cx.tcx.is_diagnostic_item(sym::btreemap_contains_key, id) {
288-
Some((MapType::BTree, expr))
289-
} else if cx.tcx.is_diagnostic_item(sym::hashmap_contains_key, id) {
290-
Some((MapType::Hash, expr))
291-
} else {
292-
None
293-
}
294-
},
295-
_ => None,
279+
key,
280+
call_ctxt: expr.span.ctxt(),
281+
};
282+
match cx.tcx.get_diagnostic_name(id) {
283+
Some(sym::btreemap_contains_key) => Some((MapType::BTree, expr)),
284+
Some(sym::hashmap_contains_key) => Some((MapType::Hash, expr)),
285+
_ => None,
286+
}
287+
} else {
288+
None
296289
}
297290
}
298291

clippy_lints/src/large_include_file.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ impl LateLintPass<'_> for LargeIncludeFile {
6464
}
6565
&& len as u64 > self.max_file_size
6666
&& let Some(macro_call) = root_macro_call_first_node(cx, expr)
67-
&& (cx.tcx.is_diagnostic_item(sym::include_bytes_macro, macro_call.def_id)
68-
|| cx.tcx.is_diagnostic_item(sym::include_str_macro, macro_call.def_id))
67+
&& let Some(macro_name) = cx.tcx.get_diagnostic_name(macro_call.def_id)
68+
&& matches!(macro_name, sym::include_bytes_macro | sym::include_str_macro)
6969
{
7070
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
7171
span_lint_and_then(

clippy_lints/src/len_zero.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -355,24 +355,26 @@ fn parse_len_output<'tcx>(cx: &LateContext<'tcx>, sig: FnSig<'tcx>) -> Option<Le
355355
return Some(LenOutput::Integral);
356356
}
357357

358-
if let Res::Def(_, def_id) = res {
359-
if cx.tcx.is_diagnostic_item(sym::Option, def_id) && is_first_generic_integral(segment) {
360-
return Some(LenOutput::Option(def_id));
361-
} else if cx.tcx.is_diagnostic_item(sym::Result, def_id) && is_first_generic_integral(segment) {
362-
return Some(LenOutput::Result(def_id));
358+
if let Res::Def(_, def_id) = res
359+
&& let Some(res) = match cx.tcx.get_diagnostic_name(def_id) {
360+
Some(sym::Option) => Some(LenOutput::Option(def_id)),
361+
Some(sym::Result) => Some(LenOutput::Result(def_id)),
362+
_ => None,
363363
}
364+
&& is_first_generic_integral(segment)
365+
{
366+
return Some(res);
364367
}
365368

366369
return None;
367370
}
368371

369372
match *sig.output().kind() {
370373
ty::Int(_) | ty::Uint(_) => Some(LenOutput::Integral),
371-
ty::Adt(adt, subs) if cx.tcx.is_diagnostic_item(sym::Option, adt.did()) => {
372-
subs.type_at(0).is_integral().then(|| LenOutput::Option(adt.did()))
373-
},
374-
ty::Adt(adt, subs) if cx.tcx.is_diagnostic_item(sym::Result, adt.did()) => {
375-
subs.type_at(0).is_integral().then(|| LenOutput::Result(adt.did()))
374+
ty::Adt(adt, subs) if subs.type_at(0).is_integral() => match cx.tcx.get_diagnostic_name(adt.did()) {
375+
Some(sym::Option) => Some(LenOutput::Option(adt.did())),
376+
Some(sym::Result) => Some(LenOutput::Result(adt.did())),
377+
_ => None,
376378
},
377379
_ => None,
378380
}

clippy_lints/src/manual_retain.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ fn check_iter(
123123
) {
124124
if let hir::ExprKind::MethodCall(_, filter_expr, [], _) = &target_expr.kind
125125
&& let Some(copied_def_id) = cx.typeck_results().type_dependent_def_id(target_expr.hir_id)
126-
&& (cx.tcx.is_diagnostic_item(sym::iter_copied, copied_def_id)
127-
|| cx.tcx.is_diagnostic_item(sym::iter_cloned, copied_def_id))
126+
&& let Some(copied_name) = cx.tcx.get_diagnostic_name(copied_def_id)
127+
&& matches!(copied_name, sym::iter_copied | sym::iter_cloned)
128128
&& let hir::ExprKind::MethodCall(_, iter_expr, [_], _) = &filter_expr.kind
129129
&& let Some(filter_def_id) = cx.typeck_results().type_dependent_def_id(filter_expr.hir_id)
130130
&& cx.tcx.is_diagnostic_item(sym::iter_filter, filter_def_id)
@@ -243,9 +243,9 @@ fn make_sugg(
243243
}
244244

245245
fn match_acceptable_sym(cx: &LateContext<'_>, collect_def_id: DefId) -> bool {
246-
ACCEPTABLE_METHODS
247-
.iter()
248-
.any(|&method| cx.tcx.is_diagnostic_item(method, collect_def_id))
246+
cx.tcx
247+
.get_diagnostic_name(collect_def_id)
248+
.is_some_and(|collect_name| ACCEPTABLE_METHODS.contains(&collect_name))
249249
}
250250

251251
fn match_acceptable_type(cx: &LateContext<'_>, expr: &hir::Expr<'_>, msrv: Msrv) -> bool {

clippy_lints/src/manual_strip.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,10 @@ impl<'tcx> LateLintPass<'tcx> for ManualStrip {
7575
&& let ExprKind::Path(target_path) = &target_arg.kind
7676
&& let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(cond.hir_id)
7777
{
78-
let strip_kind = if cx.tcx.is_diagnostic_item(sym::str_starts_with, method_def_id) {
79-
StripKind::Prefix
80-
} else if cx.tcx.is_diagnostic_item(sym::str_ends_with, method_def_id) {
81-
StripKind::Suffix
82-
} else {
83-
return;
78+
let strip_kind = match cx.tcx.get_diagnostic_name(method_def_id) {
79+
Some(sym::str_starts_with) => StripKind::Prefix,
80+
Some(sym::str_ends_with) => StripKind::Suffix,
81+
_ => return,
8482
};
8583
let target_res = cx.qpath_res(target_path, target_arg.hir_id);
8684
if target_res == Res::Err {

clippy_lints/src/methods/iter_out_of_bounds.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,29 @@ fn get_iterator_length<'tcx>(cx: &LateContext<'tcx>, iter: &'tcx Expr<'tcx>) ->
2424
let ty::Adt(adt, substs) = cx.typeck_results().expr_ty(iter).kind() else {
2525
return None;
2626
};
27-
let did = adt.did();
2827

29-
if cx.tcx.is_diagnostic_item(sym::ArrayIntoIter, did) {
30-
// For array::IntoIter<T, const N: usize>, the length is the second generic
31-
// parameter.
32-
substs.const_at(1).try_to_target_usize(cx.tcx).map(u128::from)
33-
} else if cx.tcx.is_diagnostic_item(sym::SliceIter, did)
34-
&& let ExprKind::MethodCall(_, recv, ..) = iter.kind
35-
{
36-
if let ty::Array(_, len) = cx.typeck_results().expr_ty(recv).peel_refs().kind() {
37-
// For slice::Iter<'_, T>, the receiver might be an array literal: [1,2,3].iter().skip(..)
38-
len.try_to_target_usize(cx.tcx).map(u128::from)
39-
} else if let Some(args) = VecArgs::hir(cx, expr_or_init(cx, recv)) {
40-
match args {
41-
VecArgs::Vec(vec) => vec.len().try_into().ok(),
42-
VecArgs::Repeat(_, len) => expr_as_u128(cx, len),
28+
match cx.tcx.get_diagnostic_name(adt.did()) {
29+
Some(sym::ArrayIntoIter) => {
30+
// For array::IntoIter<T, const N: usize>, the length is the second generic
31+
// parameter.
32+
substs.const_at(1).try_to_target_usize(cx.tcx).map(u128::from)
33+
},
34+
Some(sym::SliceIter) if let ExprKind::MethodCall(_, recv, ..) = iter.kind => {
35+
if let ty::Array(_, len) = cx.typeck_results().expr_ty(recv).peel_refs().kind() {
36+
// For slice::Iter<'_, T>, the receiver might be an array literal: [1,2,3].iter().skip(..)
37+
len.try_to_target_usize(cx.tcx).map(u128::from)
38+
} else if let Some(args) = VecArgs::hir(cx, expr_or_init(cx, recv)) {
39+
match args {
40+
VecArgs::Vec(vec) => vec.len().try_into().ok(),
41+
VecArgs::Repeat(_, len) => expr_as_u128(cx, len),
42+
}
43+
} else {
44+
None
4345
}
44-
} else {
45-
None
46-
}
47-
} else if cx.tcx.is_diagnostic_item(sym::IterEmpty, did) {
48-
Some(0)
49-
} else if cx.tcx.is_diagnostic_item(sym::IterOnce, did) {
50-
Some(1)
51-
} else {
52-
None
46+
},
47+
Some(sym::IterEmpty) => Some(0),
48+
Some(sym::IterOnce) => Some(1),
49+
_ => None,
5350
}
5451
}
5552

clippy_lints/src/methods/option_as_ref_deref.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,13 @@ pub(super) fn check(
3838
];
3939

4040
let is_deref = match map_arg.kind {
41-
hir::ExprKind::Path(ref expr_qpath) => {
42-
cx.qpath_res(expr_qpath, map_arg.hir_id)
43-
.opt_def_id()
44-
.is_some_and(|fun_def_id| {
45-
cx.tcx.is_diagnostic_item(sym::deref_method, fun_def_id)
46-
|| cx.tcx.is_diagnostic_item(sym::deref_mut_method, fun_def_id)
47-
|| deref_aliases
48-
.iter()
49-
.any(|&sym| cx.tcx.is_diagnostic_item(sym, fun_def_id))
50-
})
51-
},
41+
hir::ExprKind::Path(ref expr_qpath) => cx
42+
.qpath_res(expr_qpath, map_arg.hir_id)
43+
.opt_def_id()
44+
.and_then(|fun_def_id| cx.tcx.get_diagnostic_name(fun_def_id))
45+
.is_some_and(|fun_name| {
46+
matches!(fun_name, sym::deref_method | sym::deref_mut_method) || deref_aliases.contains(&fun_name)
47+
}),
5248
hir::ExprKind::Closure(&hir::Closure { body, .. }) => {
5349
let closure_body = cx.tcx.hir_body(body);
5450
let closure_expr = peel_blocks(closure_body.value);
@@ -63,13 +59,11 @@ pub(super) fn check(
6359
.map(|x| &x.kind)
6460
.collect::<Box<[_]>>()
6561
&& let [ty::adjustment::Adjust::Deref(None), ty::adjustment::Adjust::Borrow(_)] = *adj
62+
&& let method_did = cx.typeck_results().type_dependent_def_id(closure_expr.hir_id).unwrap()
63+
&& let Some(method_name) = cx.tcx.get_diagnostic_name(method_did)
6664
{
67-
let method_did = cx.typeck_results().type_dependent_def_id(closure_expr.hir_id).unwrap();
68-
cx.tcx.is_diagnostic_item(sym::deref_method, method_did)
69-
|| cx.tcx.is_diagnostic_item(sym::deref_mut_method, method_did)
70-
|| deref_aliases
71-
.iter()
72-
.any(|&sym| cx.tcx.is_diagnostic_item(sym, method_did))
65+
matches!(method_name, sym::deref_method | sym::deref_mut_method)
66+
|| deref_aliases.contains(&method_name)
7367
} else {
7468
false
7569
}

clippy_lints/src/methods/unnecessary_min_or_max.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ pub(super) fn check<'tcx>(
2222
let typeck_results = cx.typeck_results();
2323
let ecx = ConstEvalCtxt::with_env(cx.tcx, cx.typing_env(), typeck_results);
2424
if let Some(id) = typeck_results.type_dependent_def_id(expr.hir_id)
25-
&& (cx.tcx.is_diagnostic_item(sym::cmp_ord_min, id) || cx.tcx.is_diagnostic_item(sym::cmp_ord_max, id))
25+
&& let Some(fn_name) = cx.tcx.get_diagnostic_name(id)
26+
&& matches!(fn_name, sym::cmp_ord_min | sym::cmp_ord_max)
2627
{
2728
if let Some((left, ConstantSource::Local | ConstantSource::CoreConstant)) = ecx.eval_with_source(recv)
2829
&& let Some((right, ConstantSource::Local | ConstantSource::CoreConstant)) = ecx.eval_with_source(arg)

clippy_lints/src/non_canonical_impls.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ impl LateLintPass<'_> for NonCanonicalImpls {
134134
return;
135135
}
136136

137-
if cx.tcx.is_diagnostic_item(sym::Clone, trait_impl.def_id)
137+
let trait_name = cx.tcx.get_diagnostic_name(trait_impl.def_id);
138+
if trait_name == Some(sym::Clone)
138139
&& let Some(copy_def_id) = cx.tcx.get_diagnostic_item(sym::Copy)
139140
&& implements_trait(cx, trait_impl.self_ty(), copy_def_id, &[])
140141
{
@@ -170,12 +171,8 @@ impl LateLintPass<'_> for NonCanonicalImpls {
170171
String::new(),
171172
Applicability::MaybeIncorrect,
172173
);
173-
174-
return;
175174
}
176-
}
177-
178-
if cx.tcx.is_diagnostic_item(sym::PartialOrd, trait_impl.def_id)
175+
} else if trait_name == Some(sym::PartialOrd)
179176
&& impl_item.ident.name == sym::partial_cmp
180177
&& let Some(ord_def_id) = cx.tcx.get_diagnostic_item(sym::Ord)
181178
&& implements_trait(cx, trait_impl.self_ty(), ord_def_id, &[])

0 commit comments

Comments
 (0)