Skip to content

Add clippy to CI, address some non-default lints #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ jobs:
components: rustfmt
- name: cargo fmt --check
run: cargo fmt --all -- --check

clippy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- run: cargo clippy --all --all-targets -- --deny warnings
2 changes: 1 addition & 1 deletion src/fnv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Hasher {

impl Default for Hasher {
fn default() -> Self {
Hasher { state: BASIS }
Self { state: BASIS }
}
}

Expand Down
18 changes: 12 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
//!
//! This crate provides implementations of the following 32-bit hashing algorithms:
//!
//! - [Fowler-Noll-Vo](struct.FnvHasher.html)
//! - [MurmurHash3](struct.Murmur3Hasher.html)
//! - [`FnvHasher`] Fowler-Noll-Vo 1a
//! - [`Murmur3Hasher`] `MurmurHash3`
//!
//! # Generic code
//!
Expand All @@ -48,6 +48,12 @@

#![deny(missing_docs)]
#![deny(warnings)]
#![warn(
clippy::use_self,
clippy::doc_markdown,
clippy::ptr_as_ptr,
clippy::trivially_copy_pass_by_ref
)]
#![no_std]

use core::fmt;
Expand All @@ -72,20 +78,20 @@ pub struct BuildHasherDefault<H> {

impl<H> Default for BuildHasherDefault<H> {
fn default() -> Self {
BuildHasherDefault {
Self {
_marker: PhantomData,
}
}
}

impl<H> Clone for BuildHasherDefault<H> {
fn clone(&self) -> Self {
BuildHasherDefault::default()
Self::default()
}
}

impl<H> PartialEq for BuildHasherDefault<H> {
fn eq(&self, _other: &BuildHasherDefault<H>) -> bool {
fn eq(&self, _other: &Self) -> bool {
true
}
}
Expand All @@ -101,7 +107,7 @@ impl<H> fmt::Debug for BuildHasherDefault<H> {
impl<H> BuildHasherDefault<H> {
/// `const` constructor
pub const fn new() -> Self {
BuildHasherDefault {
Self {
_marker: PhantomData,
}
}
Expand Down
27 changes: 14 additions & 13 deletions src/murmur3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::slice;

use crate::Hasher as _;

/// 32-bit MurmurHash3 hasher
/// 32-bit `MurmurHash3` hasher
pub struct Hasher {
buf: Buffer,
index: Index,
Expand All @@ -28,23 +28,23 @@ enum Index {
}

impl Index {
fn usize(&self) -> usize {
match *self {
Index::_0 => 0,
Index::_1 => 1,
Index::_2 => 2,
Index::_3 => 3,
fn usize(self) -> usize {
match self {
Self::_0 => 0,
Self::_1 => 1,
Self::_2 => 2,
Self::_3 => 3,
}
}
}

impl From<usize> for Index {
fn from(x: usize) -> Self {
match x % 4 {
0 => Index::_0,
1 => Index::_1,
2 => Index::_2,
3 => Index::_3,
0 => Self::_0,
1 => Self::_1,
2 => Self::_2,
3 => Self::_3,
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -75,7 +75,7 @@ impl Hasher {
impl Default for Hasher {
#[allow(deprecated)]
fn default() -> Self {
Hasher {
Self {
buf: Buffer {
bytes: MaybeUninit::uninit(),
},
Expand Down Expand Up @@ -176,7 +176,7 @@ impl core::hash::Hasher for Hasher {
for block in body.chunks(4) {
if block.len() == 4 {
self.state
.process_block(unsafe { &*(block.as_ptr() as *const _) });
.process_block(unsafe { &*(block.as_ptr().cast()) });
} else {
// NOTE(unsafe) In this branch, `block.len() < 4`. For CASE 1 and CASE 2 above,
// `self.index.usize()` will be 0 here, so `self.index.usize() + block.len() < 4`.
Expand Down Expand Up @@ -209,6 +209,7 @@ const C2: u32 = 0x1b873593;
const R1: u32 = 15;

impl State {
#[allow(clippy::trivially_copy_pass_by_ref)]
fn process_block(&mut self, block: &MaybeUninit<[u8; 4]>) {
self.0 ^= pre_mix(u32::from_le_bytes(unsafe { *block.assume_init_ref() }));
self.0 = self.0.rotate_left(13);
Expand Down