Skip to content

Commit

Permalink
Fix clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
newAM committed Nov 15, 2023
1 parent ee14cda commit 4f5938e
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 66 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ jobs:
- name: cargo fmt --check
run: cargo fmt --all -- --check

clippy:
runs-on: ubuntu-latest
- name: Checkout
uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
components: clippy
targets: i686-unknown-linux-gnu
- run: cargo clippy --all --target i686-unknown-linux-gnu -- --deny warnings

# Compilation check
check:
name: check
Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

- configure `stable_deref_trait` as a platform-dependent dependency
### Changed

- Changed `stable_deref_trait` to a platform-dependent dependency.

### Fixed

- Fixed clippy lints.

## [v0.8.0] - 2023-11-07

Expand Down
4 changes: 3 additions & 1 deletion src/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ where
/// assert_eq!(heap.peek(), Some(&5));
/// ```
pub fn peek(&self) -> Option<&T> {
self.data.as_slice().get(0)
self.data.as_slice().first()
}

/// Returns a mutable reference to the greatest item in the binary heap, or
Expand Down Expand Up @@ -297,6 +297,7 @@ where

/// Removes the *top* (greatest if max-heap, smallest if min-heap) item from the binary heap and
/// returns it, without checking if the binary heap is empty.
#[allow(clippy::missing_safety_doc)] // TODO
pub unsafe fn pop_unchecked(&mut self) -> T {
let mut item = self.data.pop_unchecked();

Expand Down Expand Up @@ -330,6 +331,7 @@ where
}

/// Pushes an item onto the binary heap without first checking if it's full.
#[allow(clippy::missing_safety_doc)] // TODO
pub unsafe fn push_unchecked(&mut self, item: T) {
let old_len = self.len();
self.data.push_unchecked(item);
Expand Down
4 changes: 2 additions & 2 deletions src/deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl<T, const N: usize> Deque<T, N> {
let index = self.front;
self.full = false;
self.front = Self::increment(self.front);
(self.buffer.get_unchecked_mut(index).as_ptr() as *const T).read()
self.buffer.get_unchecked_mut(index).as_ptr().read()
}

/// Removes an item from the back of the deque and returns it, without checking that the deque
Expand All @@ -297,7 +297,7 @@ impl<T, const N: usize> Deque<T, N> {

self.full = false;
self.back = Self::decrement(self.back);
(self.buffer.get_unchecked_mut(self.back).as_ptr() as *const T).read()
self.buffer.get_unchecked_mut(self.back).as_ptr().read()
}

/// Appends an `item` to the front of the deque
Expand Down
17 changes: 16 additions & 1 deletion src/histbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
}
}

/// Returns true if the buffer is empty.
///
/// # Examples
///
/// ```
/// use heapless::HistoryBuffer;
///
/// let x: HistoryBuffer<u8, 16> = HistoryBuffer::new();
/// assert!(x.is_empty());
/// ```
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Returns the capacity of the buffer, which is the length of the
/// underlying backing array.
#[inline]
Expand Down Expand Up @@ -219,7 +234,7 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
/// }
///
/// ```
pub fn oldest_ordered<'a>(&'a self) -> OldestOrdered<'a, T, N> {
pub fn oldest_ordered(&self) -> OldestOrdered<'_, T, N> {
if self.filled {
OldestOrdered {
buf: self,
Expand Down
12 changes: 5 additions & 7 deletions src/indexmap.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::{
borrow::Borrow,
fmt,
hash::{BuildHasher, Hash, Hasher as _},
hash::{BuildHasher, Hash},
iter::FromIterator,
mem,
num::NonZeroU32,
Expand Down Expand Up @@ -64,7 +64,7 @@ impl HashValue {
}

fn probe_distance(&self, mask: usize, current: usize) -> usize {
current.wrapping_sub(self.desired_pos(mask) as usize) & mask
current.wrapping_sub(self.desired_pos(mask)) & mask
}
}

Expand Down Expand Up @@ -364,7 +364,7 @@ where
fn clone(&self) -> Self {
Self {
entries: self.entries.clone(),
indices: self.indices.clone(),
indices: self.indices,
}
}
}
Expand Down Expand Up @@ -961,7 +961,7 @@ where
K: Borrow<Q>,
Q: ?Sized + Hash + Eq,
{
if self.len() == 0 {
if self.is_empty() {
return None;
}
let h = hash_with(key, &self.build_hasher);
Expand Down Expand Up @@ -1238,9 +1238,7 @@ where
K: ?Sized + Hash,
S: BuildHasher,
{
let mut h = build_hasher.build_hasher();
key.hash(&mut h);
HashValue(h.finish() as u16)
HashValue(build_hasher.hash_one(key) as u16)
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions src/linear_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ pub struct Iter<'a, K, V> {
impl<'a, K, V> Iterator for Iter<'a, K, V> {
type Item = (&'a K, &'a V);

#[allow(clippy::needless_borrowed_reference)]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|&(ref k, ref v)| (k, v))
}
Expand Down
5 changes: 4 additions & 1 deletion src/mpmc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ impl<T, const N: usize> MpMcQueue<T, N> {
crate::sealed::power_of_two::<N>();

// Const assert on size.
Self::ASSERT[!(N < (IntSize::MAX as usize)) as usize];
#[allow(clippy::no_effect)]
Self::ASSERT[(N >= (IntSize::MAX as usize)) as usize];

let mut cell_count = 0;

Expand Down Expand Up @@ -204,6 +205,7 @@ impl<T> Cell<T> {
}
}

#[allow(clippy::comparison_chain)]
unsafe fn dequeue<T>(
buffer: *mut Cell<T>,
dequeue_pos: &AtomicTargetSize,
Expand Down Expand Up @@ -243,6 +245,7 @@ unsafe fn dequeue<T>(
Some(data)
}

#[allow(clippy::comparison_chain)]
unsafe fn enqueue<T>(
buffer: *mut Cell<T>,
enqueue_pos: &AtomicTargetSize,
Expand Down
2 changes: 1 addition & 1 deletion src/pool/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ where
P: ArcPool,
{
fn as_ref(&self) -> &P::Data {
&**self
self
}
}

Expand Down
16 changes: 6 additions & 10 deletions src/sealed.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
#[allow(dead_code)]
#[allow(path_statements)]
#[allow(dead_code, path_statements, clippy::no_effect)]
pub(crate) const fn smaller_than<const N: usize, const MAX: usize>() {
Assert::<N, MAX>::LESS;
}

#[allow(dead_code)]
#[allow(path_statements)]
#[allow(dead_code, path_statements, clippy::no_effect)]
pub(crate) const fn greater_than_eq_0<const N: usize>() {
Assert::<N, 0>::GREATER_EQ;
}

#[allow(dead_code)]
#[allow(path_statements)]
#[allow(dead_code, path_statements, clippy::no_effect)]
pub(crate) const fn greater_than_0<const N: usize>() {
Assert::<N, 0>::GREATER;
}

#[allow(dead_code)]
#[allow(path_statements)]
#[allow(dead_code, path_statements, clippy::no_effect)]
pub(crate) const fn greater_than_1<const N: usize>() {
Assert::<N, 1>::GREATER;
}

#[allow(dead_code)]
#[allow(path_statements)]
#[allow(dead_code, path_statements, clippy::no_effect)]
pub(crate) const fn power_of_two<const N: usize>() {
Assert::<N, 0>::GREATER;
Assert::<N, 0>::POWER_OF_TWO;
Expand All @@ -42,6 +37,7 @@ impl<const L: usize, const R: usize> Assert<L, R> {
pub const LESS_EQ: usize = R - L;

/// Const assert hack
#[allow(clippy::erasing_op)]
pub const NOT_EQ: isize = 0 / (R as isize - L as isize);

/// Const assert hack
Expand Down
4 changes: 3 additions & 1 deletion src/sorted_linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ where
/// ```
pub fn push(&mut self, value: T) -> Result<(), T> {
if !self.is_full() {
Ok(unsafe { self.push_unchecked(value) })
unsafe { self.push_unchecked(value) }
Ok(())
} else {
Err(value)
}
Expand Down Expand Up @@ -462,6 +463,7 @@ where
/// assert_eq!(ll.pop(), Ok(1));
/// assert_eq!(ll.pop(), Err(()));
/// ```
#[allow(clippy::result_unit_err)]
pub fn pop(&mut self) -> Result<T, ()> {
if !self.is_empty() {
Ok(unsafe { self.pop_unchecked() })
Expand Down
51 changes: 43 additions & 8 deletions src/spsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl<T, const N: usize> Queue<T, N> {

/// Adds an `item` to the end of the queue, without checking if it's full
///
/// # Unsafety
/// # Safety
///
/// If the queue is full this operation will leak a value (T's destructor won't run on
/// the value that got overwritten by `item`), *and* will allow the `dequeue` operation
Expand Down Expand Up @@ -295,7 +295,7 @@ impl<T, const N: usize> Queue<T, N> {
/// Returns the item in the front of the queue, without checking if there is something in the
/// queue
///
/// # Unsafety
/// # Safety
///
/// If the queue is empty this operation will return uninitialized memory.
pub unsafe fn dequeue_unchecked(&mut self) -> T {
Expand Down Expand Up @@ -507,7 +507,9 @@ impl<'a, T, const N: usize> Consumer<'a, T, N> {
/// Returns the item in the front of the queue, without checking if there are elements in the
/// queue
///
/// See [`Queue::dequeue_unchecked`] for safety
/// # Safety
///
/// See [`Queue::dequeue_unchecked`]
#[inline]
pub unsafe fn dequeue_unchecked(&mut self) -> T {
self.rb.inner_dequeue_unchecked()
Expand All @@ -526,6 +528,22 @@ impl<'a, T, const N: usize> Consumer<'a, T, N> {
self.rb.len()
}

/// Returns true if the queue is empty
///
/// # Examples
///
/// ```
/// use heapless::spsc::Queue;
///
/// let mut queue: Queue<u8, 235> = Queue::new();
/// let (mut producer, mut consumer) = queue.split();
/// assert!(consumer.is_empty());
/// ```
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Returns the maximum number of elements the queue can hold
#[inline]
pub fn capacity(&self) -> usize {
Expand All @@ -536,6 +554,7 @@ impl<'a, T, const N: usize> Consumer<'a, T, N> {
/// empty
///
/// # Examples
///
/// ```
/// use heapless::spsc::Queue;
///
Expand All @@ -562,7 +581,9 @@ impl<'a, T, const N: usize> Producer<'a, T, N> {

/// Adds an `item` to the end of the queue, without checking if the queue is full
///
/// See [`Queue::enqueue_unchecked`] for safety
/// # Safety
///
/// See [`Queue::enqueue_unchecked`]
#[inline]
pub unsafe fn enqueue_unchecked(&mut self, val: T) {
self.rb.inner_enqueue_unchecked(val)
Expand All @@ -581,6 +602,22 @@ impl<'a, T, const N: usize> Producer<'a, T, N> {
self.rb.len()
}

/// Returns true if the queue is empty
///
/// # Examples
///
/// ```
/// use heapless::spsc::Queue;
///
/// let mut queue: Queue<u8, 235> = Queue::new();
/// let (mut producer, mut consumer) = queue.split();
/// assert!(producer.is_empty());
/// ```
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Returns the maximum number of elements the queue can hold
#[inline]
pub fn capacity(&self) -> usize {
Expand Down Expand Up @@ -894,14 +931,12 @@ mod tests {
let hash1 = {
let mut hasher1 = hash32::FnvHasher::default();
rb1.hash(&mut hasher1);
let hash1 = hasher1.finish();
hash1
hasher1.finish()
};
let hash2 = {
let mut hasher2 = hash32::FnvHasher::default();
rb2.hash(&mut hasher2);
let hash2 = hasher2.finish();
hash2
hasher2.finish()
};
assert_eq!(hash1, hash2);
}
Expand Down
Loading

0 comments on commit 4f5938e

Please sign in to comment.