Skip to content

Commit 6322b15

Browse files
committed
get_diagnostic_name in other places
1 parent 8779679 commit 6322b15

15 files changed

+87
-101
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/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/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/operators/cmp_owned.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,10 @@ fn check_op(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left: bool)
4747
(arg, arg.span)
4848
},
4949
ExprKind::Call(path, [arg])
50-
if path_def_id(cx, path).is_some_and(|did| {
51-
if cx.tcx.is_diagnostic_item(sym::from_str_method, did) {
52-
true
53-
} else if cx.tcx.is_diagnostic_item(sym::from_fn, did) {
54-
!is_copy(cx, typeck.expr_ty(expr))
55-
} else {
56-
false
57-
}
50+
if path_def_id(cx, path).is_some_and(|did| match cx.tcx.get_diagnostic_name(did) {
51+
Some(sym::from_str_method) => true,
52+
Some(sym::from_fn) => !is_copy(cx, typeck.expr_ty(expr)),
53+
_ => false,
5854
}) =>
5955
{
6056
(arg, arg.span)

clippy_lints/src/redundant_clone.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,13 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClone {
9696
let (fn_def_id, arg, arg_ty, clone_ret) =
9797
unwrap_or_continue!(is_call_with_ref_arg(cx, mir, &terminator.kind));
9898

99+
let fn_name = cx.tcx.get_diagnostic_name(fn_def_id);
100+
99101
let from_borrow = cx.tcx.lang_items().get(LangItem::CloneFn) == Some(fn_def_id)
100-
|| cx.tcx.is_diagnostic_item(sym::to_owned_method, fn_def_id)
101-
|| (cx.tcx.is_diagnostic_item(sym::to_string_method, fn_def_id)
102-
&& is_type_lang_item(cx, arg_ty, LangItem::String));
102+
|| fn_name == Some(sym::to_owned_method)
103+
|| (fn_name == Some(sym::to_string_method) && is_type_lang_item(cx, arg_ty, LangItem::String));
103104

104-
let from_deref = !from_borrow
105-
&& (cx.tcx.is_diagnostic_item(sym::path_to_pathbuf, fn_def_id)
106-
|| cx.tcx.is_diagnostic_item(sym::os_str_to_os_string, fn_def_id));
105+
let from_deref = !from_borrow && matches!(fn_name, Some(sym::path_to_pathbuf | sym::os_str_to_os_string));
107106

108107
if !from_borrow && !from_deref {
109108
continue;

clippy_lints/src/strings.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,8 @@ impl<'tcx> LateLintPass<'tcx> for TrimSplitWhitespace {
457457
}
458458

459459
fn is_one_of_trim_diagnostic_items(cx: &LateContext<'_>, trim_def_id: DefId) -> bool {
460-
cx.tcx.is_diagnostic_item(sym::str_trim, trim_def_id)
461-
|| cx.tcx.is_diagnostic_item(sym::str_trim_start, trim_def_id)
462-
|| cx.tcx.is_diagnostic_item(sym::str_trim_end, trim_def_id)
460+
matches!(
461+
cx.tcx.get_diagnostic_name(trim_def_id),
462+
Some(sym::str_trim | sym::str_trim_start | sym::str_trim_end)
463+
)
463464
}

0 commit comments

Comments
 (0)