Skip to content

Commit

Permalink
refactor: enable more clippy lints and unify them
Browse files Browse the repository at this point in the history
Ensuring consistent code quality through lints within all the crates
that are part of this project.
  • Loading branch information
dnaka91 committed Oct 23, 2023
1 parent 9137e20 commit 3c206de
Show file tree
Hide file tree
Showing 41 changed files with 233 additions and 76 deletions.
12 changes: 12 additions & 0 deletions crates/stef-benches/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
#![forbid(unsafe_code)]
#![deny(rust_2018_idioms, clippy::all)]
#![warn(clippy::pedantic)]
#![allow(
clippy::cast_possible_wrap,
clippy::cast_precision_loss,
clippy::cast_sign_loss,
clippy::missing_errors_doc,
clippy::missing_panics_doc
)]

use std::fmt::Write;

pub mod varint;

#[must_use]
pub fn generate_schema(count: usize) -> String {
let mut input = String::new();

Expand Down
14 changes: 13 additions & 1 deletion crates/stef-benches/src/varint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ pub mod postcard {
}

#[inline]
#[must_use]
pub fn encode_zigzag(value: i128) -> u128 {
((value << 1) ^ (value >> 127)) as u128
}

#[inline]
#[must_use]
pub fn decode(buf: &[u8]) -> u128 {
let mut value = 0;
for (i, b) in buf.iter().copied().enumerate().take(max_size::<u128>()) {
value |= ((b & 0x7f) as u128) << (7 * i);
value |= u128::from(b & 0x7f) << (7 * i);

if b & 0x80 == 0 {
return value;
Expand All @@ -42,11 +44,13 @@ pub mod postcard {
}

#[inline]
#[must_use]
pub fn decode_i128(buf: &[u8]) -> i128 {
decode_zigzag(decode(buf))
}

#[inline]
#[must_use]
pub fn decode_zigzag(value: u128) -> i128 {
((value >> 1) as i128) ^ (-((value & 0b1) as i128))
}
Expand Down Expand Up @@ -191,6 +195,7 @@ pub mod bincode {
}

#[inline]
#[must_use]
pub fn decode_u16(buf: &[u8]) -> u16 {
match buf[0] {
byte @ 0..=250 => byte.into(),
Expand All @@ -204,6 +209,7 @@ pub mod bincode {
}

#[inline]
#[must_use]
pub fn decode_i16(buf: &[u8]) -> i16 {
let value = decode_u16(buf);
if value % 2 == 0 {
Expand All @@ -214,6 +220,7 @@ pub mod bincode {
}

#[inline]
#[must_use]
pub fn decode_u32(buf: &[u8]) -> u32 {
match buf[0] {
byte @ 0..=250 => byte.into(),
Expand All @@ -232,6 +239,7 @@ pub mod bincode {
}

#[inline]
#[must_use]
pub fn decode_i32(buf: &[u8]) -> i32 {
let value = decode_u32(buf);
if value % 2 == 0 {
Expand All @@ -242,6 +250,7 @@ pub mod bincode {
}

#[inline]
#[must_use]
pub fn decode_u64(buf: &[u8]) -> u64 {
match buf[0] {
byte @ 0..=250 => byte.into(),
Expand All @@ -265,6 +274,7 @@ pub mod bincode {
}

#[inline]
#[must_use]
pub fn decode_i64(buf: &[u8]) -> i64 {
let value = decode_u64(buf);
if value % 2 == 0 {
Expand All @@ -275,6 +285,7 @@ pub mod bincode {
}

#[inline]
#[must_use]
pub fn decode_u128(buf: &[u8]) -> u128 {
match buf[0] {
byte @ 0..=250 => byte.into(),
Expand Down Expand Up @@ -303,6 +314,7 @@ pub mod bincode {
}

#[inline]
#[must_use]
pub fn decode_i128(buf: &[u8]) -> i128 {
let value = decode_u128(buf);
if value % 2 == 0 {
Expand Down
3 changes: 2 additions & 1 deletion crates/stef-build/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn compile_struct(
quote! {
#[automatically_derived]
impl #generics ::stef::Decode for #name #generics #generics_where {
#[allow(clippy::type_complexity)]
#[allow(clippy::type_complexity, clippy::too_many_lines)]
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
#body
}
Expand All @@ -62,6 +62,7 @@ pub fn compile_enum(
quote! {
#[automatically_derived]
impl #generics ::stef::Decode for #name #generics #generics_where {
#[allow(clippy::too_many_lines)]
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
match ::stef::buf::decode_id(r)? {
#(#variants,)*
Expand Down
18 changes: 14 additions & 4 deletions crates/stef-build/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ fn compile_struct(
quote! {
#comment
#[derive(Clone, Debug, PartialEq)]
#[allow(clippy::module_name_repetitions, clippy::option_option)]
pub struct #name #generics #fields
}
}
Expand All @@ -111,6 +112,7 @@ fn compile_enum(
quote! {
#comment
#[derive(Clone, Debug, PartialEq)]
#[allow(clippy::module_name_repetitions, clippy::option_option)]
pub enum #name #generics {
#(#variants,)*
}
Expand Down Expand Up @@ -148,7 +150,7 @@ fn compile_alias(

quote! {
#comment
#[allow(dead_code)]
#[allow(dead_code, clippy::module_name_repetitions, clippy::option_option)]
pub type #alias = #target;
}
}
Expand Down Expand Up @@ -429,6 +431,7 @@ mod tests {
use ::stef::buf::{Decode, Encode};
/// Hello world!
#[derive(Clone, Debug, PartialEq)]
#[allow(clippy::module_name_repetitions, clippy::option_option)]
pub struct Sample {
pub field1: u32,
pub field2: Vec<u8>,
Expand All @@ -440,6 +443,7 @@ mod tests {
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
clippy::too_many_lines,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {
::stef::buf::encode_field(
Expand Down Expand Up @@ -475,7 +479,7 @@ mod tests {
}
#[automatically_derived]
impl ::stef::Decode for Sample {
#[allow(clippy::type_complexity)]
#[allow(clippy::type_complexity, clippy::too_many_lines)]
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
let mut field1: Option<u32> = None;
let mut field2: Option<Vec<u8>> = None;
Expand Down Expand Up @@ -546,14 +550,19 @@ mod tests {
use ::stef::buf::{Decode, Encode};
/// Hello world!
#[derive(Clone, Debug, PartialEq)]
#[allow(clippy::module_name_repetitions, clippy::option_option)]
pub enum Sample {
Variant1,
Variant2(u32, u8),
Variant3 { field1: String, field2: Vec<bool> },
}
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(clippy::borrow_deref_ref)]
#[allow(
clippy::borrow_deref_ref,
clippy::semicolon_if_nothing_returned,
clippy::too_many_lines,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {
match self {
Self::Variant1 => {
Expand Down Expand Up @@ -592,6 +601,7 @@ mod tests {
}
#[automatically_derived]
impl ::stef::Decode for Sample {
#[allow(clippy::too_many_lines)]
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
match ::stef::buf::decode_id(r)? {
1 => Ok(Self::Variant1),
Expand Down Expand Up @@ -671,7 +681,7 @@ mod tests {
#[allow(unused_imports)]
use ::stef::buf::{Decode, Encode};
/// Hello world!
#[allow(dead_code)]
#[allow(dead_code, clippy::module_name_repetitions, clippy::option_option)]
pub type Sample = String;
"#};

Expand Down
7 changes: 6 additions & 1 deletion crates/stef-build/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub fn compile_struct(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
clippy::too_many_lines,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {
#fields
Expand Down Expand Up @@ -99,7 +100,11 @@ pub fn compile_enum(
quote! {
#[automatically_derived]
impl #generics ::stef::Encode for #name #generics #generics_where {
#[allow(clippy::borrow_deref_ref)]
#[allow(
clippy::borrow_deref_ref,
clippy::semicolon_if_nothing_returned,
clippy::too_many_lines,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {
match self {
#(#variants,)*
Expand Down
4 changes: 3 additions & 1 deletion crates/stef-build/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#![deny(rust_2018_idioms, clippy::all, clippy::pedantic)]
#![forbid(unsafe_code)]
#![deny(rust_2018_idioms, clippy::all)]
#![warn(clippy::pedantic)]
#![allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]

use std::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ input_file: crates/stef-parser/tests/inputs/alias-basic.stef
#[allow(unused_imports)]
use ::stef::buf::{Decode, Encode};
/// Sample type alias.
#[allow(dead_code)]
#[allow(dead_code, clippy::module_name_repetitions, clippy::option_option)]
pub type Sample = u32;

Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ input_file: crates/stef-parser/tests/inputs/attribute-multi.stef
#[allow(unused_imports)]
use ::stef::buf::{Decode, Encode};
#[derive(Clone, Debug, PartialEq)]
#[allow(clippy::module_name_repetitions, clippy::option_option)]
pub struct Sample;
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
clippy::too_many_lines,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {}
}
#[automatically_derived]
impl ::stef::Decode for Sample {
#[allow(clippy::type_complexity)]
#[allow(clippy::type_complexity, clippy::too_many_lines)]
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
Ok(Self)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ input_file: crates/stef-parser/tests/inputs/attribute-single.stef
#[allow(unused_imports)]
use ::stef::buf::{Decode, Encode};
#[derive(Clone, Debug, PartialEq)]
#[allow(clippy::module_name_repetitions, clippy::option_option)]
pub struct Sample;
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
clippy::too_many_lines,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {}
}
#[automatically_derived]
impl ::stef::Decode for Sample {
#[allow(clippy::type_complexity)]
#[allow(clippy::type_complexity, clippy::too_many_lines)]
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
Ok(Self)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ input_file: crates/stef-parser/tests/inputs/attribute-unit.stef
#[allow(unused_imports)]
use ::stef::buf::{Decode, Encode};
#[derive(Clone, Debug, PartialEq)]
#[allow(clippy::module_name_repetitions, clippy::option_option)]
pub struct Sample;
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
clippy::too_many_lines,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {}
}
#[automatically_derived]
impl ::stef::Decode for Sample {
#[allow(clippy::type_complexity)]
#[allow(clippy::type_complexity, clippy::too_many_lines)]
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
Ok(Self)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ input_file: crates/stef-parser/tests/inputs/attributes-min-ws.stef
#[allow(unused_imports)]
use ::stef::buf::{Decode, Encode};
#[derive(Clone, Debug, PartialEq)]
#[allow(clippy::module_name_repetitions, clippy::option_option)]
pub struct Sample;
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
clippy::too_many_lines,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {}
}
#[automatically_derived]
impl ::stef::Decode for Sample {
#[allow(clippy::type_complexity)]
#[allow(clippy::type_complexity, clippy::too_many_lines)]
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
Ok(Self)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ input_file: crates/stef-parser/tests/inputs/attributes.stef
#[allow(unused_imports)]
use ::stef::buf::{Decode, Encode};
#[derive(Clone, Debug, PartialEq)]
#[allow(clippy::module_name_repetitions, clippy::option_option)]
pub struct Sample;
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
clippy::too_many_lines,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {}
}
#[automatically_derived]
impl ::stef::Decode for Sample {
#[allow(clippy::type_complexity)]
#[allow(clippy::type_complexity, clippy::too_many_lines)]
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
Ok(Self)
}
Expand Down
Loading

0 comments on commit 3c206de

Please sign in to comment.