get_diagnostic_name in other places

This commit is contained in:
Ada Alakbarova
2025-08-19 23:44:02 +02:00
parent 877967959a
commit 02bc3c94d2
15 changed files with 88 additions and 102 deletions
+4 -6
View File
@@ -62,12 +62,10 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Msrv)
&& let ExprKind::Path(ref qpath @ QPath::Resolved(None, path)) = func.kind
&& let Some(method_defid) = path.res.opt_def_id()
{
if cx.tcx.is_diagnostic_item(sym::ptr_null, method_defid) {
OmitFollowedCastReason::Null(qpath)
} else if cx.tcx.is_diagnostic_item(sym::ptr_null_mut, method_defid) {
OmitFollowedCastReason::NullMut(qpath)
} else {
OmitFollowedCastReason::None
match cx.tcx.get_diagnostic_name(method_defid) {
Some(sym::ptr_null) => OmitFollowedCastReason::Null(qpath),
Some(sym::ptr_null_mut) => OmitFollowedCastReason::NullMut(qpath),
_ => OmitFollowedCastReason::None,
}
} else {
OmitFollowedCastReason::None
+2 -2
View File
@@ -64,8 +64,8 @@ fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
}
&& len as u64 > self.max_file_size
&& let Some(macro_call) = root_macro_call_first_node(cx, expr)
&& (cx.tcx.is_diagnostic_item(sym::include_bytes_macro, macro_call.def_id)
|| cx.tcx.is_diagnostic_item(sym::include_str_macro, macro_call.def_id))
&& let Some(macro_name) = cx.tcx.get_diagnostic_name(macro_call.def_id)
&& matches!(macro_name, sym::include_bytes_macro | sym::include_str_macro)
{
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
+5 -5
View File
@@ -123,8 +123,8 @@ fn check_iter(
) {
if let hir::ExprKind::MethodCall(_, filter_expr, [], _) = &target_expr.kind
&& let Some(copied_def_id) = cx.typeck_results().type_dependent_def_id(target_expr.hir_id)
&& (cx.tcx.is_diagnostic_item(sym::iter_copied, copied_def_id)
|| cx.tcx.is_diagnostic_item(sym::iter_cloned, copied_def_id))
&& let Some(copied_name) = cx.tcx.get_diagnostic_name(copied_def_id)
&& matches!(copied_name, sym::iter_copied | sym::iter_cloned)
&& let hir::ExprKind::MethodCall(_, iter_expr, [_], _) = &filter_expr.kind
&& let Some(filter_def_id) = cx.typeck_results().type_dependent_def_id(filter_expr.hir_id)
&& cx.tcx.is_diagnostic_item(sym::iter_filter, filter_def_id)
@@ -243,9 +243,9 @@ fn make_sugg(
}
fn match_acceptable_sym(cx: &LateContext<'_>, collect_def_id: DefId) -> bool {
ACCEPTABLE_METHODS
.iter()
.any(|&method| cx.tcx.is_diagnostic_item(method, collect_def_id))
cx.tcx
.get_diagnostic_name(collect_def_id)
.is_some_and(|collect_name| ACCEPTABLE_METHODS.contains(&collect_name))
}
fn match_acceptable_type(cx: &LateContext<'_>, expr: &hir::Expr<'_>, msrv: Msrv) -> bool {
+4 -6
View File
@@ -75,12 +75,10 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
&& let ExprKind::Path(target_path) = &target_arg.kind
&& let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(cond.hir_id)
{
let strip_kind = if cx.tcx.is_diagnostic_item(sym::str_starts_with, method_def_id) {
StripKind::Prefix
} else if cx.tcx.is_diagnostic_item(sym::str_ends_with, method_def_id) {
StripKind::Suffix
} else {
return;
let strip_kind = match cx.tcx.get_diagnostic_name(method_def_id) {
Some(sym::str_starts_with) => StripKind::Prefix,
Some(sym::str_ends_with) => StripKind::Suffix,
_ => return,
};
let target_res = cx.qpath_res(target_path, target_arg.hir_id);
if target_res == Res::Err {
+21 -24
View File
@@ -24,32 +24,29 @@ fn get_iterator_length<'tcx>(cx: &LateContext<'tcx>, iter: &'tcx Expr<'tcx>) ->
let ty::Adt(adt, substs) = cx.typeck_results().expr_ty(iter).kind() else {
return None;
};
let did = adt.did();
if cx.tcx.is_diagnostic_item(sym::ArrayIntoIter, did) {
// For array::IntoIter<T, const N: usize>, the length is the second generic
// parameter.
substs.const_at(1).try_to_target_usize(cx.tcx).map(u128::from)
} else if cx.tcx.is_diagnostic_item(sym::SliceIter, did)
&& let ExprKind::MethodCall(_, recv, ..) = iter.kind
{
if let ty::Array(_, len) = cx.typeck_results().expr_ty(recv).peel_refs().kind() {
// For slice::Iter<'_, T>, the receiver might be an array literal: [1,2,3].iter().skip(..)
len.try_to_target_usize(cx.tcx).map(u128::from)
} else if let Some(args) = VecArgs::hir(cx, expr_or_init(cx, recv)) {
match args {
VecArgs::Vec(vec) => vec.len().try_into().ok(),
VecArgs::Repeat(_, len) => expr_as_u128(cx, len),
match cx.tcx.get_diagnostic_name(adt.did()) {
Some(sym::ArrayIntoIter) => {
// For array::IntoIter<T, const N: usize>, the length is the second generic
// parameter.
substs.const_at(1).try_to_target_usize(cx.tcx).map(u128::from)
},
Some(sym::SliceIter) if let ExprKind::MethodCall(_, recv, ..) = iter.kind => {
if let ty::Array(_, len) = cx.typeck_results().expr_ty(recv).peel_refs().kind() {
// For slice::Iter<'_, T>, the receiver might be an array literal: [1,2,3].iter().skip(..)
len.try_to_target_usize(cx.tcx).map(u128::from)
} else if let Some(args) = VecArgs::hir(cx, expr_or_init(cx, recv)) {
match args {
VecArgs::Vec(vec) => vec.len().try_into().ok(),
VecArgs::Repeat(_, len) => expr_as_u128(cx, len),
}
} else {
None
}
} else {
None
}
} else if cx.tcx.is_diagnostic_item(sym::IterEmpty, did) {
Some(0)
} else if cx.tcx.is_diagnostic_item(sym::IterOnce, did) {
Some(1)
} else {
None
},
Some(sym::IterEmpty) => Some(0),
Some(sym::IterOnce) => Some(1),
_ => None,
}
}
+11 -17
View File
@@ -38,17 +38,13 @@ pub(super) fn check(
];
let is_deref = match map_arg.kind {
hir::ExprKind::Path(ref expr_qpath) => {
cx.qpath_res(expr_qpath, map_arg.hir_id)
.opt_def_id()
.is_some_and(|fun_def_id| {
cx.tcx.is_diagnostic_item(sym::deref_method, fun_def_id)
|| cx.tcx.is_diagnostic_item(sym::deref_mut_method, fun_def_id)
|| deref_aliases
.iter()
.any(|&sym| cx.tcx.is_diagnostic_item(sym, fun_def_id))
})
},
hir::ExprKind::Path(ref expr_qpath) => cx
.qpath_res(expr_qpath, map_arg.hir_id)
.opt_def_id()
.and_then(|fun_def_id| cx.tcx.get_diagnostic_name(fun_def_id))
.is_some_and(|fun_name| {
matches!(fun_name, sym::deref_method | sym::deref_mut_method) || deref_aliases.contains(&fun_name)
}),
hir::ExprKind::Closure(&hir::Closure { body, .. }) => {
let closure_body = cx.tcx.hir_body(body);
let closure_expr = peel_blocks(closure_body.value);
@@ -63,13 +59,11 @@ pub(super) fn check(
.map(|x| &x.kind)
.collect::<Box<[_]>>()
&& let [ty::adjustment::Adjust::Deref(None), ty::adjustment::Adjust::Borrow(_)] = *adj
&& let method_did = cx.typeck_results().type_dependent_def_id(closure_expr.hir_id).unwrap()
&& let Some(method_name) = cx.tcx.get_diagnostic_name(method_did)
{
let method_did = cx.typeck_results().type_dependent_def_id(closure_expr.hir_id).unwrap();
cx.tcx.is_diagnostic_item(sym::deref_method, method_did)
|| cx.tcx.is_diagnostic_item(sym::deref_mut_method, method_did)
|| deref_aliases
.iter()
.any(|&sym| cx.tcx.is_diagnostic_item(sym, method_did))
matches!(method_name, sym::deref_method | sym::deref_mut_method)
|| deref_aliases.contains(&method_name)
} else {
false
}
@@ -22,7 +22,8 @@ pub(super) fn check<'tcx>(
let typeck_results = cx.typeck_results();
let ecx = ConstEvalCtxt::with_env(cx.tcx, cx.typing_env(), typeck_results);
if let Some(id) = typeck_results.type_dependent_def_id(expr.hir_id)
&& (cx.tcx.is_diagnostic_item(sym::cmp_ord_min, id) || cx.tcx.is_diagnostic_item(sym::cmp_ord_max, id))
&& let Some(fn_name) = cx.tcx.get_diagnostic_name(id)
&& matches!(fn_name, sym::cmp_ord_min | sym::cmp_ord_max)
{
if let Some((left, ConstantSource::Local | ConstantSource::CoreConstant)) = ecx.eval_with_source(recv)
&& let Some((right, ConstantSource::Local | ConstantSource::CoreConstant)) = ecx.eval_with_source(arg)
+4 -8
View File
@@ -47,14 +47,10 @@ fn check_op(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left: bool)
(arg, arg.span)
},
ExprKind::Call(path, [arg])
if path_def_id(cx, path).is_some_and(|did| {
if cx.tcx.is_diagnostic_item(sym::from_str_method, did) {
true
} else if cx.tcx.is_diagnostic_item(sym::from_fn, did) {
!is_copy(cx, typeck.expr_ty(expr))
} else {
false
}
if path_def_id(cx, path).is_some_and(|did| match cx.tcx.get_diagnostic_name(did) {
Some(sym::from_str_method) => true,
Some(sym::from_fn) => !is_copy(cx, typeck.expr_ty(expr)),
_ => false,
}) =>
{
(arg, arg.span)
+6 -7
View File
@@ -96,14 +96,13 @@ fn check_fn(
let (fn_def_id, arg, arg_ty, clone_ret) =
unwrap_or_continue!(is_call_with_ref_arg(cx, mir, &terminator.kind));
let from_borrow = cx.tcx.lang_items().get(LangItem::CloneFn) == Some(fn_def_id)
|| cx.tcx.is_diagnostic_item(sym::to_owned_method, fn_def_id)
|| (cx.tcx.is_diagnostic_item(sym::to_string_method, fn_def_id)
&& is_type_lang_item(cx, arg_ty, LangItem::String));
let fn_name = cx.tcx.get_diagnostic_name(fn_def_id);
let from_deref = !from_borrow
&& (cx.tcx.is_diagnostic_item(sym::path_to_pathbuf, fn_def_id)
|| cx.tcx.is_diagnostic_item(sym::os_str_to_os_string, fn_def_id));
let from_borrow = cx.tcx.lang_items().get(LangItem::CloneFn) == Some(fn_def_id)
|| fn_name == Some(sym::to_owned_method)
|| (fn_name == Some(sym::to_string_method) && is_type_lang_item(cx, arg_ty, LangItem::String));
let from_deref = !from_borrow && matches!(fn_name, Some(sym::path_to_pathbuf | sym::os_str_to_os_string));
if !from_borrow && !from_deref {
continue;
+4 -3
View File
@@ -457,7 +457,8 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
}
fn is_one_of_trim_diagnostic_items(cx: &LateContext<'_>, trim_def_id: DefId) -> bool {
cx.tcx.is_diagnostic_item(sym::str_trim, trim_def_id)
|| cx.tcx.is_diagnostic_item(sym::str_trim_start, trim_def_id)
|| cx.tcx.is_diagnostic_item(sym::str_trim_end, trim_def_id)
matches!(
cx.tcx.get_diagnostic_name(trim_def_id),
Some(sym::str_trim | sym::str_trim_start | sym::str_trim_end)
)
}
+3 -2
View File
@@ -11,7 +11,8 @@
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool {
let app = Applicability::Unspecified;
if cx.tcx.is_diagnostic_item(sym::Rc, def_id) {
let name = cx.tcx.get_diagnostic_name(def_id);
if name == Some(sym::Rc) {
if let Some(alternate) = match_buffer_type(cx, qpath) {
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
@@ -56,7 +57,7 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
);
return true;
}
} else if cx.tcx.is_diagnostic_item(sym::Arc, def_id) {
} else if name == Some(sym::Arc) {
if let Some(alternate) = match_buffer_type(cx, qpath) {
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
@@ -13,14 +13,11 @@
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, hir_ty: &hir::Ty<'tcx>, qpath: &QPath<'tcx>, def_id: DefId) -> bool {
let mut applicability = Applicability::MaybeIncorrect;
let outer_sym = if Some(def_id) == cx.tcx.lang_items().owned_box() {
"Box"
} else if cx.tcx.is_diagnostic_item(sym::Rc, def_id) {
"Rc"
} else if cx.tcx.is_diagnostic_item(sym::Arc, def_id) {
"Arc"
} else {
return false;
let outer_sym = match cx.tcx.get_diagnostic_name(def_id) {
_ if Some(def_id) == cx.tcx.lang_items().owned_box() => "Box",
Some(sym::Rc) => "Rc",
Some(sym::Arc) => "Arc",
_ => return false,
};
if let Some(span) = utils::match_borrows_parameter(cx, qpath) {
@@ -41,7 +41,8 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
&& let ty::Ref(_, inner_str, _) = cx.typeck_results().expr_ty_adjusted(expr).kind()
&& inner_str.is_str()
{
if cx.tcx.is_diagnostic_item(sym::string_new, fun_def_id) {
let fun_name = cx.tcx.get_diagnostic_name(fun_def_id);
if fun_name == Some(sym::string_new) {
span_lint_and_sugg(
cx,
UNNECESSARY_OWNED_EMPTY_STRINGS,
@@ -51,7 +52,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
"\"\"".to_owned(),
Applicability::MachineApplicable,
);
} else if cx.tcx.is_diagnostic_item(sym::from_fn, fun_def_id)
} else if fun_name == Some(sym::from_fn)
&& let [arg] = args
&& let ExprKind::Lit(spanned) = &arg.kind
&& let LitKind::Str(symbol, _) = spanned.node
+4 -6
View File
@@ -107,12 +107,10 @@ fn check_fn(
// Get the wrapper and inner types, if can't, abort.
let (return_type_label, lang_item, inner_type) =
if let ty::Adt(adt_def, subst) = return_ty(cx, hir_id.expect_owner()).kind() {
if cx.tcx.is_diagnostic_item(sym::Option, adt_def.did()) {
("Option", OptionSome, subst.type_at(0))
} else if cx.tcx.is_diagnostic_item(sym::Result, adt_def.did()) {
("Result", ResultOk, subst.type_at(0))
} else {
return;
match cx.tcx.get_diagnostic_name(adt_def.did()) {
Some(sym::Option) => ("Option", OptionSome, subst.type_at(0)),
Some(sym::Result) => ("Result", ResultOk, subst.type_at(0)),
_ => return,
}
} else {
return;
+10 -5
View File
@@ -177,8 +177,8 @@ fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) -> Self::Result {
Node::Expr(expr) if let ExprKind::AddrOf(_, Mutability::Not, _) = expr.kind => {},
Node::Expr(expr)
if let Some(fn_did) = fn_def_id(self.cx, expr)
&& (self.cx.tcx.is_diagnostic_item(sym::child_id, fn_did)
|| self.cx.tcx.is_diagnostic_item(sym::child_kill, fn_did)) => {},
&& let Some(fn_name) = self.cx.tcx.get_diagnostic_name(fn_did)
&& matches!(fn_name, sym::child_id | sym::child_kill) => {},
// Conservatively assume that all other kinds of nodes call `.wait()` somehow.
_ => return Break(MaybeWait(ex.span)),
@@ -351,9 +351,14 @@ fn check<'tcx>(cx: &LateContext<'tcx>, spawn_expr: &'tcx Expr<'tcx>, cause: Caus
/// Checks if the given expression exits the process.
fn is_exit_expression(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
fn_def_id(cx, expr).is_some_and(|fn_did| {
cx.tcx.is_diagnostic_item(sym::process_exit, fn_did) || cx.tcx.is_diagnostic_item(sym::process_abort, fn_did)
})
if let Some(fn_did) = fn_def_id(cx, expr)
&& let Some(fn_name) = cx.tcx.get_diagnostic_name(fn_did)
&& matches!(fn_name, sym::process_exit | sym::process_abort)
{
true
} else {
false
}
}
#[derive(Debug)]