From cc010b3be144781a627fc213f5f80824c1434063 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 24 Mar 2025 16:12:48 -0500 Subject: [PATCH 1/5] Fix `clippy::ptr_as_ptr` warnings --- src/deque.rs | 12 ++++++------ src/histbuf.rs | 4 ++-- src/lib.rs | 3 ++- src/spsc.rs | 4 ++-- src/storage.rs | 4 ++-- src/vec/mod.rs | 12 ++++++------ 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/deque.rs b/src/deque.rs index 8a8e720efa..0748615a67 100644 --- a/src/deque.rs +++ b/src/deque.rs @@ -269,15 +269,15 @@ impl + ?Sized> DequeInner { } else if self.back <= self.front { ( slice::from_raw_parts( - self.buffer.borrow().as_ptr().add(self.front) as *const T, + self.buffer.borrow().as_ptr().add(self.front).cast::(), self.storage_capacity() - self.front, ), - slice::from_raw_parts(self.buffer.borrow().as_ptr() as *const T, self.back), + slice::from_raw_parts(self.buffer.borrow().as_ptr().cast::(), self.back), ) } else { ( slice::from_raw_parts( - self.buffer.borrow().as_ptr().add(self.front) as *const T, + self.buffer.borrow().as_ptr().add(self.front).cast::(), self.back - self.front, ), &[], @@ -297,15 +297,15 @@ impl + ?Sized> DequeInner { } else if self.back <= self.front { ( slice::from_raw_parts_mut( - ptr.add(self.front) as *mut T, + ptr.add(self.front).cast::(), self.storage_capacity() - self.front, ), - slice::from_raw_parts_mut(ptr as *mut T, self.back), + slice::from_raw_parts_mut(ptr.cast::(), self.back), ) } else { ( slice::from_raw_parts_mut( - ptr.add(self.front) as *mut T, + ptr.add(self.front).cast::(), self.back - self.front, ), &mut [], diff --git a/src/histbuf.rs b/src/histbuf.rs index 752c0f0006..5300126759 100644 --- a/src/histbuf.rs +++ b/src/histbuf.rs @@ -291,7 +291,7 @@ impl + ?Sized> HistoryBufferInner { unsafe fn drop_contents(&mut self) { unsafe { ptr::drop_in_place(ptr::slice_from_raw_parts_mut( - self.data.borrow_mut().as_mut_ptr() as *mut T, + self.data.borrow_mut().as_mut_ptr().cast::(), self.len(), )) } @@ -446,7 +446,7 @@ impl + ?Sized> HistoryBufferInner { /// Returns the array slice backing the buffer, without keeping track /// of the write position. Therefore, the element order is unspecified. pub fn as_slice(&self) -> &[T] { - unsafe { slice::from_raw_parts(self.data.borrow().as_ptr() as *const _, self.len()) } + unsafe { slice::from_raw_parts(self.data.borrow().as_ptr().cast(), self.len()) } } /// Returns a pair of slices which contain, in order, the contents of the buffer. diff --git a/src/lib.rs b/src/lib.rs index 9a4b42f2c0..19a8501c62 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -144,7 +144,8 @@ clippy::use_self, clippy::too_long_first_doc_paragraph, clippy::redundant_pub_crate, - clippy::option_if_let_else + clippy::option_if_let_else, + clippy::ptr_as_ptr, )] pub use binary_heap::BinaryHeap; diff --git a/src/spsc.rs b/src/spsc.rs index 10a0e26eb7..3f1661fc43 100644 --- a/src/spsc.rs +++ b/src/spsc.rs @@ -465,7 +465,7 @@ impl<'a, T, S: Storage> Iterator for IterMutInner<'a, T, S> { let i = (head + self.index) % self.rb.n(); self.index += 1; - Some(unsafe { &mut *(self.rb.buffer.borrow().get_unchecked(i).get() as *mut T) }) + Some(unsafe { &mut *self.rb.buffer.borrow().get_unchecked(i).get().cast::() }) } else { None } @@ -495,7 +495,7 @@ impl DoubleEndedIterator for IterMutInner<'_, T, S> { // self.len > 0, since it's larger than self.index > 0 let i = (head + self.len - 1) % self.rb.n(); self.len -= 1; - Some(unsafe { &mut *(self.rb.buffer.borrow().get_unchecked(i).get() as *mut T) }) + Some(unsafe { &mut *self.rb.buffer.borrow().get_unchecked(i).get().cast::() }) } else { None } diff --git a/src/storage.rs b/src/storage.rs index 34c3746e18..2995dae195 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -43,7 +43,7 @@ impl SealedStorage for OwnedStorage { N } fn as_ptr(this: *mut Self::Buffer) -> *mut T { - this as _ + this.cast() } } @@ -57,6 +57,6 @@ impl SealedStorage for ViewStorage { } fn as_ptr(this: *mut Self::Buffer) -> *mut T { - this as _ + this.cast() } } diff --git a/src/vec/mod.rs b/src/vec/mod.rs index 2833480596..b5f7ba4702 100644 --- a/src/vec/mod.rs +++ b/src/vec/mod.rs @@ -255,7 +255,7 @@ impl Vec { pub fn into_array(self) -> Result<[T; M], Self> { if self.len() == M { // This is how the unstable `MaybeUninit::array_assume_init` method does it - let array = unsafe { (&self.buffer as *const _ as *const [T; M]).read() }; + let array = unsafe { (core::ptr::from_ref(&self.buffer).cast::<[T; M]>()).read() }; // We don't want `self`'s destructor to be called because that would drop all the // items in the array @@ -431,12 +431,12 @@ impl VecView { impl + ?Sized> VecInner { /// Returns a raw pointer to the vector’s buffer. pub fn as_ptr(&self) -> *const T { - self.buffer.borrow().as_ptr() as *const T + self.buffer.borrow().as_ptr().cast::() } /// Returns a raw pointer to the vector’s buffer, which may be mutated through. pub fn as_mut_ptr(&mut self) -> *mut T { - self.buffer.borrow_mut().as_mut_ptr() as *mut T + self.buffer.borrow_mut().as_mut_ptr().cast::() } /// Extracts a slice containing the entire vector. @@ -453,7 +453,7 @@ impl + ?Sized> VecInner { pub fn as_slice(&self) -> &[T] { // NOTE(unsafe) avoid bound checks in the slicing operation // &buffer[..self.len] - unsafe { slice::from_raw_parts(self.buffer.borrow().as_ptr() as *const T, self.len) } + unsafe { slice::from_raw_parts(self.buffer.borrow().as_ptr().cast::(), self.len) } } /// Extracts a mutable slice containing the entire vector. @@ -473,7 +473,7 @@ impl + ?Sized> VecInner { // NOTE(unsafe) avoid bound checks in the slicing operation // &mut buffer[..self.len] unsafe { - slice::from_raw_parts_mut(self.buffer.borrow_mut().as_mut_ptr() as *mut T, self.len) + slice::from_raw_parts_mut(self.buffer.borrow_mut().as_mut_ptr().cast::(), self.len) } } @@ -1317,7 +1317,7 @@ where if self.next < self.vec.len() { let s = unsafe { slice::from_raw_parts( - (self.vec.buffer.buffer.as_ptr() as *const T).add(self.next), + self.vec.buffer.buffer.as_ptr().cast::().add(self.next), self.vec.len() - self.next, ) }; From 1635b7292a5746e060a0716096541e8437f4b137 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 24 Mar 2025 16:16:40 -0500 Subject: [PATCH 2/5] Fix `clippy::doc_markdown` warnings --- src/binary_heap.rs | 2 +- src/histbuf.rs | 4 ++-- src/lib.rs | 15 ++++++++------- src/mpmc.rs | 4 ++-- src/spsc.rs | 2 +- src/vec/mod.rs | 6 +++--- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/binary_heap.rs b/src/binary_heap.rs index 7cfcc97d5f..bd586f71d8 100644 --- a/src/binary_heap.rs +++ b/src/binary_heap.rs @@ -159,7 +159,7 @@ pub type BinaryHeapView = BinaryHeapInner>; impl BinaryHeap { /* Constructors */ - /// Creates an empty BinaryHeap as a $K-heap. + /// Creates an empty `BinaryHeap` as a $K-heap. /// /// ``` /// use heapless::binary_heap::{BinaryHeap, Max}; diff --git a/src/histbuf.rs b/src/histbuf.rs index 5300126759..f3ab69958d 100644 --- a/src/histbuf.rs +++ b/src/histbuf.rs @@ -791,7 +791,7 @@ mod tests { assert_eq!(x.as_slice(), [5, 2, 3, 4]); } - /// Test whether .as_slices() behaves as expected. + /// Test whether `.as_slices()` behaves as expected. #[test] fn as_slices() { let mut buffer: HistoryBuffer = HistoryBuffer::new(); @@ -807,7 +807,7 @@ mod tests { extend_then_assert(b"123456", (b"34", b"56")); } - /// Test whether .as_slices() and .oldest_ordered() produce elements in the same order. + /// Test whether `.as_slices()` and `.oldest_ordered()` produce elements in the same order. #[test] fn as_slices_equals_ordered() { let mut buffer: HistoryBuffer = HistoryBuffer::new(); diff --git a/src/lib.rs b/src/lib.rs index 19a8501c62..7bc0cc8501 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -110,17 +110,17 @@ ), doc = "- [Object](pool::object::Object) -- objects managed by an object pool" )] -//! - [BinaryHeap] -- priority queue +//! - [`BinaryHeap`] -- priority queue //! - [Deque] -- double-ended queue -//! - [HistoryBuffer] -- similar to a write-only ring buffer -//! - [IndexMap] -- hash table -//! - [IndexSet] -- hash set -//! - [LinearMap] -//! - [sorted_linked_list::SortedLinkedList] +//! - [`HistoryBuffer`] -- similar to a write-only ring buffer +//! - [`IndexMap`] -- hash table +//! - [`IndexSet`] -- hash set +//! - [`LinearMap`] +//! - [`sorted_linked_list::SortedLinkedList`] //! - [String] //! - [Vec] //! - [`mpmc::Q*`](mpmc) -- multiple producer multiple consumer lock-free queue -//! - [spsc] and [spsc::Queue] -- single producer single consumer lock-free queue +//! - [spsc] and [`spsc::Queue`] -- single producer single consumer lock-free queue //! //! # Minimum Supported Rust Version (MSRV) //! @@ -146,6 +146,7 @@ clippy::redundant_pub_crate, clippy::option_if_let_else, clippy::ptr_as_ptr, + clippy::doc_markdown, )] pub use binary_heap::BinaryHeap; diff --git a/src/mpmc.rs b/src/mpmc.rs index 9ef54a25d0..99f004e1dc 100644 --- a/src/mpmc.rs +++ b/src/mpmc.rs @@ -142,12 +142,12 @@ pub struct MpMcQueueInner { /// MPMC queue with a capacity for N elements /// N must be a power of 2 -/// The max value of N is u8::MAX - 1 if `mpmc_large` feature is not enabled. +/// The max value of N is `u8::MAX` - 1 if `mpmc_large` feature is not enabled. pub type MpMcQueue = MpMcQueueInner>; /// MPMC queue with a capacity for N elements /// N must be a power of 2 -/// The max value of N is u8::MAX - 1 if `mpmc_large` feature is not enabled. +/// The max value of N is `u8::MAX` - 1 if `mpmc_large` feature is not enabled. pub type MpMcQueueView = MpMcQueueInner; impl MpMcQueue { diff --git a/src/spsc.rs b/src/spsc.rs index 3f1661fc43..c1b94c141f 100644 --- a/src/spsc.rs +++ b/src/spsc.rs @@ -27,7 +27,7 @@ //! assert_eq!(rb.dequeue(), Some(0)); //! ``` //! -//! - [Queue] can be [Queue::split] and then be used in Single Producer Single Consumer mode. +//! - [Queue] can be [`Queue::split`] and then be used in Single Producer Single Consumer mode. //! //! "no alloc" applications can create a `&'static mut` reference to a `Queue` -- using a static //! variable -- and then `split` it: this consumes the static reference. The resulting `Consumer` diff --git a/src/vec/mod.rs b/src/vec/mod.rs index b5f7ba4702..c47b74e9fe 100644 --- a/src/vec/mod.rs +++ b/src/vec/mod.rs @@ -619,11 +619,11 @@ impl + ?Sized> VecInner { } } - /// Resizes the Vec in-place so that len is equal to new_len. + /// Resizes the Vec in-place so that len is equal to `new_len`. /// - /// If new_len is greater than len, the Vec is extended by the + /// If `new_len` is greater than len, the Vec is extended by the /// difference, with each additional slot filled with value. If - /// new_len is less than len, the Vec is simply truncated. + /// `new_len` is less than len, the Vec is simply truncated. /// /// See also [`resize_default`](Self::resize_default). #[allow(clippy::result_unit_err)] From 739dbc4c3c349df40c1f60adb318bab954bd582f Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 24 Mar 2025 16:21:19 -0500 Subject: [PATCH 3/5] Fix `clippy::semicolon_if_nothing_returned` warnings --- src/binary_heap.rs | 2 +- src/deque.rs | 2 +- src/histbuf.rs | 6 +++--- src/indexmap.rs | 8 ++++---- src/indexset.rs | 6 +++--- src/lib.rs | 1 + src/linear_map.rs | 2 +- src/pool/arc.rs | 4 ++-- src/pool/boxed.rs | 4 ++-- src/pool/object.rs | 4 ++-- src/pool/treiber.rs | 2 +- src/pool/treiber/cas.rs | 2 +- src/sorted_linked_list.rs | 6 +++--- src/spsc.rs | 4 ++-- src/string/mod.rs | 14 +++++++------- src/vec/mod.rs | 10 +++++----- tests/tsan.rs | 2 +- 17 files changed, 40 insertions(+), 39 deletions(-) diff --git a/src/binary_heap.rs b/src/binary_heap.rs index bd586f71d8..5125aedea9 100644 --- a/src/binary_heap.rs +++ b/src/binary_heap.rs @@ -222,7 +222,7 @@ where /// assert!(heap.is_empty()); /// ``` pub fn clear(&mut self) { - self.data.clear() + self.data.clear(); } /// Returns the length of the binary heap. diff --git a/src/deque.rs b/src/deque.rs index 0748615a67..b170de1f9e 100644 --- a/src/deque.rs +++ b/src/deque.rs @@ -896,7 +896,7 @@ impl + ?Sized> Extend for DequeInner { } impl<'a, T: 'a + Copy, S: VecStorage + ?Sized> Extend<&'a T> for DequeInner { fn extend>(&mut self, iter: I) { - self.extend(iter.into_iter().copied()) + self.extend(iter.into_iter().copied()); } } diff --git a/src/histbuf.rs b/src/histbuf.rs index f3ab69958d..cedd9eb93b 100644 --- a/src/histbuf.rs +++ b/src/histbuf.rs @@ -293,7 +293,7 @@ impl + ?Sized> HistoryBufferInner { ptr::drop_in_place(ptr::slice_from_raw_parts_mut( self.data.borrow_mut().as_mut_ptr().cast::(), self.len(), - )) + )); } } @@ -514,7 +514,7 @@ where where I: IntoIterator, { - self.extend(iter.into_iter().cloned()) + self.extend(iter.into_iter().cloned()); } } @@ -818,7 +818,7 @@ mod tests { assert_eq_iter( [head, tail].iter().copied().flatten(), buffer.oldest_ordered(), - ) + ); } } diff --git a/src/indexmap.rs b/src/indexmap.rs index 54abf8e6a3..c0afe2b702 100644 --- a/src/indexmap.rs +++ b/src/indexmap.rs @@ -1298,7 +1298,7 @@ where where I: IntoIterator, { - self.extend(iterable.into_iter().map(|(&key, &value)| (key, value))) + self.extend(iterable.into_iter().map(|(&key, &value)| (key, value))); } } @@ -1481,7 +1481,7 @@ mod tests { mem::size_of::() // hash ) + // buckets mem::size_of::() // entries.length - ) + ); } #[test] @@ -1673,7 +1673,7 @@ mod tests { assert_eq!(value, *v.insert(value).unwrap()); } }; - assert_eq!(value, *src.get(&key).unwrap()) + assert_eq!(value, *src.get(&key).unwrap()); } #[test] @@ -1693,7 +1693,7 @@ mod tests { panic!("Entry not found"); } }; - assert_eq!(value2, *src.get(&key).unwrap()) + assert_eq!(value2, *src.get(&key).unwrap()); } #[test] diff --git a/src/indexset.rs b/src/indexset.rs index 1a322d4659..438fab80b4 100644 --- a/src/indexset.rs +++ b/src/indexset.rs @@ -212,7 +212,7 @@ impl IndexSet { /// assert!(v.is_empty()); /// ``` pub fn clear(&mut self) { - self.map.clear() + self.map.clear(); } } @@ -560,7 +560,7 @@ where where I: IntoIterator, { - self.map.extend(iterable.into_iter().map(|k| (k, ()))) + self.map.extend(iterable.into_iter().map(|k| (k, ()))); } } @@ -573,7 +573,7 @@ where where I: IntoIterator, { - self.extend(iterable.into_iter().cloned()) + self.extend(iterable.into_iter().cloned()); } } diff --git a/src/lib.rs b/src/lib.rs index 7bc0cc8501..38a0fee209 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -147,6 +147,7 @@ clippy::option_if_let_else, clippy::ptr_as_ptr, clippy::doc_markdown, + clippy::semicolon_if_nothing_returned )] pub use binary_heap::BinaryHeap; diff --git a/src/linear_map.rs b/src/linear_map.rs index 718eeff8f7..ec67ba9e91 100644 --- a/src/linear_map.rs +++ b/src/linear_map.rs @@ -85,7 +85,7 @@ where /// assert!(map.is_empty()); /// ``` pub fn clear(&mut self) { - self.buffer.clear() + self.buffer.clear(); } /// Returns true if the map contains a value for the specified key. diff --git a/src/pool/arc.rs b/src/pool/arc.rs index 572da2ff70..7007236b4c 100644 --- a/src/pool/arc.rs +++ b/src/pool/arc.rs @@ -148,7 +148,7 @@ pub trait ArcPool: Sized { /// Add a statically allocated memory block to the memory pool fn manage(block: &'static mut ArcBlock) { - Self::singleton().manage(block) + Self::singleton().manage(block); } } @@ -315,7 +315,7 @@ where where H: Hasher, { - (**self).hash(state) + (**self).hash(state); } } diff --git a/src/pool/boxed.rs b/src/pool/boxed.rs index 807b6748f3..472840fd6f 100644 --- a/src/pool/boxed.rs +++ b/src/pool/boxed.rs @@ -166,7 +166,7 @@ pub trait BoxPool: Sized { /// Add a statically allocated memory block to the memory pool fn manage(block: &'static mut BoxBlock) { - Self::singleton().manage(block) + Self::singleton().manage(block); } } @@ -259,7 +259,7 @@ where where H: Hasher, { - (**self).hash(state) + (**self).hash(state); } } diff --git a/src/pool/object.rs b/src/pool/object.rs index 96b107939e..1a3f21b3b4 100644 --- a/src/pool/object.rs +++ b/src/pool/object.rs @@ -135,7 +135,7 @@ pub trait ObjectPool: Sized { /// Adds a statically allocate object to the pool fn manage(block: &'static mut ObjectBlock) { - Self::singleton().manage(block) + Self::singleton().manage(block); } } @@ -262,7 +262,7 @@ where where H: Hasher, { - (**self).hash(state) + (**self).hash(state); } } diff --git a/src/pool/treiber.rs b/src/pool/treiber.rs index 15fb05b763..922520a4e0 100644 --- a/src/pool/treiber.rs +++ b/src/pool/treiber.rs @@ -27,7 +27,7 @@ where /// - `node` must be a valid pointer /// - aliasing rules must be enforced by the caller. e.g, the same `node` may not be pushed more than once pub unsafe fn push(&self, node: NonNullPtr) { - impl_::push(self, node) + impl_::push(self, node); } pub fn try_pop(&self) -> Option> { diff --git a/src/pool/treiber/cas.rs b/src/pool/treiber/cas.rs index ab426c0571..2749cc66ac 100644 --- a/src/pool/treiber/cas.rs +++ b/src/pool/treiber/cas.rs @@ -87,7 +87,7 @@ where #[inline] fn store(&self, value: Option>, order: Ordering) { self.inner - .store(value.map(NonNullPtr::into_inner).unwrap_or_default(), order) + .store(value.map(NonNullPtr::into_inner).unwrap_or_default(), order); } } diff --git a/src/sorted_linked_list.rs b/src/sorted_linked_list.rs index 2c12ee8221..62bb3d5f58 100644 --- a/src/sorted_linked_list.rs +++ b/src/sorted_linked_list.rs @@ -770,7 +770,7 @@ where /// ``` #[inline] pub fn finish(self) { - drop(self) + drop(self); } } @@ -925,14 +925,14 @@ mod tests { ll.push(2).unwrap(); ll.push(3).unwrap(); - assert!(ll.is_full()) + assert!(ll.is_full()); } #[test] fn test_empty() { let ll: SortedLinkedList = SortedLinkedList::new_usize(); - assert!(ll.is_empty()) + assert!(ll.is_empty()); } #[test] diff --git a/src/spsc.rs b/src/spsc.rs index c1b94c141f..1c53f74866 100644 --- a/src/spsc.rs +++ b/src/spsc.rs @@ -308,7 +308,7 @@ impl QueueInner { /// to create a copy of `item`, which could result in `T`'s destructor running on `item` /// twice. pub unsafe fn enqueue_unchecked(&mut self, val: T) { - self.inner_enqueue_unchecked(val) + self.inner_enqueue_unchecked(val); } // The memory for dequeuing is "owned" by the head pointer,. @@ -678,7 +678,7 @@ impl ProducerInner<'_, T, S> { /// See [`Queue::enqueue_unchecked`] #[inline] pub unsafe fn enqueue_unchecked(&mut self, val: T) { - self.rb.inner_enqueue_unchecked(val) + self.rb.inner_enqueue_unchecked(val); } /// Returns if there is any space to enqueue a new item. When this returns true, at diff --git a/src/string/mod.rs b/src/string/mod.rs index 6aee8f270f..d8d4ec2aa7 100644 --- a/src/string/mod.rs +++ b/src/string/mod.rs @@ -519,7 +519,7 @@ impl + ?Sized> StringInner { pub fn truncate(&mut self, new_len: usize) { if new_len <= self.len() { assert!(self.is_char_boundary(new_len)); - self.vec.truncate(new_len) + self.vec.truncate(new_len); } } @@ -619,7 +619,7 @@ impl + ?Sized> StringInner { /// ``` #[inline] pub fn clear(&mut self) { - self.vec.clear() + self.vec.clear(); } } @@ -701,7 +701,7 @@ impl + ?Sized> fmt::Display for StringInner { impl + ?Sized> hash::Hash for StringInner { #[inline] fn hash(&self, hasher: &mut H) { - ::hash(self, hasher) + ::hash(self, hasher); } } @@ -1153,26 +1153,26 @@ mod tests { let number = 5; let float = 3.12; let formatted = format!(15; "{:0>3} plus {float}", number).unwrap(); - assert_eq!(formatted, "005 plus 3.12") + assert_eq!(formatted, "005 plus 3.12"); } #[test] fn format_inferred_capacity() { let number = 5; let float = 3.12; let formatted: String<15> = format!("{:0>3} plus {float}", number).unwrap(); - assert_eq!(formatted, "005 plus 3.12") + assert_eq!(formatted, "005 plus 3.12"); } #[test] fn format_overflow() { let i = 1234567; let formatted = format!(4; "13{}", i); - assert_eq!(formatted, Err(core::fmt::Error)) + assert_eq!(formatted, Err(core::fmt::Error)); } #[test] fn format_plain_string_overflow() { let formatted = format!(2; "123"); - assert_eq!(formatted, Err(core::fmt::Error)) + assert_eq!(formatted, Err(core::fmt::Error)); } } diff --git a/src/vec/mod.rs b/src/vec/mod.rs index c47b74e9fe..edf9840a24 100644 --- a/src/vec/mod.rs +++ b/src/vec/mod.rs @@ -497,7 +497,7 @@ impl + ?Sized> VecInner { I: IntoIterator, { for elem in iter { - self.push(elem).ok().unwrap() + self.push(elem).ok().unwrap(); } } @@ -753,7 +753,7 @@ impl + ?Sized> VecInner { pub unsafe fn set_len(&mut self, new_len: usize) { debug_assert!(new_len <= self.capacity()); - self.len = new_len + self.len = new_len; } /// Removes an element from the vector and returns it. @@ -1223,7 +1223,7 @@ impl + ?Sized> Extend for VecInner { where I: IntoIterator, { - self.extend(iter) + self.extend(iter); } } @@ -1235,7 +1235,7 @@ where where I: IntoIterator, { - self.extend(iter.into_iter().cloned()) + self.extend(iter.into_iter().cloned()); } } @@ -1244,7 +1244,7 @@ where T: core::hash::Hash, { fn hash(&self, state: &mut H) { - <[T] as hash::Hash>::hash(self, state) + <[T] as hash::Hash>::hash(self, state); } } diff --git a/tests/tsan.rs b/tests/tsan.rs index f1657fa4ce..0d8c0a4d0f 100644 --- a/tests/tsan.rs +++ b/tests/tsan.rs @@ -229,5 +229,5 @@ fn iterator_properly_wraps() { for (idx, el) in rb.iter().enumerate() { actual[idx] = *el; } - assert_eq!(expected, actual) + assert_eq!(expected, actual); } From 6db54e57dcc4551a2ec2d859708ce9b1ace5b9e0 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 24 Mar 2025 17:38:57 -0500 Subject: [PATCH 4/5] Fix `clippy::if_not_else` warnings --- src/histbuf.rs | 6 +++--- src/lib.rs | 3 ++- src/sorted_linked_list.rs | 20 ++++++++++---------- src/spsc.rs | 12 ++++++------ 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/histbuf.rs b/src/histbuf.rs index cedd9eb93b..9d105ed6db 100644 --- a/src/histbuf.rs +++ b/src/histbuf.rs @@ -464,10 +464,10 @@ impl + ?Sized> HistoryBufferInner { pub fn as_slices(&self) -> (&[T], &[T]) { let buffer = self.as_slice(); - if !self.filled { - (buffer, &[]) - } else { + if self.filled { (&buffer[self.write_at..], &buffer[..self.write_at]) + } else { + (buffer, &[]) } } diff --git a/src/lib.rs b/src/lib.rs index 38a0fee209..90454b2ca8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -147,7 +147,8 @@ clippy::option_if_let_else, clippy::ptr_as_ptr, clippy::doc_markdown, - clippy::semicolon_if_nothing_returned + clippy::semicolon_if_nothing_returned, + clippy::if_not_else )] pub use binary_heap::BinaryHeap; diff --git a/src/sorted_linked_list.rs b/src/sorted_linked_list.rs index 62bb3d5f58..22f188809a 100644 --- a/src/sorted_linked_list.rs +++ b/src/sorted_linked_list.rs @@ -364,11 +364,8 @@ where if self .read_data_in_node_at(head) .cmp(self.read_data_in_node_at(new)) - != K::ordering() + == K::ordering() { - self.node_at_mut(new).next = self.head; - self.head = Idx::new_unchecked(new); - } else { // It's not head, search the list for the correct placement let mut current = head; @@ -386,6 +383,9 @@ where self.node_at_mut(new).next = self.node_at(current).next; self.node_at_mut(current).next = Idx::new_unchecked(new); + } else { + self.node_at_mut(new).next = self.head; + self.head = Idx::new_unchecked(new); } } else { self.node_at_mut(new).next = self.head; @@ -417,11 +417,11 @@ where /// assert_eq!(ll.push(4), Err(4)); /// ``` pub fn push(&mut self, value: T) -> Result<(), T> { - if !self.is_full() { + if self.is_full() { + Err(value) + } else { unsafe { self.push_unchecked(value) } Ok(()) - } else { - Err(value) } } @@ -572,10 +572,10 @@ where /// assert_eq!(ll.pop(), None); /// ``` pub fn pop(&mut self) -> Option { - if !self.is_empty() { - Some(unsafe { self.pop_unchecked() }) - } else { + if self.is_empty() { None + } else { + Some(unsafe { self.pop_unchecked() }) } } diff --git a/src/spsc.rs b/src/spsc.rs index 1c53f74866..e00b5f78ee 100644 --- a/src/spsc.rs +++ b/src/spsc.rs @@ -263,11 +263,11 @@ impl QueueInner { /// assert_eq!(None, consumer.peek()); /// ``` pub fn peek(&self) -> Option<&T> { - if !self.is_empty() { + if self.is_empty() { + None + } else { let head = self.head.load(Ordering::Relaxed); Some(unsafe { &*(self.buffer.borrow().get_unchecked(head).get() as *const T) }) - } else { - None } } @@ -278,13 +278,13 @@ impl QueueInner { let current_tail = self.tail.load(Ordering::Relaxed); let next_tail = self.increment(current_tail); - if next_tail != self.head.load(Ordering::Acquire) { + if next_tail == self.head.load(Ordering::Acquire) { + Err(val) + } else { (self.buffer.borrow().get_unchecked(current_tail).get()).write(MaybeUninit::new(val)); self.tail.store(next_tail, Ordering::Release); Ok(()) - } else { - Err(val) } } From 2c6c169bf0f0dc1b0a4905bea741ae28b1f7aa78 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 24 Mar 2025 17:43:11 -0500 Subject: [PATCH 5/5] Fix `clippy::ref_as_ptr` warnings --- src/lib.rs | 1 + src/vec/drain.rs | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 90454b2ca8..f5e4bfbeb3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -146,6 +146,7 @@ clippy::redundant_pub_crate, clippy::option_if_let_else, clippy::ptr_as_ptr, + clippy::ref_as_ptr, clippy::doc_markdown, clippy::semicolon_if_nothing_returned, clippy::if_not_else diff --git a/src/vec/drain.rs b/src/vec/drain.rs index d43355fd7a..5ba523fdfe 100644 --- a/src/vec/drain.rs +++ b/src/vec/drain.rs @@ -73,7 +73,7 @@ impl Iterator for Drain<'_, T> { fn next(&mut self) -> Option { self.iter .next() - .map(|elt| unsafe { ptr::read(elt as *const _) }) + .map(|elt| unsafe { ptr::read(core::ptr::from_ref(elt)) }) } fn size_hint(&self) -> (usize, Option) { @@ -86,7 +86,7 @@ impl DoubleEndedIterator for Drain<'_, T> { fn next_back(&mut self) -> Option { self.iter .next_back() - .map(|elt| unsafe { ptr::read(elt as *const _) }) + .map(|elt| unsafe { ptr::read(core::ptr::from_ref(elt)) }) } }