Skip to content

Commit

Permalink
Fix Clippy lints and enable on CI (#44)
Browse files Browse the repository at this point in the history
* Run Clippy on CI

* Clippy and Rustdoc cleanups
  • Loading branch information
michaelsproul authored Feb 4, 2025
1 parent d6819f6 commit a051ff7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,28 @@ jobs:
name: cargo-fmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Get latest version of stable Rust
run: rustup update stable
- name: Check formatting with cargo fmt
run: cargo fmt --all -- --check
clippy:
name: clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Get latest version of stable Rust
run: rustup update stable
- name: Lint code for quality and style with Clippy
run: cargo clippy --all
test:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
name: test-${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Get latest version of stable Rust
run: rustup update stable
- name: Run tests
Expand All @@ -37,7 +46,7 @@ jobs:
runs-on: ubuntu-latest
name: cargo-tarpaulin
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Get latest version of stable Rust
run: rustup update stable
- name: Install cargo-tarpaulin
Expand Down
14 changes: 7 additions & 7 deletions ssz/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ pub enum DecodeError {
/// An offset points “backwards” into the fixed-bytes portion of the message, essentially
/// double-decoding bytes that will also be decoded as fixed-length.
///
/// https://notes.ethereum.org/ruKvDXl6QOW3gnqVYb8ezA?view#1-Offset-into-fixed-portion
/// <https://notes.ethereum.org/ruKvDXl6QOW3gnqVYb8ezA?view#1-Offset-into-fixed-portion>
OffsetIntoFixedPortion(usize),
/// The first offset does not point to the byte that follows the fixed byte portion,
/// essentially skipping a variable-length byte.
///
/// https://notes.ethereum.org/ruKvDXl6QOW3gnqVYb8ezA?view#2-Skip-first-variable-byte
/// <https://notes.ethereum.org/ruKvDXl6QOW3gnqVYb8ezA?view#2-Skip-first-variable-byte>
OffsetSkipsVariableBytes(usize),
/// An offset points to bytes prior to the previous offset. Depending on how you look at it,
/// this either double-decodes bytes or makes the first offset a negative-length.
///
/// https://notes.ethereum.org/ruKvDXl6QOW3gnqVYb8ezA?view#3-Offsets-are-decreasing
/// <https://notes.ethereum.org/ruKvDXl6QOW3gnqVYb8ezA?view#3-Offsets-are-decreasing>
OffsetsAreDecreasing(usize),
/// An offset references byte indices that do not exist in the source bytes.
///
/// https://notes.ethereum.org/ruKvDXl6QOW3gnqVYb8ezA?view#4-Offsets-are-out-of-bounds
/// <https://notes.ethereum.org/ruKvDXl6QOW3gnqVYb8ezA?view#4-Offsets-are-out-of-bounds>
OffsetOutOfBounds(usize),
/// A variable-length list does not have a fixed portion that is cleanly divisible by
/// `BYTES_PER_LENGTH_OFFSET`.
Expand Down Expand Up @@ -78,15 +78,15 @@ pub fn sanitize_offset(
num_bytes: usize,
num_fixed_bytes: Option<usize>,
) -> Result<usize, DecodeError> {
if num_fixed_bytes.map_or(false, |fixed_bytes| offset < fixed_bytes) {
if num_fixed_bytes.is_some_and(|fixed_bytes| offset < fixed_bytes) {
Err(DecodeError::OffsetIntoFixedPortion(offset))
} else if previous_offset.is_none()
&& num_fixed_bytes.map_or(false, |fixed_bytes| offset != fixed_bytes)
&& num_fixed_bytes.is_some_and(|fixed_bytes| offset != fixed_bytes)
{
Err(DecodeError::OffsetSkipsVariableBytes(offset))
} else if offset > num_bytes {
Err(DecodeError::OffsetOutOfBounds(offset))
} else if previous_offset.map_or(false, |prev| prev > offset) {
} else if previous_offset.is_some_and(|prev| prev > offset) {
Err(DecodeError::OffsetsAreDecreasing(offset))
} else {
Ok(offset)
Expand Down
2 changes: 1 addition & 1 deletion ssz/src/decode/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ pub fn decode_list_of_variable_length_items<T: Decode, Container: TryFromIter<T>

let num_items = first_offset / BYTES_PER_LENGTH_OFFSET;

if max_len.map_or(false, |max| num_items > max) {
if max_len.is_some_and(|max| num_items > max) {
return Err(DecodeError::BytesInvalid(format!(
"Variable length list of {} items exceeds maximum of {:?}",
num_items, max_len
Expand Down
2 changes: 1 addition & 1 deletion ssz/src/union_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::*;

/// Provides the one-byte "selector" from the SSZ union specification:
///
/// https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.3/ssz/simple-serialize.md#union
/// <https://github.com/ethereum/consensus-specs/blob/v1.1.0-beta.3/ssz/simple-serialize.md#union>
#[derive(Copy, Clone)]
pub struct UnionSelector(u8);

Expand Down
17 changes: 7 additions & 10 deletions ssz_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![recursion_limit = "256"]
//! Provides procedural derive macros for the `Encode` and `Decode` traits of the `eth2_ssz` crate.
//! Provides procedural derive macros for the `Encode` and `Decode` traits of the `ethereum_ssz`
//! crate.
//!
//! ## Attributes
//!
Expand Down Expand Up @@ -288,11 +289,7 @@ fn parse_ssz_fields(
let field_opts_candidates = field
.attrs
.iter()
.filter(|attr| {
attr.path()
.get_ident()
.map_or(false, |ident| *ident == "ssz")
})
.filter(|attr| attr.path().get_ident().is_some_and(|ident| *ident == "ssz"))
.collect::<Vec<_>>();

if field_opts_candidates.len() > 1 {
Expand Down Expand Up @@ -753,8 +750,8 @@ pub fn ssz_decode_derive(input: TokenStream) -> TokenStream {
/// ## Field attributes
///
/// - `#[ssz(skip_deserializing)]`: during de-serialization the field will be instantiated from a
/// `Default` implementation. The decoder will assume that the field was not serialized at all
/// (e.g., if it has been serialized, an error will be raised instead of `Default` overriding it).
/// `Default` implementation. The decoder will assume that the field was not serialized at all
/// (e.g., if it has been serialized, an error will be raised instead of `Default` overriding it).
fn ssz_decode_derive_struct(item: &DeriveInput, struct_data: &DataStruct) -> TokenStream {
let name = &item.ident;
let (impl_generics, ty_generics, where_clause) = &item.generics.split_for_impl();
Expand Down Expand Up @@ -903,8 +900,8 @@ fn ssz_decode_derive_struct(item: &DeriveInput, struct_data: &DataStruct) -> Tok
/// ## Field attributes
///
/// - `#[ssz(skip_deserializing)]`: during de-serialization the field will be instantiated from a
/// `Default` implementation. The decoder will assume that the field was not serialized at all
/// (e.g., if it has been serialized, an error will be raised instead of `Default` overriding it).
/// `Default` implementation. The decoder will assume that the field was not serialized at all
/// (e.g., if it has been serialized, an error will be raised instead of `Default` overriding it).
fn ssz_decode_derive_struct_transparent(
item: &DeriveInput,
struct_data: &DataStruct,
Expand Down

0 comments on commit a051ff7

Please sign in to comment.