use rustc_hir::def_id::DefId;
fn any_of(callee: Option<DefId>, items: &[Option<DefId>]) -> bool {
callee.is_some_and(|c| items.contains(&Some(c)))
}
fn any_fn(callee: Option<DefId>, fns: &[DefId]) -> bool {
callee.is_some_and(|c| fns.contains(&c))
}
pub fn is_ownership_reconstruction(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::box_from_raw(),
crate::def_id::cstring_from_raw(),
crate::def_id::arc_from_raw(),
crate::def_id::rc_from_raw(),
crate::def_id::box_from_raw_in(),
crate::def_id::arc_from_raw_in(),
crate::def_id::rc_from_raw_in(),
crate::def_id::cstring_from_vec_with_nul_unchecked(),
],
)
}
pub fn is_as_ptr(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::slice_as_ptr(),
crate::def_id::slice_as_mut_ptr(),
crate::def_id::str_as_ptr(),
crate::def_id::str_as_mut_ptr(),
crate::def_id::vec_as_ptr(),
crate::def_id::vec_as_mut_ptr(),
crate::def_id::cstr_as_ptr(),
crate::def_id::nonnull_as_ptr(),
crate::def_id::const_ptr_slice_as_ptr(),
crate::def_id::mut_ptr_slice_as_mut_ptr(),
crate::def_id::nonnull_slice_as_mut_ptr(),
crate::def_id::box_as_ptr(),
crate::def_id::box_as_mut_ptr(),
crate::def_id::maybe_uninit_as_ptr(),
crate::def_id::maybe_uninit_as_mut_ptr(),
crate::def_id::arc_as_ptr(),
crate::def_id::rc_as_ptr(),
crate::def_id::const_ptr_cast(),
crate::def_id::const_ptr_cast_mut(),
crate::def_id::mut_ptr_cast(),
crate::def_id::mut_ptr_cast_const(),
crate::def_id::nonnull_cast(),
crate::def_id::box_into_raw(),
crate::def_id::cstring_into_raw(),
crate::def_id::arc_into_raw(),
crate::def_id::rc_into_raw(),
],
)
}
pub(crate) fn is_raw_ptr_cast(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::const_ptr_cast(),
crate::def_id::const_ptr_cast_mut(),
crate::def_id::mut_ptr_cast(),
crate::def_id::mut_ptr_cast_const(),
],
)
}
pub fn is_as_ptr_valid(callee: Option<DefId>) -> bool {
is_as_ptr(callee) && !is_raw_ptr_cast(callee)
}
pub fn is_str_as_bytes(callee: Option<DefId>) -> bool {
any_of(callee, &[crate::def_id::str_as_bytes()])
}
pub(crate) fn is_element_ptr_add(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::const_ptr_add(),
crate::def_id::const_ptr_wrapping_add(),
crate::def_id::const_ptr_offset(),
crate::def_id::const_ptr_wrapping_offset(),
crate::def_id::mut_ptr_add(),
crate::def_id::mut_ptr_wrapping_add(),
crate::def_id::mut_ptr_offset(),
crate::def_id::mut_ptr_wrapping_offset(),
crate::def_id::nonnull_add(),
crate::def_id::nonnull_offset(),
],
)
}
pub(crate) fn is_element_ptr_sub(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::const_ptr_sub(),
crate::def_id::const_ptr_wrapping_sub(),
crate::def_id::mut_ptr_sub(),
crate::def_id::mut_ptr_wrapping_sub(),
crate::def_id::nonnull_sub(),
],
)
}
pub(crate) fn is_byte_ptr_add(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::const_ptr_byte_add(),
crate::def_id::const_ptr_wrapping_byte_add(),
crate::def_id::const_ptr_byte_offset(),
crate::def_id::const_ptr_wrapping_byte_offset(),
crate::def_id::mut_ptr_byte_add(),
crate::def_id::mut_ptr_wrapping_byte_add(),
crate::def_id::mut_ptr_byte_offset(),
crate::def_id::mut_ptr_wrapping_byte_offset(),
crate::def_id::nonnull_byte_add(),
crate::def_id::nonnull_byte_offset(),
],
)
}
pub(crate) fn is_byte_ptr_sub(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::const_ptr_byte_sub(),
crate::def_id::const_ptr_wrapping_byte_sub(),
crate::def_id::mut_ptr_byte_sub(),
crate::def_id::mut_ptr_wrapping_byte_sub(),
crate::def_id::nonnull_byte_sub(),
],
)
}
pub fn is_pointer_add(callee: Option<DefId>) -> bool {
is_element_ptr_add(callee) || is_byte_ptr_add(callee)
}
pub fn is_pointer_sub(callee: Option<DefId>) -> bool {
is_element_ptr_sub(callee) || is_byte_ptr_sub(callee)
}
pub fn is_byte_ptr_arith(callee: Option<DefId>) -> bool {
is_byte_ptr_add(callee) || is_byte_ptr_sub(callee)
}
pub fn is_layout_constant(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::mem_size_of(),
crate::def_id::mem_align_of(),
crate::def_id::intrinsics_size_of(),
crate::def_id::intrinsics_align_of(),
],
)
}
pub fn is_align_offset(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::ptr_align_offset(),
crate::def_id::nonnull_align_offset(),
crate::def_id::const_ptr_align_offset(),
crate::def_id::mut_ptr_align_offset(),
],
)
}
pub fn is_ptr_write(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::ptr_write(),
crate::def_id::ptr_write_unaligned(),
crate::def_id::ptr_write_volatile(),
crate::def_id::ptr_write_bytes(),
],
)
}
pub fn is_ptr_read(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::ptr_read(),
crate::def_id::ptr_read_unaligned(),
crate::def_id::ptr_read_volatile(),
crate::def_id::copy_to(),
crate::def_id::copy_to_nonoverlapping(),
crate::def_id::copy_from(),
crate::def_id::copy_from_nonoverlapping(),
crate::def_id::assume_init_read(),
crate::def_id::intrinsics_copy(),
crate::def_id::intrinsics_copy_nonoverlapping(),
],
)
}
pub fn is_maybe_uninit_write(callee: Option<DefId>) -> bool {
any_of(callee, &[crate::def_id::maybe_uninit_write()])
}
pub fn is_maybe_uninit_uninit(callee: Option<DefId>) -> bool {
any_of(callee, &[crate::def_id::maybe_uninit_uninit()])
}
pub fn is_maybe_uninit_assume_init(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::maybe_uninit_assume_init(),
crate::def_id::assume_init_read(),
crate::def_id::maybe_uninit_assume_init_ref(),
crate::def_id::maybe_uninit_assume_init_mut(),
],
)
}
pub fn is_mem_copy_or_write(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::intrinsics_copy(),
crate::def_id::intrinsics_copy_nonoverlapping(),
crate::def_id::copy_from_nonoverlapping(),
crate::def_id::copy_to_nonoverlapping(),
crate::def_id::ptr_write(),
crate::def_id::ptr_write_bytes(),
],
)
}
pub fn is_len(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::len_fns())
}
pub fn is_capacity(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::capacity_fns())
}
pub fn is_unwrap(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::option_unwrap(),
crate::def_id::option_expect(),
crate::def_id::option_unwrap_unchecked(),
crate::def_id::result_unwrap(),
crate::def_id::result_unwrap_err(),
crate::def_id::result_expect(),
crate::def_id::result_expect_err(),
crate::def_id::result_unwrap_unchecked(),
],
)
}
pub fn is_from_raw_parts(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::from_raw_parts_fns())
}
pub fn is_from_raw_parts_mut(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::from_raw_parts_mut_fns())
}
pub fn is_cstr_from_ptr(callee: Option<DefId>) -> bool {
any_of(callee, &[crate::def_id::cstr_from_ptr()])
}
pub fn is_cstr_unchecked_constructor(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::cstr_from_bytes_with_nul_unchecked(),
crate::def_id::cstring_from_vec_with_nul_unchecked(),
],
)
}
pub fn is_vec_push_or_reserve(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::vec_push(),
crate::def_id::vec_reserve(),
crate::def_id::vec_reserve_exact(),
],
)
}
pub fn is_vec_alloc_constructor(callee: Option<DefId>) -> bool {
any_of(callee, &[crate::def_id::vec_from_elem()])
}
pub fn is_vec_from_box(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::slice_into_vec(),
crate::def_id::box_assume_init_into_vec_unsafe(),
],
)
}
pub fn is_vec_with_capacity(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::with_capacity_fns())
}
pub fn is_into_boxed_slice(callee: Option<DefId>) -> bool {
any_of(callee, &[crate::def_id::vec_into_boxed_slice()])
}
pub fn is_ownership_transfer(callee: Option<DefId>) -> bool {
let Some(callee) = callee else { return false };
is_ownership_reconstruction(Some(callee))
&& !crate::def_id::contains(
&[crate::def_id::cstring_from_vec_with_nul_unchecked()],
callee,
)
}
pub fn is_vec_ownership_transfer(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::vec_ownership_transfer_fns())
}
pub(crate) fn is_nonnull_checked_new(callee: Option<DefId>) -> bool {
any_of(callee, &[crate::def_id::nonnull_new()])
}
pub fn is_nonnull_as_ref_as_mut(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::nonnull_as_ref(),
crate::def_id::nonnull_as_mut(),
],
)
}
pub fn is_vec_invalidating_method(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::vec_push(),
crate::def_id::vec_reserve(),
crate::def_id::vec_reserve_exact(),
crate::def_id::vec_shrink_to_fit(),
crate::def_id::vec_shrink_to(),
crate::def_id::vec_insert(),
crate::def_id::vec_remove(),
crate::def_id::vec_clear(),
crate::def_id::vec_truncate(),
crate::def_id::vec_set_len(),
],
)
}
pub fn is_ownership_return(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::box_into_raw(),
crate::def_id::cstring_into_raw(),
crate::def_id::arc_into_raw(),
crate::def_id::rc_into_raw(),
],
)
}
pub fn is_benign_origin_use(callee: Option<DefId>) -> bool {
any_of(
callee,
&[
crate::def_id::const_ptr_is_null(),
crate::def_id::const_ptr_addr(),
crate::def_id::const_ptr_cast(),
crate::def_id::const_ptr_cast_mut(),
crate::def_id::const_ptr_slice_is_empty(),
crate::def_id::const_ptr_slice_len(),
crate::def_id::const_ptr_slice_as_ptr(),
crate::def_id::mut_ptr_is_null(),
crate::def_id::mut_ptr_addr(),
crate::def_id::mut_ptr_cast(),
crate::def_id::mut_ptr_cast_const(),
crate::def_id::mut_ptr_slice_is_empty(),
crate::def_id::mut_ptr_slice_len(),
crate::def_id::mut_ptr_slice_as_mut_ptr(),
crate::def_id::nonnull_addr(),
crate::def_id::nonnull_cast(),
crate::def_id::nonnull_as_ptr(),
crate::def_id::nonnull_slice_is_empty(),
crate::def_id::nonnull_slice_len(),
crate::def_id::nonnull_slice_as_mut_ptr(),
crate::def_id::slice_len(),
crate::def_id::slice_is_empty(),
crate::def_id::slice_as_ptr(),
crate::def_id::slice_as_mut_ptr(),
crate::def_id::str_len(),
crate::def_id::str_is_empty(),
crate::def_id::str_as_ptr(),
crate::def_id::str_as_mut_ptr(),
crate::def_id::vec_len(),
crate::def_id::vec_is_empty(),
crate::def_id::vec_as_ptr(),
crate::def_id::vec_as_mut_ptr(),
crate::def_id::string_len(),
crate::def_id::string_is_empty(),
crate::def_id::cstr_as_ptr(),
crate::def_id::cstr_is_empty(),
],
)
}
pub fn is_std_vec(def_id: DefId) -> bool {
crate::def_id::vec_types().contains(&def_id)
}
pub fn is_std_box(def_id: DefId) -> bool {
crate::def_id::box_types().contains(&def_id)
}
pub fn is_std_cstring(def_id: DefId) -> bool {
crate::def_id::cstring_types().contains(&def_id)
}
pub fn is_std_nonnull(def_id: DefId) -> bool {
crate::def_id::nonnull_types().contains(&def_id)
}
pub fn is_maybe_uninit_type(def_id: DefId) -> bool {
crate::def_id::maybe_uninit_types().contains(&def_id)
}
pub fn is_std_iter_or_itermut(def_id: DefId) -> bool {
crate::def_id::iter_types().contains(&def_id)
}
pub fn is_std_ordering(def_id: DefId) -> bool {
crate::def_id::ordering_types().contains(&def_id)
}
pub fn is_min_like(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::min_like_fns())
}
pub fn is_max(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::max_fns())
}
pub fn is_clamp(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::clamp_fns())
}
pub fn is_abs(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::abs_fns())
}
pub fn is_neg(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::neg_fns())
}
pub fn is_sat_unchecked_add(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::sat_unchecked_add_fns())
}
pub fn is_sat_unchecked_mul(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::sat_unchecked_mul_fns())
}
pub fn is_checked_add(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::checked_add_fns())
}
pub fn is_checked_mul(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::checked_mul_fns())
}
pub fn is_overflowing_abs_neg(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::overflowing_nz_fns())
}
pub fn is_bit_preserving_nz(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::bit_preserving_nz_fns())
}
pub fn is_checked_nonzero_iff(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::checked_nonzero_iff_fns())
}
pub fn is_checked_next_pow2(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::checked_next_pow2_fns())
}
pub fn is_layout_align(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::layout_align_fns())
}
pub fn is_split_at(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::split_at_fns())
}
pub fn is_align_to_local(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::align_to_local_fns())
}
pub fn is_iter_position(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::iter_position_fns())
}
pub fn is_strlen(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::strlen_fns())
}
pub fn is_slice_get_unchecked(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::slice_get_unchecked_fns())
}
pub fn is_sliceindex_get_unchecked(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::sliceindex_get_unchecked_fns())
}
pub fn is_slice_range(callee: Option<DefId>) -> bool {
any_fn(callee, crate::def_id::slice_range_fns())
}
pub fn is_mem_replace(callee: Option<DefId>) -> bool {
any_of(callee, &[crate::def_id::replace()])
}