From 3c206de825d94ed2559d93fba79ff41f1155a0af Mon Sep 17 00:00:00 2001 From: Dominik Nakamura Date: Mon, 23 Oct 2023 18:18:40 +0900 Subject: [PATCH] refactor: enable more clippy lints and unify them Ensuring consistent code quality through lints within all the crates that are part of this project. --- crates/stef-benches/src/lib.rs | 12 +++++++++ crates/stef-benches/src/varint.rs | 14 +++++++++- crates/stef-build/src/decode.rs | 3 ++- crates/stef-build/src/definition.rs | 18 ++++++++++--- crates/stef-build/src/encode.rs | 7 ++++- crates/stef-build/src/lib.rs | 4 ++- .../compiler__compile@alias-basic.stef.snap | 2 +- ...ompiler__compile@attribute-multi.stef.snap | 4 ++- ...mpiler__compile@attribute-single.stef.snap | 4 ++- ...compiler__compile@attribute-unit.stef.snap | 4 ++- ...piler__compile@attributes-min-ws.stef.snap | 4 ++- .../compiler__compile@attributes.stef.snap | 4 ++- .../compiler__compile@enum-basic.stef.snap | 8 +++++- .../compiler__compile@enum-generics.stef.snap | 8 +++++- .../compiler__compile@enum-many-ws.stef.snap | 8 +++++- .../compiler__compile@enum-min-ws.stef.snap | 8 +++++- .../compiler__compile@module-basic.stef.snap | 12 +++++++-- .../compiler__compile@schema-basic.stef.snap | 12 +++++++-- .../compiler__compile@struct-basic.stef.snap | 4 ++- ...ompiler__compile@struct-generics.stef.snap | 4 ++- ...compiler__compile@struct-many-ws.stef.snap | 4 ++- .../compiler__compile@struct-min-ws.stef.snap | 4 ++- .../compiler__compile@struct-tuple.stef.snap | 4 ++- .../compiler__compile@types-basic.stef.snap | 4 ++- .../compiler__compile@types-generic.stef.snap | 4 ++- .../compiler__compile@types-nested.stef.snap | 4 ++- ...compiler__compile@types-non-zero.stef.snap | 4 ++- .../compiler__compile@types-ref.stef.snap | 16 +++++++++--- crates/stef-cli/src/main.rs | 7 ++++- crates/stef-compiler/src/ids.rs | 2 +- crates/stef-compiler/src/lib.rs | 5 ++++ crates/stef-compiler/src/names.rs | 2 +- crates/stef-derive/src/attributes.rs | 4 +-- crates/stef-derive/src/cause.rs | 18 ++++++------- crates/stef-derive/src/error.rs | 26 +++++++++---------- crates/stef-derive/src/lib.rs | 9 +++++++ crates/stef-playground/src/lib.rs | 25 +++++++++++------- crates/stef/src/buf/encode.rs | 6 ++--- crates/stef/src/buf/mod.rs | 2 +- crates/stef/src/lib.rs | 13 ++++++++++ crates/stef/src/varint.rs | 2 ++ 41 files changed, 233 insertions(+), 76 deletions(-) diff --git a/crates/stef-benches/src/lib.rs b/crates/stef-benches/src/lib.rs index ee2317c..264f756 100644 --- a/crates/stef-benches/src/lib.rs +++ b/crates/stef-benches/src/lib.rs @@ -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(); diff --git a/crates/stef-benches/src/varint.rs b/crates/stef-benches/src/varint.rs index 5c06161..cdadb6d 100644 --- a/crates/stef-benches/src/varint.rs +++ b/crates/stef-benches/src/varint.rs @@ -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::()) { - value |= ((b & 0x7f) as u128) << (7 * i); + value |= u128::from(b & 0x7f) << (7 * i); if b & 0x80 == 0 { return value; @@ -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)) } @@ -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(), @@ -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 { @@ -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(), @@ -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 { @@ -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(), @@ -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 { @@ -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(), @@ -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 { diff --git a/crates/stef-build/src/decode.rs b/crates/stef-build/src/decode.rs index f854e18..fdd033c 100644 --- a/crates/stef-build/src/decode.rs +++ b/crates/stef-build/src/decode.rs @@ -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 { #body } @@ -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 { match ::stef::buf::decode_id(r)? { #(#variants,)* diff --git a/crates/stef-build/src/definition.rs b/crates/stef-build/src/definition.rs index f7ec59a..39da5d2 100644 --- a/crates/stef-build/src/definition.rs +++ b/crates/stef-build/src/definition.rs @@ -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 } } @@ -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,)* } @@ -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; } } @@ -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, @@ -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( @@ -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 { let mut field1: Option = None; let mut field2: Option> = None; @@ -546,6 +550,7 @@ 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), @@ -553,7 +558,11 @@ mod tests { } #[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 => { @@ -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 { match ::stef::buf::decode_id(r)? { 1 => Ok(Self::Variant1), @@ -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; "#}; diff --git a/crates/stef-build/src/encode.rs b/crates/stef-build/src/encode.rs index bffb65a..1a0e47e 100644 --- a/crates/stef-build/src/encode.rs +++ b/crates/stef-build/src/encode.rs @@ -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 @@ -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,)* diff --git a/crates/stef-build/src/lib.rs b/crates/stef-build/src/lib.rs index 01a01ee..b080004 100644 --- a/crates/stef-build/src/lib.rs +++ b/crates/stef-build/src/lib.rs @@ -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::{ diff --git a/crates/stef-build/tests/snapshots/compiler__compile@alias-basic.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@alias-basic.stef.snap index 697b96d..cd716b4 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@alias-basic.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@alias-basic.stef.snap @@ -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; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@attribute-multi.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@attribute-multi.stef.snap index 9a34ee1..eafcbfa 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@attribute-multi.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@attribute-multi.stef.snap @@ -6,6 +6,7 @@ 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 { @@ -13,12 +14,13 @@ impl ::stef::Encode for Sample { 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 { Ok(Self) } diff --git a/crates/stef-build/tests/snapshots/compiler__compile@attribute-single.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@attribute-single.stef.snap index 1be237e..03b3a08 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@attribute-single.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@attribute-single.stef.snap @@ -6,6 +6,7 @@ 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 { @@ -13,12 +14,13 @@ impl ::stef::Encode for Sample { 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 { Ok(Self) } diff --git a/crates/stef-build/tests/snapshots/compiler__compile@attribute-unit.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@attribute-unit.stef.snap index fb18a08..bd0790f 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@attribute-unit.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@attribute-unit.stef.snap @@ -6,6 +6,7 @@ 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 { @@ -13,12 +14,13 @@ impl ::stef::Encode for Sample { 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 { Ok(Self) } diff --git a/crates/stef-build/tests/snapshots/compiler__compile@attributes-min-ws.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@attributes-min-ws.stef.snap index bc63f69..4fab8c9 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@attributes-min-ws.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@attributes-min-ws.stef.snap @@ -6,6 +6,7 @@ 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 { @@ -13,12 +14,13 @@ impl ::stef::Encode for Sample { 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 { Ok(Self) } diff --git a/crates/stef-build/tests/snapshots/compiler__compile@attributes.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@attributes.stef.snap index aaf2668..9f150dc 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@attributes.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@attributes.stef.snap @@ -6,6 +6,7 @@ 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 { @@ -13,12 +14,13 @@ impl ::stef::Encode for Sample { 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 { Ok(Self) } diff --git a/crates/stef-build/tests/snapshots/compiler__compile@enum-basic.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@enum-basic.stef.snap index deb2102..456082b 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@enum-basic.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@enum-basic.stef.snap @@ -7,6 +7,7 @@ input_file: crates/stef-parser/tests/inputs/enum-basic.stef use ::stef::buf::{Decode, Encode}; /// Sample enum. #[derive(Clone, Debug, PartialEq)] +#[allow(clippy::module_name_repetitions, clippy::option_option)] pub enum Sample { One, /// Second variant @@ -19,7 +20,11 @@ pub enum Sample { } #[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::One => { @@ -50,6 +55,7 @@ impl ::stef::Encode for Sample { } #[automatically_derived] impl ::stef::Decode for Sample { + #[allow(clippy::too_many_lines)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { 1 => Ok(Self::One), diff --git a/crates/stef-build/tests/snapshots/compiler__compile@enum-generics.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@enum-generics.stef.snap index f4c8a51..17ed14c 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@enum-generics.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@enum-generics.stef.snap @@ -7,6 +7,7 @@ input_file: crates/stef-parser/tests/inputs/enum-generics.stef use ::stef::buf::{Decode, Encode}; /// Enum with generics. #[derive(Clone, Debug, PartialEq)] +#[allow(clippy::module_name_repetitions, clippy::option_option)] pub enum Sample { One, Two(A, B), @@ -20,7 +21,11 @@ where C: ::stef::buf::Encode, D: ::stef::buf::Encode, { - #[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::One => { @@ -49,6 +54,7 @@ where C: ::std::fmt::Debug + ::stef::buf::Decode, D: ::std::fmt::Debug + ::stef::buf::Decode, { + #[allow(clippy::too_many_lines)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { 1 => Ok(Self::One), diff --git a/crates/stef-build/tests/snapshots/compiler__compile@enum-many-ws.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@enum-many-ws.stef.snap index eab573c..cfaa32d 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@enum-many-ws.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@enum-many-ws.stef.snap @@ -7,6 +7,7 @@ input_file: crates/stef-parser/tests/inputs/enum-many-ws.stef use ::stef::buf::{Decode, Encode}; /// Sample enum. #[derive(Clone, Debug, PartialEq)] +#[allow(clippy::module_name_repetitions, clippy::option_option)] pub enum Sample { One, Two(u32, u64), @@ -14,7 +15,11 @@ pub enum Sample { } #[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::One => { @@ -45,6 +50,7 @@ impl ::stef::Encode for Sample { } #[automatically_derived] impl ::stef::Decode for Sample { + #[allow(clippy::too_many_lines)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { 1 => Ok(Self::One), diff --git a/crates/stef-build/tests/snapshots/compiler__compile@enum-min-ws.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@enum-min-ws.stef.snap index fbc635b..b09bb1d 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@enum-min-ws.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@enum-min-ws.stef.snap @@ -6,6 +6,7 @@ input_file: crates/stef-parser/tests/inputs/enum-min-ws.stef #[allow(unused_imports)] use ::stef::buf::{Decode, Encode}; #[derive(Clone, Debug, PartialEq)] +#[allow(clippy::module_name_repetitions, clippy::option_option)] pub enum Sample { One, Two(u32, u64, T), @@ -16,7 +17,11 @@ impl ::stef::Encode for Sample where T: ::stef::buf::Encode, { - #[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::One => { @@ -52,6 +57,7 @@ impl ::stef::Decode for Sample where T: ::std::fmt::Debug + ::stef::buf::Decode, { + #[allow(clippy::too_many_lines)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { 1 => Ok(Self::One), diff --git a/crates/stef-build/tests/snapshots/compiler__compile@module-basic.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@module-basic.stef.snap index 97202d1..9d3db8a 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@module-basic.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@module-basic.stef.snap @@ -13,12 +13,17 @@ pub mod a { #[allow(unused_imports)] use ::stef::buf::{Decode, Encode}; #[derive(Clone, Debug, PartialEq)] + #[allow(clippy::module_name_repetitions, clippy::option_option)] pub enum Sample { One, } #[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::One => { @@ -29,6 +34,7 @@ pub mod a { } #[automatically_derived] impl ::stef::Decode for Sample { + #[allow(clippy::too_many_lines)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { 1 => Ok(Self::One), @@ -38,6 +44,7 @@ pub mod a { } } #[derive(Clone, Debug, PartialEq)] + #[allow(clippy::module_name_repetitions, clippy::option_option)] pub struct Sample { pub value: u32, } @@ -47,6 +54,7 @@ pub mod a { 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( @@ -61,7 +69,7 @@ pub mod a { } #[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 { let mut value: Option = None; loop { diff --git a/crates/stef-build/tests/snapshots/compiler__compile@schema-basic.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@schema-basic.stef.snap index 423122b..b0c6fab 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@schema-basic.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@schema-basic.stef.snap @@ -7,6 +7,7 @@ input_file: crates/stef-parser/tests/inputs/schema-basic.stef use ::stef::buf::{Decode, Encode}; /// Basic struct. #[derive(Clone, Debug, PartialEq)] +#[allow(clippy::module_name_repetitions, clippy::option_option)] pub struct SampleStruct { pub a: u32, pub b: bool, @@ -17,6 +18,7 @@ impl ::stef::Encode for SampleStruct { 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( @@ -38,7 +40,7 @@ impl ::stef::Encode for SampleStruct { } #[automatically_derived] impl ::stef::Decode for SampleStruct { - #[allow(clippy::type_complexity)] + #[allow(clippy::type_complexity, clippy::too_many_lines)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut a: Option = None; let mut b: Option = None; @@ -66,6 +68,7 @@ impl ::stef::Decode for SampleStruct { } /// Sample enum. #[derive(Clone, Debug, PartialEq)] +#[allow(clippy::module_name_repetitions, clippy::option_option)] pub enum SampleEnum { One, Two(u32, u64), @@ -73,7 +76,11 @@ pub enum SampleEnum { } #[automatically_derived] impl ::stef::Encode for SampleEnum { - #[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::One => { @@ -104,6 +111,7 @@ impl ::stef::Encode for SampleEnum { } #[automatically_derived] impl ::stef::Decode for SampleEnum { + #[allow(clippy::too_many_lines)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { 1 => Ok(Self::One), diff --git a/crates/stef-build/tests/snapshots/compiler__compile@struct-basic.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@struct-basic.stef.snap index a4b4764..92e2842 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@struct-basic.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@struct-basic.stef.snap @@ -7,6 +7,7 @@ input_file: crates/stef-parser/tests/inputs/struct-basic.stef use ::stef::buf::{Decode, Encode}; /// Basic struct. #[derive(Clone, Debug, PartialEq)] +#[allow(clippy::module_name_repetitions, clippy::option_option)] pub struct Sample { pub a: u32, /// Second field @@ -18,6 +19,7 @@ impl ::stef::Encode for Sample { 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( @@ -39,7 +41,7 @@ impl ::stef::Encode for Sample { } #[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 { let mut a: Option = None; let mut b: Option = None; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@struct-generics.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@struct-generics.stef.snap index bf1865b..69c9c89 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@struct-generics.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@struct-generics.stef.snap @@ -7,6 +7,7 @@ input_file: crates/stef-parser/tests/inputs/struct-generics.stef use ::stef::buf::{Decode, Encode}; /// Generic key-value pair. #[derive(Clone, Debug, PartialEq)] +#[allow(clippy::module_name_repetitions, clippy::option_option)] pub struct KeyValue { pub key: K, pub value: V, @@ -21,6 +22,7 @@ where 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( @@ -46,7 +48,7 @@ where K: ::std::fmt::Debug + ::stef::buf::Decode, V: ::std::fmt::Debug + ::stef::buf::Decode, { - #[allow(clippy::type_complexity)] + #[allow(clippy::type_complexity, clippy::too_many_lines)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut key: Option = None; let mut value: Option = None; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@struct-many-ws.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@struct-many-ws.stef.snap index 94a87f1..15bf327 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@struct-many-ws.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@struct-many-ws.stef.snap @@ -7,6 +7,7 @@ input_file: crates/stef-parser/tests/inputs/struct-many-ws.stef use ::stef::buf::{Decode, Encode}; /// Some comment #[derive(Clone, Debug, PartialEq)] +#[allow(clippy::module_name_repetitions, clippy::option_option)] pub struct Sample { pub a: u32, pub b: bool, @@ -21,6 +22,7 @@ where 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( @@ -46,7 +48,7 @@ where A: ::std::fmt::Debug + ::stef::buf::Decode, B: ::std::fmt::Debug + ::stef::buf::Decode, { - #[allow(clippy::type_complexity)] + #[allow(clippy::type_complexity, clippy::too_many_lines)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut a: Option = None; let mut b: Option = None; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@struct-min-ws.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@struct-min-ws.stef.snap index 5ea9a68..2ef926b 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@struct-min-ws.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@struct-min-ws.stef.snap @@ -6,6 +6,7 @@ input_file: crates/stef-parser/tests/inputs/struct-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 { pub a: u32, pub b: bool, @@ -20,6 +21,7 @@ where 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( @@ -51,7 +53,7 @@ impl ::stef::Decode for Sample where T: ::std::fmt::Debug + ::stef::buf::Decode, { - #[allow(clippy::type_complexity)] + #[allow(clippy::type_complexity, clippy::too_many_lines)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut a: Option = None; let mut b: Option = None; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@struct-tuple.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@struct-tuple.stef.snap index 905ce5e..3c12a11 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@struct-tuple.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@struct-tuple.stef.snap @@ -7,6 +7,7 @@ input_file: crates/stef-parser/tests/inputs/struct-tuple.stef use ::stef::buf::{Decode, Encode}; /// Basic struct. #[derive(Clone, Debug, PartialEq)] +#[allow(clippy::module_name_repetitions, clippy::option_option)] pub struct Sample(u32, bool); #[automatically_derived] impl ::stef::Encode for Sample { @@ -14,6 +15,7 @@ impl ::stef::Encode for Sample { 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(w, 1, |w| { ::stef::buf::encode_u32(w, self.0) }); @@ -23,7 +25,7 @@ impl ::stef::Encode for Sample { } #[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 { let mut n0: Option = None; let mut n1: Option = None; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@types-basic.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@types-basic.stef.snap index 4b5aff5..de323c2 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@types-basic.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@types-basic.stef.snap @@ -6,6 +6,7 @@ input_file: crates/stef-parser/tests/inputs/types-basic.stef #[allow(unused_imports)] use ::stef::buf::{Decode, Encode}; #[derive(Clone, Debug, PartialEq)] +#[allow(clippy::module_name_repetitions, clippy::option_option)] pub struct Sample { pub f01: bool, pub f02: u8, @@ -35,6 +36,7 @@ impl ::stef::Encode for Sample { 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( @@ -197,7 +199,7 @@ impl ::stef::Encode for Sample { } #[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 { let mut f01: Option = None; let mut f02: Option = None; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@types-generic.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@types-generic.stef.snap index 2f98e74..5a6d2df 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@types-generic.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@types-generic.stef.snap @@ -6,6 +6,7 @@ input_file: crates/stef-parser/tests/inputs/types-generic.stef #[allow(unused_imports)] use ::stef::buf::{Decode, Encode}; #[derive(Clone, Debug, PartialEq)] +#[allow(clippy::module_name_repetitions, clippy::option_option)] pub struct Sample { pub f1: Vec, pub f2: ::std::collections::HashMap, @@ -19,6 +20,7 @@ impl ::stef::Encode for Sample { 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( @@ -83,7 +85,7 @@ impl ::stef::Encode for Sample { } #[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 { let mut f1: Option> = None; let mut f2: Option<::std::collections::HashMap> = None; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@types-nested.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@types-nested.stef.snap index 2880019..ed1e30e 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@types-nested.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@types-nested.stef.snap @@ -6,6 +6,7 @@ input_file: crates/stef-parser/tests/inputs/types-nested.stef #[allow(unused_imports)] use ::stef::buf::{Decode, Encode}; #[derive(Clone, Debug, PartialEq)] +#[allow(clippy::module_name_repetitions, clippy::option_option)] pub struct Sample { pub value: Vec>>>, } @@ -15,6 +16,7 @@ impl ::stef::Encode for Sample { 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( @@ -50,7 +52,7 @@ impl ::stef::Encode for Sample { } #[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 { let mut value: Option>>>> = None; loop { diff --git a/crates/stef-build/tests/snapshots/compiler__compile@types-non-zero.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@types-non-zero.stef.snap index d24eb64..3581c17 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@types-non-zero.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@types-non-zero.stef.snap @@ -6,6 +6,7 @@ input_file: crates/stef-parser/tests/inputs/types-non-zero.stef #[allow(unused_imports)] use ::stef::buf::{Decode, Encode}; #[derive(Clone, Debug, PartialEq)] +#[allow(clippy::module_name_repetitions, clippy::option_option)] pub struct Sample { pub f01: ::std::num::NonZeroU8, pub f02: ::std::num::NonZeroU16, @@ -29,6 +30,7 @@ impl ::stef::Encode for Sample { 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( @@ -162,7 +164,7 @@ impl ::stef::Encode for Sample { } #[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 { let mut f01: Option<::std::num::NonZeroU8> = None; let mut f02: Option<::std::num::NonZeroU16> = None; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@types-ref.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@types-ref.stef.snap index 6898b34..c6e02c6 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@types-ref.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@types-ref.stef.snap @@ -6,6 +6,7 @@ input_file: crates/stef-parser/tests/inputs/types-ref.stef #[allow(unused_imports)] use ::stef::buf::{Decode, Encode}; #[derive(Clone, Debug, PartialEq)] +#[allow(clippy::module_name_repetitions, clippy::option_option)] pub struct Sample { pub basic: Test123, pub with_generics: KeyValue, @@ -16,6 +17,7 @@ impl ::stef::Encode for Sample { 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( @@ -37,7 +39,7 @@ impl ::stef::Encode for Sample { } #[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 { let mut basic: Option = None; let mut with_generics: Option> = None; @@ -64,12 +66,17 @@ impl ::stef::Decode for Sample { } } #[derive(Clone, Debug, PartialEq)] +#[allow(clippy::module_name_repetitions, clippy::option_option)] pub enum Test123 { Value, } #[automatically_derived] impl ::stef::Encode for Test123 { - #[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::Value => { @@ -80,6 +87,7 @@ impl ::stef::Encode for Test123 { } #[automatically_derived] impl ::stef::Decode for Test123 { + #[allow(clippy::too_many_lines)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { 1 => Ok(Self::Value), @@ -88,6 +96,7 @@ impl ::stef::Decode for Test123 { } } #[derive(Clone, Debug, PartialEq)] +#[allow(clippy::module_name_repetitions, clippy::option_option)] pub struct KeyValue { pub key: K, pub value: V, @@ -102,6 +111,7 @@ where 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( @@ -127,7 +137,7 @@ where K: ::std::fmt::Debug + ::stef::buf::Decode, V: ::std::fmt::Debug + ::stef::buf::Decode, { - #[allow(clippy::type_complexity)] + #[allow(clippy::type_complexity, clippy::too_many_lines)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut key: Option = None; let mut value: Option = None; diff --git a/crates/stef-cli/src/main.rs b/crates/stef-cli/src/main.rs index dd27cdb..33c188e 100644 --- a/crates/stef-cli/src/main.rs +++ b/crates/stef-cli/src/main.rs @@ -1,4 +1,7 @@ -mod cli; +#![forbid(unsafe_code)] +#![deny(rust_2018_idioms, clippy::all)] +#![warn(clippy::pedantic)] +#![allow(clippy::missing_errors_doc)] use std::{fs, path::PathBuf, process::ExitCode}; @@ -7,6 +10,8 @@ use stef_parser::Schema; use self::cli::Cli; +mod cli; + #[global_allocator] static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc; diff --git a/crates/stef-compiler/src/ids.rs b/crates/stef-compiler/src/ids.rs index fafde4d..b575af2 100644 --- a/crates/stef-compiler/src/ids.rs +++ b/crates/stef-compiler/src/ids.rs @@ -68,7 +68,7 @@ pub(crate) fn validate_enum_ids(value: &Enum<'_>) -> Result<(), DuplicateId> { } /// Ensure all field IDs of a struct or enum are unique. -fn validate_field_ids(value: &Fields) -> Result<(), DuplicateFieldId> { +fn validate_field_ids(value: &Fields<'_>) -> Result<(), DuplicateFieldId> { match value { Fields::Named(named) => { let mut visited = HashMap::with_capacity(named.len()); diff --git a/crates/stef-compiler/src/lib.rs b/crates/stef-compiler/src/lib.rs index d8cdfdd..34ff561 100644 --- a/crates/stef-compiler/src/lib.rs +++ b/crates/stef-compiler/src/lib.rs @@ -1,3 +1,8 @@ +#![forbid(unsafe_code)] +#![deny(rust_2018_idioms, clippy::all)] +#![warn(clippy::pedantic)] +#![allow(clippy::missing_errors_doc)] + pub use ids::{DuplicateFieldId, DuplicateId, DuplicateVariantId}; use stef_parser::{Definition, Schema}; use thiserror::Error; diff --git a/crates/stef-compiler/src/names.rs b/crates/stef-compiler/src/names.rs index 56e0e1f..ef29d88 100644 --- a/crates/stef-compiler/src/names.rs +++ b/crates/stef-compiler/src/names.rs @@ -53,7 +53,7 @@ pub(crate) fn validate_enum_names(value: &Enum<'_>) -> Result<(), DuplicateName> } /// Ensure all field names of a struct or enum are unique. -fn validate_field_names(value: &Fields) -> Result<(), DuplicateFieldName> { +fn validate_field_names(value: &Fields<'_>) -> Result<(), DuplicateFieldName> { match value { Fields::Named(named) => { let mut visited = HashSet::with_capacity(named.len()); diff --git a/crates/stef-derive/src/attributes.rs b/crates/stef-derive/src/attributes.rs index 60c000d..bca171f 100644 --- a/crates/stef-derive/src/attributes.rs +++ b/crates/stef-derive/src/attributes.rs @@ -177,13 +177,13 @@ impl FieldAttributesParser { let mut value = Self::default(); for attr in attrs.iter().filter(|attr| attr.path().is_ident("err")) { - attr.parse_nested_meta(|meta| value.parse(meta))?; + attr.parse_nested_meta(|meta| value.parse(&meta))?; } Ok(value) } - fn parse(&mut self, meta: ParseNestedMeta<'_>) -> syn::Result<()> { + fn parse(&mut self, meta: &ParseNestedMeta<'_>) -> syn::Result<()> { if meta.path.is_ident("label") { let content; parenthesized!(content in meta.input); diff --git a/crates/stef-derive/src/cause.rs b/crates/stef-derive/src/cause.rs index c2bc4aa..81785eb 100644 --- a/crates/stef-derive/src/cause.rs +++ b/crates/stef-derive/src/cause.rs @@ -38,8 +38,8 @@ pub fn expand(derive: DeriveInput) -> syn::Result { .map(VariantInfo::parse) .collect::>>()?; - let error_impl = expand_error(ident, &variants)?; - let miette_impl = expand_miette(ident, &attrs, &variants)?; + let error_impl = expand_error(ident, &variants); + let miette_impl = expand_miette(ident, &attrs, &variants); let winnow_impl = expand_winnow(ident, &variants)?; Ok(quote! { @@ -111,13 +111,13 @@ impl<'a> VariantInfo<'a> { .iter() .map(|f| Ok((f, FieldAttributes::parse(&f.attrs)?))) .collect::>>()?, - _ => Vec::new(), + Fields::Unit => Vec::new(), }, }) } } -fn expand_error(ident: &Ident, variants: &[VariantInfo<'_>]) -> syn::Result { +fn expand_error(ident: &Ident, variants: &[VariantInfo<'_>]) -> TokenStream { let sources = variants.iter().map(|v| { let ident = &v.variant.ident; @@ -216,7 +216,7 @@ fn expand_error(ident: &Ident, variants: &[VariantInfo<'_>]) -> syn::Result Option<&(dyn std::error::Error + 'static)> { match self { @@ -236,14 +236,14 @@ fn expand_error(ident: &Ident, variants: &[VariantInfo<'_>]) -> syn::Result], -) -> syn::Result { +) -> TokenStream { let codes = variants.iter().map(|v| { let ident = &v.variant.ident; @@ -387,7 +387,7 @@ fn expand_miette( } }); - Ok(quote! { + quote! { impl miette::Diagnostic for #ident { fn code(&self) -> Option> { match self { @@ -424,7 +424,7 @@ fn expand_miette( } } } - }) + } } fn expand_winnow(ident: &Ident, variants: &[VariantInfo<'_>]) -> syn::Result { diff --git a/crates/stef-derive/src/error.rs b/crates/stef-derive/src/error.rs index 091839a..2c15d1e 100644 --- a/crates/stef-derive/src/error.rs +++ b/crates/stef-derive/src/error.rs @@ -24,12 +24,10 @@ pub fn expand(derive: DeriveInput) -> syn::Result { bail!(data.fields, "only named structs supported") }; - let Some(_cause_field) = fields.iter().find(|f| { - f.ident - .as_ref() - .map(|ident| ident == "cause") - .unwrap_or(false) - }) else { + let Some(_cause_field) = fields + .iter() + .find(|f| f.ident.as_ref().is_some_and(|ident| ident == "cause")) + else { bail!(fields, "struct musn't be empty"); }; @@ -41,8 +39,8 @@ pub fn expand(derive: DeriveInput) -> syn::Result { .collect::>>()?, }; - let error_impl = expand_error(ident, &info)?; - let miette_impl = expand_miette(ident, &info)?; + let error_impl = expand_error(ident, &info); + let miette_impl = expand_miette(ident, &info); Ok(quote! { #error_impl @@ -68,10 +66,10 @@ struct StructInfo<'a> { fields: Vec<(&'a Field, Option)>, } -fn expand_error(ident: &Ident, info: &StructInfo<'_>) -> syn::Result { +fn expand_error(ident: &Ident, info: &StructInfo<'_>) -> TokenStream { let StructAttributes { msg, .. } = &info.attr; - Ok(quote! { + quote! { impl std::error::Error for #ident { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.cause as &(dyn std::error::Error + 'static)) @@ -83,10 +81,10 @@ fn expand_error(ident: &Ident, info: &StructInfo<'_>) -> syn::Result) -> syn::Result { +fn expand_miette(ident: &Ident, info: &StructInfo<'_>) -> TokenStream { let StructAttributes { code, help, .. } = &info.attr; let code = code.segments.iter().fold(String::new(), |mut acc, seg| { @@ -148,7 +146,7 @@ fn expand_miette(ident: &Ident, info: &StructInfo<'_>) -> syn::Result Option> { Some(Box::new(#code)) @@ -171,5 +169,5 @@ fn expand_miette(ident: &Ident, info: &StructInfo<'_>) -> syn::Result(value: T) { + fn roundtrip(value: &T) { let mut buf = Vec::new(); value.encode(&mut buf); println!("{}: {buf:?}", std::any::type_name::()); let value2 = T::decode(&mut &*buf).unwrap(); - assert_eq!(value, value2); + assert_eq!(*value, value2); } #[test] fn sample() { - roundtrip(sample::Sample { + roundtrip(&sample::Sample { a: 5, b: true, c: ("Test".into(), -2), @@ -134,17 +139,17 @@ mod tests { #[test] fn sample2_unit() { - roundtrip(sample::Sample2::Unit); + roundtrip(&sample::Sample2::Unit); } #[test] fn sample2_tuple() { - roundtrip(sample::Sample2::Tuple(7, 8)); + roundtrip(&sample::Sample2::Tuple(7, 8)); } #[test] fn sample2_fields() { - roundtrip(sample::Sample2::Fields { + roundtrip(&sample::Sample2::Fields { name: "this".into(), valid: true, dates: vec![ @@ -157,7 +162,7 @@ mod tests { #[test] fn sample_gen() { - roundtrip(sample::gens::SampleGen { + roundtrip(&sample::gens::SampleGen { raw: vec![5, 6, 7, 8], array: [9_i16; 4], value: 9, @@ -166,7 +171,7 @@ mod tests { #[test] fn sample_gen2() { - roundtrip(sample::gens::SampleGen2::Value(sample::SampleAlias { + roundtrip(&sample::gens::SampleGen2::Value(sample::SampleAlias { a: 50, b: false, c: (String::new(), -10), @@ -175,7 +180,7 @@ mod tests { #[test] fn specials_options_some() { - roundtrip(sample::specials::SomeOptions { + roundtrip(&sample::specials::SomeOptions { maybe_int: Some(5), maybe_text: Some("hi".into()), maybe_tuple: Some((20, 30)), @@ -186,7 +191,7 @@ mod tests { #[test] fn specials_options_none() { - roundtrip(sample::specials::SomeOptions { + roundtrip(&sample::specials::SomeOptions { maybe_int: None, maybe_text: None, maybe_tuple: None, diff --git a/crates/stef/src/buf/encode.rs b/crates/stef/src/buf/encode.rs index 913808a..936fd4b 100644 --- a/crates/stef/src/buf/encode.rs +++ b/crates/stef/src/buf/encode.rs @@ -53,7 +53,7 @@ pub fn encode_string(w: &mut impl BufMut, value: &str) { pub fn encode_bytes(w: &mut impl BufMut, value: &[u8]) { encode_u64(w, value.len() as u64); - w.put(value) + w.put(value); } pub fn encode_vec(w: &mut W, vec: &[T], encode: E) @@ -125,7 +125,7 @@ where #[inline(always)] pub fn encode_id(w: &mut impl BufMut, id: u32) { - encode_u32(w, id) + encode_u32(w, id); } #[inline(always)] @@ -323,7 +323,7 @@ where { #[inline(always)] fn encode(&self, w: &mut impl BufMut) { - self.0.encode(w) + self.0.encode(w); } } diff --git a/crates/stef/src/buf/mod.rs b/crates/stef/src/buf/mod.rs index fa08543..911a5e6 100644 --- a/crates/stef/src/buf/mod.rs +++ b/crates/stef/src/buf/mod.rs @@ -94,7 +94,7 @@ mod tests { fn non_zero_hash_set_valid() { let mut buf = Vec::new(); encode_hash_set(&mut buf, &HashSet::from_iter([1, 2, 3]), |w, v| { - encode_u32(w, *v) + encode_u32(w, *v); }); assert!(decode_non_zero_hash_set(&mut &*buf, decode_u32).is_ok()); } diff --git a/crates/stef/src/lib.rs b/crates/stef/src/lib.rs index 2cc36c5..dc4a49a 100644 --- a/crates/stef/src/lib.rs +++ b/crates/stef/src/lib.rs @@ -1,3 +1,15 @@ +#![forbid(unsafe_code)] +#![deny(rust_2018_idioms, clippy::all)] +#![warn(clippy::pedantic)] +#![allow( + clippy::cast_possible_truncation, + clippy::implicit_hasher, + clippy::inline_always, + clippy::missing_errors_doc, + clippy::missing_panics_doc, + clippy::module_name_repetitions +)] + use std::{ collections::{HashMap, HashSet}, ops::Deref, @@ -38,6 +50,7 @@ macro_rules! non_zero_collection { impl $(< $($gens),+ >)? NonZero<$name $(< $($gens),+ >)?> { /// Try to create a new non-zero instance, which will succeed if the given collection /// contains in fact some elements. Otherwise `None` is returned. + #[must_use] pub fn new(value: $name $(< $($gens),+ >)?) -> Option { (!value.is_empty()).then_some(Self(value)) } diff --git a/crates/stef/src/varint.rs b/crates/stef/src/varint.rs index 55ffeb7..2a275f6 100644 --- a/crates/stef/src/varint.rs +++ b/crates/stef/src/varint.rs @@ -40,6 +40,7 @@ macro_rules! varint { ($ty:ty, $signed:ty) => { paste::paste! { #[inline] + #[must_use] pub fn [](mut value: $ty) -> ([u8; max_size::<$ty>()], usize) { let mut buf = [0; max_size::<$ty>()]; @@ -72,6 +73,7 @@ macro_rules! varint { } #[inline] + #[must_use] pub fn [](value: $signed) -> ([u8; max_size::<$ty>()], usize) { []([](value)) }