Skip to content

Commit

Permalink
Resolve cast-related clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Apr 21, 2024
1 parent 10a8a75 commit 4e66bd3
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 83 deletions.
66 changes: 53 additions & 13 deletions src/imp/arm_linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,25 +291,34 @@ macro_rules! atomic64 {
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
pub(crate) fn load(&self, order: Ordering) -> $int_type {
crate::utils::assert_load_ordering(order);
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by the kernel user helper or the lock
// and the raw pointer passed in is valid because we got it from a reference.
unsafe { atomic_load(self.v.get().cast::<u64>()) as $int_type }
unsafe {
atomic_load(self.v.get().cast::<u64>()) as $int_type
}
}

#[inline]
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)]
pub(crate) fn store(&self, val: $int_type, order: Ordering) {
crate::utils::assert_store_ordering(order);
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by the kernel user helper or the lock
// and the raw pointer passed in is valid because we got it from a reference.
unsafe { atomic_store(self.v.get().cast::<u64>(), val as u64) }
unsafe {
atomic_store(self.v.get().cast::<u64>(), val as u64)
}
}

#[inline]
pub(crate) fn swap(&self, val: $int_type, _order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by the kernel user helper or the lock
// and the raw pointer passed in is valid because we got it from a reference.
unsafe { atomic_swap(self.v.get().cast::<u64>(), val as u64) as $int_type }
unsafe {
atomic_swap(self.v.get().cast::<u64>(), val as u64) as $int_type
}
}

#[inline]
Expand All @@ -322,6 +331,7 @@ macro_rules! atomic64 {
failure: Ordering,
) -> Result<$int_type, $int_type> {
crate::utils::assert_compare_exchange_ordering(success, failure);
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by the kernel user helper or the lock
// and the raw pointer passed in is valid because we got it from a reference.
unsafe {
Expand Down Expand Up @@ -352,65 +362,92 @@ macro_rules! atomic64 {

#[inline]
pub(crate) fn fetch_add(&self, val: $int_type, _order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by the kernel user helper or the lock
// and the raw pointer passed in is valid because we got it from a reference.
unsafe { atomic_add(self.v.get().cast::<u64>(), val as u64) as $int_type }
unsafe {
atomic_add(self.v.get().cast::<u64>(), val as u64) as $int_type
}
}

#[inline]
pub(crate) fn fetch_sub(&self, val: $int_type, _order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by the kernel user helper or the lock
// and the raw pointer passed in is valid because we got it from a reference.
unsafe { atomic_sub(self.v.get().cast::<u64>(), val as u64) as $int_type }
unsafe {
atomic_sub(self.v.get().cast::<u64>(), val as u64) as $int_type
}
}

#[inline]
pub(crate) fn fetch_and(&self, val: $int_type, _order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by the kernel user helper or the lock
// and the raw pointer passed in is valid because we got it from a reference.
unsafe { atomic_and(self.v.get().cast::<u64>(), val as u64) as $int_type }
unsafe {
atomic_and(self.v.get().cast::<u64>(), val as u64) as $int_type
}
}

#[inline]
pub(crate) fn fetch_nand(&self, val: $int_type, _order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by the kernel user helper or the lock
// and the raw pointer passed in is valid because we got it from a reference.
unsafe { atomic_nand(self.v.get().cast::<u64>(), val as u64) as $int_type }
unsafe {
atomic_nand(self.v.get().cast::<u64>(), val as u64) as $int_type
}
}

#[inline]
pub(crate) fn fetch_or(&self, val: $int_type, _order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by the kernel user helper or the lock
// and the raw pointer passed in is valid because we got it from a reference.
unsafe { atomic_or(self.v.get().cast::<u64>(), val as u64) as $int_type }
unsafe {
atomic_or(self.v.get().cast::<u64>(), val as u64) as $int_type
}
}

#[inline]
pub(crate) fn fetch_xor(&self, val: $int_type, _order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by the kernel user helper or the lock
// and the raw pointer passed in is valid because we got it from a reference.
unsafe { atomic_xor(self.v.get().cast::<u64>(), val as u64) as $int_type }
unsafe {
atomic_xor(self.v.get().cast::<u64>(), val as u64) as $int_type
}
}

#[inline]
pub(crate) fn fetch_max(&self, val: $int_type, _order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by the kernel user helper or the lock
// and the raw pointer passed in is valid because we got it from a reference.
unsafe { $atomic_max(self.v.get().cast::<u64>(), val as u64) as $int_type }
unsafe {
$atomic_max(self.v.get().cast::<u64>(), val as u64) as $int_type
}
}

#[inline]
pub(crate) fn fetch_min(&self, val: $int_type, _order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by the kernel user helper or the lock
// and the raw pointer passed in is valid because we got it from a reference.
unsafe { $atomic_min(self.v.get().cast::<u64>(), val as u64) as $int_type }
unsafe {
$atomic_min(self.v.get().cast::<u64>(), val as u64) as $int_type
}
}

#[inline]
pub(crate) fn fetch_not(&self, _order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by the kernel user helper or the lock
// and the raw pointer passed in is valid because we got it from a reference.
unsafe { atomic_not(self.v.get().cast::<u64>()) as $int_type }
unsafe {
atomic_not(self.v.get().cast::<u64>()) as $int_type
}
}
#[inline]
pub(crate) fn not(&self, order: Ordering) {
Expand All @@ -419,9 +456,12 @@ macro_rules! atomic64 {

#[inline]
pub(crate) fn fetch_neg(&self, _order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by the kernel user helper or the lock
// and the raw pointer passed in is valid because we got it from a reference.
unsafe { atomic_neg(self.v.get().cast::<u64>()) as $int_type }
unsafe {
atomic_neg(self.v.get().cast::<u64>()) as $int_type
}
}
#[inline]
pub(crate) fn neg(&self, order: Ordering) {
Expand Down
67 changes: 54 additions & 13 deletions src/imp/atomic128/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ macro_rules! atomic128 {
)]
pub(crate) fn load(&self, order: Ordering) -> $int_type {
crate::utils::assert_load_ordering(order);
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by atomic intrinsics and the raw
// pointer passed in is valid because we got it from a reference.
unsafe { atomic_load(self.v.get().cast::<u128>(), order) as $int_type }
unsafe {
atomic_load(self.v.get().cast::<u128>(), order) as $int_type
}
}

#[inline]
Expand All @@ -54,17 +57,23 @@ macro_rules! atomic128 {
)]
pub(crate) fn store(&self, val: $int_type, order: Ordering) {
crate::utils::assert_store_ordering(order);
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by atomic intrinsics and the raw
// pointer passed in is valid because we got it from a reference.
unsafe { atomic_store(self.v.get().cast::<u128>(), val as u128, order) }
unsafe {
atomic_store(self.v.get().cast::<u128>(), val as u128, order)
}
}

#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub(crate) fn swap(&self, val: $int_type, order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by atomic intrinsics and the raw
// pointer passed in is valid because we got it from a reference.
unsafe { atomic_swap(self.v.get().cast::<u128>(), val as u128, order) as $int_type }
unsafe {
atomic_swap(self.v.get().cast::<u128>(), val as u128, order) as $int_type
}
}

#[inline]
Expand All @@ -80,6 +89,7 @@ macro_rules! atomic128 {
failure: Ordering,
) -> Result<$int_type, $int_type> {
crate::utils::assert_compare_exchange_ordering(success, failure);
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by atomic intrinsics and the raw
// pointer passed in is valid because we got it from a reference.
unsafe {
Expand Down Expand Up @@ -109,6 +119,7 @@ macro_rules! atomic128 {
failure: Ordering,
) -> Result<$int_type, $int_type> {
crate::utils::assert_compare_exchange_ordering(success, failure);
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by atomic intrinsics and the raw
// pointer passed in is valid because we got it from a reference.
unsafe {
Expand All @@ -128,73 +139,100 @@ macro_rules! atomic128 {
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub(crate) fn fetch_add(&self, val: $int_type, order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by atomic intrinsics and the raw
// pointer passed in is valid because we got it from a reference.
unsafe { atomic_add(self.v.get().cast::<u128>(), val as u128, order) as $int_type }
unsafe {
atomic_add(self.v.get().cast::<u128>(), val as u128, order) as $int_type
}
}

#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub(crate) fn fetch_sub(&self, val: $int_type, order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by atomic intrinsics and the raw
// pointer passed in is valid because we got it from a reference.
unsafe { atomic_sub(self.v.get().cast::<u128>(), val as u128, order) as $int_type }
unsafe {
atomic_sub(self.v.get().cast::<u128>(), val as u128, order) as $int_type
}
}

#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub(crate) fn fetch_and(&self, val: $int_type, order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by atomic intrinsics and the raw
// pointer passed in is valid because we got it from a reference.
unsafe { atomic_and(self.v.get().cast::<u128>(), val as u128, order) as $int_type }
unsafe {
atomic_and(self.v.get().cast::<u128>(), val as u128, order) as $int_type
}
}

#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub(crate) fn fetch_nand(&self, val: $int_type, order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by atomic intrinsics and the raw
// pointer passed in is valid because we got it from a reference.
unsafe { atomic_nand(self.v.get().cast::<u128>(), val as u128, order) as $int_type }
unsafe {
atomic_nand(self.v.get().cast::<u128>(), val as u128, order) as $int_type
}
}

#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub(crate) fn fetch_or(&self, val: $int_type, order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by atomic intrinsics and the raw
// pointer passed in is valid because we got it from a reference.
unsafe { atomic_or(self.v.get().cast::<u128>(), val as u128, order) as $int_type }
unsafe {
atomic_or(self.v.get().cast::<u128>(), val as u128, order) as $int_type
}
}

#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub(crate) fn fetch_xor(&self, val: $int_type, order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by atomic intrinsics and the raw
// pointer passed in is valid because we got it from a reference.
unsafe { atomic_xor(self.v.get().cast::<u128>(), val as u128, order) as $int_type }
unsafe {
atomic_xor(self.v.get().cast::<u128>(), val as u128, order) as $int_type
}
}

#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub(crate) fn fetch_max(&self, val: $int_type, order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by atomic intrinsics and the raw
// pointer passed in is valid because we got it from a reference.
unsafe { $atomic_max(self.v.get().cast::<u128>(), val as u128, order) as $int_type }
unsafe {
$atomic_max(self.v.get().cast::<u128>(), val as u128, order) as $int_type
}
}

#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub(crate) fn fetch_min(&self, val: $int_type, order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by atomic intrinsics and the raw
// pointer passed in is valid because we got it from a reference.
unsafe { $atomic_min(self.v.get().cast::<u128>(), val as u128, order) as $int_type }
unsafe {
$atomic_min(self.v.get().cast::<u128>(), val as u128, order) as $int_type
}
}

#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub(crate) fn fetch_not(&self, order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by atomic intrinsics and the raw
// pointer passed in is valid because we got it from a reference.
unsafe { atomic_not(self.v.get().cast::<u128>(), order) as $int_type }
unsafe {
atomic_not(self.v.get().cast::<u128>(), order) as $int_type
}
}
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
Expand All @@ -205,9 +243,12 @@ macro_rules! atomic128 {
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub(crate) fn fetch_neg(&self, order: Ordering) -> $int_type {
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: any data races are prevented by atomic intrinsics and the raw
// pointer passed in is valid because we got it from a reference.
unsafe { atomic_neg(self.v.get().cast::<u128>(), order) as $int_type }
unsafe {
atomic_neg(self.v.get().cast::<u128>(), order) as $int_type
}
}
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
Expand Down
2 changes: 1 addition & 1 deletion src/imp/fallback/outline_atomics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ macro_rules! atomic_rmw_3 {
#[cold]
pub(crate) unsafe fn $name(dst: *mut Udw, val: Udw, order: Ordering) -> Udw {
debug_assert_outline_atomics!();
#[allow(clippy::cast_ptr_alignment)]
#[allow(clippy::cast_ptr_alignment, clippy::cast_possible_wrap, clippy::cast_sign_loss)]
// SAFETY: the caller must uphold the safety contract.
unsafe {
(*(dst as *const $atomic_type)).$method_name(val as _, order) as Udw
Expand Down
Loading

0 comments on commit 4e66bd3

Please sign in to comment.