Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions clippy_lints/src/casts/ptr_as_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 21 additions & 28 deletions clippy_lints/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,35 +254,28 @@ fn try_parse_contains<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'_>) -> Optio
_ => None,
});

match expr.kind {
ExprKind::MethodCall(
_,
if let ExprKind::MethodCall(_, map, [arg], _) = expr.kind
&& let Expr {
kind: ExprKind::AddrOf(_, _, key),
span: key_span,
..
} = arg
&& key_span.eq_ctxt(expr.span)
{
let id = cx.typeck_results().type_dependent_def_id(expr.hir_id)?;
let expr = ContainsExpr {
negated,
map,
[
Expr {
kind: ExprKind::AddrOf(_, _, key),
span: key_span,
..
},
],
_,
) if key_span.eq_ctxt(expr.span) => {
let id = cx.typeck_results().type_dependent_def_id(expr.hir_id)?;
let expr = ContainsExpr {
negated,
map,
key,
call_ctxt: expr.span.ctxt(),
};
if cx.tcx.is_diagnostic_item(sym::btreemap_contains_key, id) {
Some((MapType::BTree, expr))
} else if cx.tcx.is_diagnostic_item(sym::hashmap_contains_key, id) {
Some((MapType::Hash, expr))
} else {
None
}
},
_ => None,
key,
call_ctxt: expr.span.ctxt(),
};
match cx.tcx.get_diagnostic_name(id) {
Some(sym::btreemap_contains_key) => Some((MapType::BTree, expr)),
Some(sym::hashmap_contains_key) => Some((MapType::Hash, expr)),
_ => None,
}
} else {
None
}
}

Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/large_include_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ impl LateLintPass<'_> for LargeIncludeFile {
}
&& 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(
Expand Down
22 changes: 12 additions & 10 deletions clippy_lints/src/len_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,24 +355,26 @@ fn parse_len_output<'tcx>(cx: &LateContext<'tcx>, sig: FnSig<'tcx>) -> Option<Le
return Some(LenOutput::Integral);
}

if let Res::Def(_, def_id) = res {
if cx.tcx.is_diagnostic_item(sym::Option, def_id) && is_first_generic_integral(segment) {
return Some(LenOutput::Option(def_id));
} else if cx.tcx.is_diagnostic_item(sym::Result, def_id) && is_first_generic_integral(segment) {
return Some(LenOutput::Result(def_id));
if let Res::Def(_, def_id) = res
&& let Some(res) = match cx.tcx.get_diagnostic_name(def_id) {
Some(sym::Option) => Some(LenOutput::Option(def_id)),
Some(sym::Result) => Some(LenOutput::Result(def_id)),
_ => None,
}
&& is_first_generic_integral(segment)
{
return Some(res);
}

return None;
}

match *sig.output().kind() {
ty::Int(_) | ty::Uint(_) => Some(LenOutput::Integral),
ty::Adt(adt, subs) if cx.tcx.is_diagnostic_item(sym::Option, adt.did()) => {
subs.type_at(0).is_integral().then(|| LenOutput::Option(adt.did()))
},
ty::Adt(adt, subs) if cx.tcx.is_diagnostic_item(sym::Result, adt.did()) => {
subs.type_at(0).is_integral().then(|| LenOutput::Result(adt.did()))
ty::Adt(adt, subs) if subs.type_at(0).is_integral() => match cx.tcx.get_diagnostic_name(adt.did()) {
Some(sym::Option) => Some(LenOutput::Option(adt.did())),
Some(sym::Result) => Some(LenOutput::Result(adt.did())),
_ => None,
},
_ => None,
}
Expand Down
10 changes: 5 additions & 5 deletions clippy_lints/src/manual_retain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 4 additions & 6 deletions clippy_lints/src/manual_strip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,10 @@ impl<'tcx> LateLintPass<'tcx> for ManualStrip {
&& 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 {
Expand Down
45 changes: 21 additions & 24 deletions clippy_lints/src/methods/iter_out_of_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
28 changes: 11 additions & 17 deletions clippy_lints/src/methods/option_as_ref_deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion clippy_lints/src/methods/unnecessary_min_or_max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 3 additions & 6 deletions clippy_lints/src/non_canonical_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ impl LateLintPass<'_> for NonCanonicalImpls {
return;
}

if cx.tcx.is_diagnostic_item(sym::Clone, trait_impl.def_id)
let trait_name = cx.tcx.get_diagnostic_name(trait_impl.def_id);
if trait_name == Some(sym::Clone)
&& let Some(copy_def_id) = cx.tcx.get_diagnostic_item(sym::Copy)
&& implements_trait(cx, trait_impl.self_ty(), copy_def_id, &[])
{
Expand Down Expand Up @@ -170,12 +171,8 @@ impl LateLintPass<'_> for NonCanonicalImpls {
String::new(),
Applicability::MaybeIncorrect,
);

return;
}
}

if cx.tcx.is_diagnostic_item(sym::PartialOrd, trait_impl.def_id)
} else if trait_name == Some(sym::PartialOrd)
&& impl_item.ident.name == sym::partial_cmp
&& let Some(ord_def_id) = cx.tcx.get_diagnostic_item(sym::Ord)
&& implements_trait(cx, trait_impl.self_ty(), ord_def_id, &[])
Expand Down
12 changes: 5 additions & 7 deletions clippy_lints/src/non_octal_unix_permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@ impl<'tcx> LateLintPass<'tcx> for NonOctalUnixPermissions {
match &expr.kind {
ExprKind::MethodCall(path, func, [param], _) => {
if let Some(adt) = cx.typeck_results().expr_ty(func).peel_refs().ty_adt_def()
&& ((path.ident.name == sym::mode
&& matches!(
cx.tcx.get_diagnostic_name(adt.did()),
Some(sym::FsOpenOptions | sym::DirBuilder)
))
|| (path.ident.name == sym::set_mode
&& cx.tcx.is_diagnostic_item(sym::FsPermissions, adt.did())))
&& matches!(
(cx.tcx.get_diagnostic_name(adt.did()), path.ident.name),
(Some(sym::FsOpenOptions | sym::DirBuilder), sym::mode)
| (Some(sym::FsPermissions), sym::set_mode)
)
&& let ExprKind::Lit(_) = param.kind
&& param.span.eq_ctxt(expr.span)
&& param
Expand Down
12 changes: 4 additions & 8 deletions clippy_lints/src/operators/cmp_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions clippy_lints/src/redundant_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint_hir, span_lint_hir_and_then};
use clippy_utils::fn_has_unsatisfiable_preds;
use clippy_utils::mir::{LocalUsage, PossibleBorrowerMap, visit_local_usage};
use clippy_utils::source::SpanRangeExt;
use clippy_utils::ty::{has_drop, is_copy, is_type_diagnostic_item, is_type_lang_item, walk_ptrs_ty_depth};
use clippy_utils::ty::{has_drop, is_copy, is_type_lang_item, walk_ptrs_ty_depth};
use rustc_errors::Applicability;
use rustc_hir::intravisit::FnKind;
use rustc_hir::{Body, FnDecl, LangItem, def_id};
Expand Down Expand Up @@ -96,14 +96,13 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClone {
let (fn_def_id, arg, arg_ty, clone_ret) =
unwrap_or_continue!(is_call_with_ref_arg(cx, mir, &terminator.kind));

let fn_name = cx.tcx.get_diagnostic_name(fn_def_id);

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));
|| 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
&& (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_deref = !from_borrow && matches!(fn_name, Some(sym::path_to_pathbuf | sym::os_str_to_os_string));

if !from_borrow && !from_deref {
continue;
Expand Down Expand Up @@ -148,8 +147,9 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClone {
is_call_with_ref_arg(cx, mir, &pred_terminator.kind)
&& res == cloned
&& cx.tcx.is_diagnostic_item(sym::deref_method, pred_fn_def_id)
&& (is_type_diagnostic_item(cx, pred_arg_ty, sym::PathBuf)
|| is_type_diagnostic_item(cx, pred_arg_ty, sym::OsString))
&& let ty::Adt(pred_arg_def, _) = pred_arg_ty.kind()
&& let Some(pred_arg_name) = cx.tcx.get_diagnostic_name(pred_arg_def.did())
&& matches!(pred_arg_name, sym::PathBuf | sym::OsString)
{
(pred_arg, res)
} else {
Expand Down
7 changes: 4 additions & 3 deletions clippy_lints/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,8 @@ impl<'tcx> LateLintPass<'tcx> for TrimSplitWhitespace {
}

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)
)
}
Loading