Skip to content

Commit d9b06bd

Browse files
authored
Merge pull request #412 from newAM/fix-clippy
Fix clippy lints
2 parents ee14cda + d7306db commit d9b06bd

15 files changed

+120
-66
lines changed

.github/workflows/build.yml

+12
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ jobs:
101101
- name: cargo fmt --check
102102
run: cargo fmt --all -- --check
103103

104+
clippy:
105+
runs-on: ubuntu-latest
106+
steps:
107+
- name: Checkout
108+
uses: actions/checkout@v4
109+
- name: Install Rust
110+
uses: dtolnay/rust-toolchain@stable
111+
with:
112+
components: clippy
113+
targets: i686-unknown-linux-gnu
114+
- run: cargo clippy --all --target i686-unknown-linux-gnu -- --deny warnings
115+
104116
# Compilation check
105117
check:
106118
name: check

CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10-
- configure `stable_deref_trait` as a platform-dependent dependency
10+
### Changed
11+
12+
- Changed `stable_deref_trait` to a platform-dependent dependency.
13+
14+
### Fixed
15+
16+
- Fixed clippy lints.
1117

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

src/binary_heap.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ where
233233
/// assert_eq!(heap.peek(), Some(&5));
234234
/// ```
235235
pub fn peek(&self) -> Option<&T> {
236-
self.data.as_slice().get(0)
236+
self.data.as_slice().first()
237237
}
238238

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

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

@@ -330,6 +331,7 @@ where
330331
}
331332

332333
/// Pushes an item onto the binary heap without first checking if it's full.
334+
#[allow(clippy::missing_safety_doc)] // TODO
333335
pub unsafe fn push_unchecked(&mut self, item: T) {
334336
let old_len = self.len();
335337
self.data.push_unchecked(item);

src/deque.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ impl<T, const N: usize> Deque<T, N> {
283283
let index = self.front;
284284
self.full = false;
285285
self.front = Self::increment(self.front);
286-
(self.buffer.get_unchecked_mut(index).as_ptr() as *const T).read()
286+
self.buffer.get_unchecked_mut(index).as_ptr().read()
287287
}
288288

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

298298
self.full = false;
299299
self.back = Self::decrement(self.back);
300-
(self.buffer.get_unchecked_mut(self.back).as_ptr() as *const T).read()
300+
self.buffer.get_unchecked_mut(self.back).as_ptr().read()
301301
}
302302

303303
/// Appends an `item` to the front of the deque

src/histbuf.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,21 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
117117
}
118118
}
119119

120+
/// Returns true if the buffer is empty.
121+
///
122+
/// # Examples
123+
///
124+
/// ```
125+
/// use heapless::HistoryBuffer;
126+
///
127+
/// let x: HistoryBuffer<u8, 16> = HistoryBuffer::new();
128+
/// assert!(x.is_empty());
129+
/// ```
130+
#[inline]
131+
pub fn is_empty(&self) -> bool {
132+
self.len() == 0
133+
}
134+
120135
/// Returns the capacity of the buffer, which is the length of the
121136
/// underlying backing array.
122137
#[inline]
@@ -219,7 +234,7 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
219234
/// }
220235
///
221236
/// ```
222-
pub fn oldest_ordered<'a>(&'a self) -> OldestOrdered<'a, T, N> {
237+
pub fn oldest_ordered(&self) -> OldestOrdered<'_, T, N> {
223238
if self.filled {
224239
OldestOrdered {
225240
buf: self,

src/indexmap.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use core::{
22
borrow::Borrow,
33
fmt,
4-
hash::{BuildHasher, Hash, Hasher as _},
4+
hash::{BuildHasher, Hash},
55
iter::FromIterator,
66
mem,
77
num::NonZeroU32,
@@ -64,7 +64,7 @@ impl HashValue {
6464
}
6565

6666
fn probe_distance(&self, mask: usize, current: usize) -> usize {
67-
current.wrapping_sub(self.desired_pos(mask) as usize) & mask
67+
current.wrapping_sub(self.desired_pos(mask)) & mask
6868
}
6969
}
7070

@@ -364,7 +364,7 @@ where
364364
fn clone(&self) -> Self {
365365
Self {
366366
entries: self.entries.clone(),
367-
indices: self.indices.clone(),
367+
indices: self.indices,
368368
}
369369
}
370370
}
@@ -961,7 +961,7 @@ where
961961
K: Borrow<Q>,
962962
Q: ?Sized + Hash + Eq,
963963
{
964-
if self.len() == 0 {
964+
if self.is_empty() {
965965
return None;
966966
}
967967
let h = hash_with(key, &self.build_hasher);
@@ -1238,9 +1238,7 @@ where
12381238
K: ?Sized + Hash,
12391239
S: BuildHasher,
12401240
{
1241-
let mut h = build_hasher.build_hasher();
1242-
key.hash(&mut h);
1243-
HashValue(h.finish() as u16)
1241+
HashValue(build_hasher.hash_one(key) as u16)
12441242
}
12451243

12461244
#[cfg(test)]

src/linear_map.rs

+1
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ pub struct Iter<'a, K, V> {
448448
impl<'a, K, V> Iterator for Iter<'a, K, V> {
449449
type Item = (&'a K, &'a V);
450450

451+
#[allow(clippy::needless_borrowed_reference)]
451452
fn next(&mut self) -> Option<Self::Item> {
452453
self.iter.next().map(|&(ref k, ref v)| (k, v))
453454
}

src/mpmc.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ impl<T, const N: usize> MpMcQueue<T, N> {
145145
crate::sealed::power_of_two::<N>();
146146

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

150151
let mut cell_count = 0;
151152

@@ -204,6 +205,7 @@ impl<T> Cell<T> {
204205
}
205206
}
206207

208+
#[allow(clippy::comparison_chain)]
207209
unsafe fn dequeue<T>(
208210
buffer: *mut Cell<T>,
209211
dequeue_pos: &AtomicTargetSize,
@@ -243,6 +245,7 @@ unsafe fn dequeue<T>(
243245
Some(data)
244246
}
245247

248+
#[allow(clippy::comparison_chain)]
246249
unsafe fn enqueue<T>(
247250
buffer: *mut Cell<T>,
248251
enqueue_pos: &AtomicTargetSize,

src/pool/arc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ where
221221
P: ArcPool,
222222
{
223223
fn as_ref(&self) -> &P::Data {
224-
&**self
224+
self
225225
}
226226
}
227227

src/sealed.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
1-
#[allow(dead_code)]
2-
#[allow(path_statements)]
1+
#[allow(dead_code, path_statements, clippy::no_effect)]
32
pub(crate) const fn smaller_than<const N: usize, const MAX: usize>() {
43
Assert::<N, MAX>::LESS;
54
}
65

7-
#[allow(dead_code)]
8-
#[allow(path_statements)]
6+
#[allow(dead_code, path_statements, clippy::no_effect)]
97
pub(crate) const fn greater_than_eq_0<const N: usize>() {
108
Assert::<N, 0>::GREATER_EQ;
119
}
1210

13-
#[allow(dead_code)]
14-
#[allow(path_statements)]
11+
#[allow(dead_code, path_statements, clippy::no_effect)]
1512
pub(crate) const fn greater_than_0<const N: usize>() {
1613
Assert::<N, 0>::GREATER;
1714
}
1815

19-
#[allow(dead_code)]
20-
#[allow(path_statements)]
16+
#[allow(dead_code, path_statements, clippy::no_effect)]
2117
pub(crate) const fn greater_than_1<const N: usize>() {
2218
Assert::<N, 1>::GREATER;
2319
}
2420

25-
#[allow(dead_code)]
26-
#[allow(path_statements)]
21+
#[allow(dead_code, path_statements, clippy::no_effect)]
2722
pub(crate) const fn power_of_two<const N: usize>() {
2823
Assert::<N, 0>::GREATER;
2924
Assert::<N, 0>::POWER_OF_TWO;
@@ -42,6 +37,7 @@ impl<const L: usize, const R: usize> Assert<L, R> {
4237
pub const LESS_EQ: usize = R - L;
4338

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

4743
/// Const assert hack

src/sorted_linked_list.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ where
310310
/// ```
311311
pub fn push(&mut self, value: T) -> Result<(), T> {
312312
if !self.is_full() {
313-
Ok(unsafe { self.push_unchecked(value) })
313+
unsafe { self.push_unchecked(value) }
314+
Ok(())
314315
} else {
315316
Err(value)
316317
}
@@ -462,6 +463,7 @@ where
462463
/// assert_eq!(ll.pop(), Ok(1));
463464
/// assert_eq!(ll.pop(), Err(()));
464465
/// ```
466+
#[allow(clippy::result_unit_err)]
465467
pub fn pop(&mut self) -> Result<T, ()> {
466468
if !self.is_empty() {
467469
Ok(unsafe { self.pop_unchecked() })

src/spsc.rs

+43-8
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ impl<T, const N: usize> Queue<T, N> {
251251

252252
/// Adds an `item` to the end of the queue, without checking if it's full
253253
///
254-
/// # Unsafety
254+
/// # Safety
255255
///
256256
/// If the queue is full this operation will leak a value (T's destructor won't run on
257257
/// the value that got overwritten by `item`), *and* will allow the `dequeue` operation
@@ -295,7 +295,7 @@ impl<T, const N: usize> Queue<T, N> {
295295
/// Returns the item in the front of the queue, without checking if there is something in the
296296
/// queue
297297
///
298-
/// # Unsafety
298+
/// # Safety
299299
///
300300
/// If the queue is empty this operation will return uninitialized memory.
301301
pub unsafe fn dequeue_unchecked(&mut self) -> T {
@@ -507,7 +507,9 @@ impl<'a, T, const N: usize> Consumer<'a, T, N> {
507507
/// Returns the item in the front of the queue, without checking if there are elements in the
508508
/// queue
509509
///
510-
/// See [`Queue::dequeue_unchecked`] for safety
510+
/// # Safety
511+
///
512+
/// See [`Queue::dequeue_unchecked`]
511513
#[inline]
512514
pub unsafe fn dequeue_unchecked(&mut self) -> T {
513515
self.rb.inner_dequeue_unchecked()
@@ -526,6 +528,22 @@ impl<'a, T, const N: usize> Consumer<'a, T, N> {
526528
self.rb.len()
527529
}
528530

531+
/// Returns true if the queue is empty
532+
///
533+
/// # Examples
534+
///
535+
/// ```
536+
/// use heapless::spsc::Queue;
537+
///
538+
/// let mut queue: Queue<u8, 235> = Queue::new();
539+
/// let (mut producer, mut consumer) = queue.split();
540+
/// assert!(consumer.is_empty());
541+
/// ```
542+
#[inline]
543+
pub fn is_empty(&self) -> bool {
544+
self.len() == 0
545+
}
546+
529547
/// Returns the maximum number of elements the queue can hold
530548
#[inline]
531549
pub fn capacity(&self) -> usize {
@@ -536,6 +554,7 @@ impl<'a, T, const N: usize> Consumer<'a, T, N> {
536554
/// empty
537555
///
538556
/// # Examples
557+
///
539558
/// ```
540559
/// use heapless::spsc::Queue;
541560
///
@@ -562,7 +581,9 @@ impl<'a, T, const N: usize> Producer<'a, T, N> {
562581

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

605+
/// Returns true if the queue is empty
606+
///
607+
/// # Examples
608+
///
609+
/// ```
610+
/// use heapless::spsc::Queue;
611+
///
612+
/// let mut queue: Queue<u8, 235> = Queue::new();
613+
/// let (mut producer, mut consumer) = queue.split();
614+
/// assert!(producer.is_empty());
615+
/// ```
616+
#[inline]
617+
pub fn is_empty(&self) -> bool {
618+
self.len() == 0
619+
}
620+
584621
/// Returns the maximum number of elements the queue can hold
585622
#[inline]
586623
pub fn capacity(&self) -> usize {
@@ -894,14 +931,12 @@ mod tests {
894931
let hash1 = {
895932
let mut hasher1 = hash32::FnvHasher::default();
896933
rb1.hash(&mut hasher1);
897-
let hash1 = hasher1.finish();
898-
hash1
934+
hasher1.finish()
899935
};
900936
let hash2 = {
901937
let mut hasher2 = hash32::FnvHasher::default();
902938
rb2.hash(&mut hasher2);
903-
let hash2 = hasher2.finish();
904-
hash2
939+
hasher2.finish()
905940
};
906941
assert_eq!(hash1, hash2);
907942
}

0 commit comments

Comments
 (0)