rapx/def_ids/
other_api.rs1use rustc_hir::def_id::DefId;
12use rustc_middle::ty::{TyCtxt, TyKind, UintTy};
13
14use crate::def_ids::lang_api;
15
16use super::{diagnostic_api, is_std_api, type_ids};
17
18pub fn is_ub_check(tcx: TyCtxt<'_>, callee: DefId) -> bool {
21 if !is_std_api(tcx, callee) {
22 return false;
23 }
24 let path = tcx.def_path_str(callee);
25 path.contains("ub_checks::check_language_ub")
26 || path.contains("::precondition_check")
27 || path.contains("::runtime_add_nowrap")
28 || path.contains("ub_checks::maybe_is_aligned")
29 || path.contains("ub_checks::is_valid_allocation_size")
30 || path.contains("ub_checks::maybe_is_nonoverlapping")
31}
32
33pub fn is_box_new(tcx: TyCtxt<'_>, did: DefId) -> bool {
37 if !tcx
38 .opt_item_name(did)
39 .is_some_and(|name| matches!(name.as_str(), "new" | "new_in"))
40 {
41 return false;
42 }
43 let Some(impl_id) = tcx.inherent_impl_of_assoc(did) else {
44 return false;
45 };
46 let self_ty = tcx.type_of(impl_id).instantiate_identity().skip_norm_wip();
47 matches!(self_ty.kind(), TyKind::Adt(adt, _) if type_ids::is_std_box(adt.did()))
48}
49
50pub fn is_nonnull_dangling(tcx: TyCtxt<'_>, did: DefId) -> bool {
54 if !tcx
55 .opt_item_name(did)
56 .is_some_and(|name| name.as_str() == "dangling")
57 {
58 return false;
59 }
60 let Some(impl_id) = tcx.inherent_impl_of_assoc(did) else {
61 return false;
62 };
63 let self_ty = tcx.type_of(impl_id).instantiate_identity().skip_norm_wip();
64 matches!(self_ty.kind(), TyKind::Adt(adt, _)
65 if type_ids::is_std_nonnull(adt.did()))
66 && tcx.fn_sig(did).skip_binder().inputs().skip_binder().is_empty()
67}
68
69pub fn is_manually_drop(tcx: TyCtxt<'_>, did: DefId) -> bool {
72 let Some(impl_id) = tcx.inherent_impl_of_assoc(did) else {
73 return false;
74 };
75 let self_ty = tcx.type_of(impl_id).instantiate_identity();
76 let self_ty = self_ty.skip_norm_wip();
77 matches!(self_ty.kind(), TyKind::Adt(adt, _)
78 if tcx.is_lang_item(adt.did(), lang_api::LangItem::ManuallyDrop))
79 && tcx.item_name(did).as_str() == "drop"
80}
81
82pub fn is_drop_fn(tcx: TyCtxt<'_>, target: DefId) -> bool {
85 diagnostic_api::is_drop(tcx, target)
86 || diagnostic_api::is_drop_in_place(tcx, target)
87 || is_manually_drop(tcx, target)
88}
89
90pub fn is_align_offset(tcx: TyCtxt<'_>, did: DefId) -> bool {
97 if !is_std_api(tcx, did)
98 || !tcx
99 .opt_item_name(did)
100 .is_some_and(|name| name.as_str() == "align_offset")
101 {
102 return false;
103 }
104 let sig = tcx.fn_sig(did).skip_binder();
105 let inputs = sig.inputs().skip_binder();
106 inputs.len() == 2
107 && matches!(inputs[0].kind(), TyKind::RawPtr(..))
108 && matches!(inputs[1].kind(), TyKind::Uint(UintTy::Usize))
109 && matches!(sig.output().skip_binder().kind(), TyKind::Uint(UintTy::Usize))
110}
111
112pub fn is_gcd(tcx: TyCtxt<'_>, did: DefId) -> bool {
116 is_std_api(tcx, did) && tcx.def_path_str(did).ends_with("::gcd")
117}