From ff71b49f014613729237178f0d3ea374f7d72cd5 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 19 Jul 2022 12:28:34 +0200 Subject: refactor: use aggressive clippy linting --- clippy.toml | 1 + example/src/main.rs | 27 +++++++++-------- syrette/src/castable_factory.rs | 6 ++-- syrette/src/di_container.rs | 35 +++++++++++++++------- syrette/src/interfaces/factory.rs | 1 + syrette/src/interfaces/injectable.rs | 4 +++ syrette/src/lib.rs | 2 ++ syrette/src/libs/intertrait/cast_box.rs | 8 ++--- syrette/src/libs/intertrait/cast_rc.rs | 8 ++--- syrette/src/libs/intertrait/hasher.rs | 16 ++++++---- syrette/src/libs/intertrait/mod.rs | 8 ++--- syrette/src/provider.rs | 11 +++---- syrette/src/ptr.rs | 2 +- syrette_macros/src/factory_type_alias.rs | 1 + syrette_macros/src/injectable_impl.rs | 3 +- syrette_macros/src/lib.rs | 9 ++++++ .../src/libs/intertrait_macros/gen_caster.rs | 8 ++--- syrette_macros/src/libs/intertrait_macros/mod.rs | 8 ++--- 18 files changed, 100 insertions(+), 58 deletions(-) create mode 100644 clippy.toml diff --git a/clippy.toml b/clippy.toml new file mode 100644 index 0000000..a85655e --- /dev/null +++ b/clippy.toml @@ -0,0 +1 @@ +doc-valid-idents = ["CodeChain", ".."] diff --git a/example/src/main.rs b/example/src/main.rs index 1f4ddb6..d79a030 100644 --- a/example/src/main.rs +++ b/example/src/main.rs @@ -1,3 +1,6 @@ +#![deny(clippy::all)] +#![deny(clippy::pedantic)] + use syrette::errors::di_container::DIContainerError; use syrette::interfaces::factory::IFactory; use syrette::ptr::{FactoryPtr, InterfacePtr}; @@ -58,14 +61,14 @@ trait ICow struct Cow { - _moo_cnt: i32, + moo_cnt: i32, } impl Cow { fn new(moo_cnt: i32) -> Self { - Self { _moo_cnt: moo_cnt } + Self { moo_cnt } } } @@ -73,7 +76,7 @@ impl ICow for Cow { fn moo(&self) { - for _ in 0..self._moo_cnt { + for _ in 0..self.moo_cnt { println!("Moo"); } } @@ -89,9 +92,9 @@ trait IHuman struct Human { - _dog: InterfacePtr, - _cat: InterfacePtr, - _cow_factory: FactoryPtr, + dog: InterfacePtr, + cat: InterfacePtr, + cow_factory: FactoryPtr, } #[injectable(IHuman)] @@ -104,9 +107,9 @@ impl Human ) -> Self { Self { - _dog: dog, - _cat: cat, - _cow_factory: cow_factory, + dog, + cat, + cow_factory, } } } @@ -117,13 +120,13 @@ impl IHuman for Human { println!("Hi doggy!"); - self._dog.woof(); + self.dog.woof(); println!("Hi kitty!"); - self._cat.meow(); + self.cat.meow(); - let cow: Box = (self._cow_factory)(3); + let cow: Box = (self.cow_factory)(3); cow.moo(); } diff --git a/syrette/src/castable_factory.rs b/syrette/src/castable_factory.rs index d820c31..5d582bb 100644 --- a/syrette/src/castable_factory.rs +++ b/syrette/src/castable_factory.rs @@ -9,7 +9,7 @@ where Args: 'static, ReturnInterface: 'static + ?Sized, { - _func: &'static dyn Fn>, + func: &'static dyn Fn>, } impl CastableFactory @@ -21,7 +21,7 @@ where func: &'static dyn Fn>, ) -> Self { - Self { _func: func } + Self { func } } } @@ -40,7 +40,7 @@ where { extern "rust-call" fn call(&self, args: Args) -> Self::Output { - self._func.call(args) + self.func.call(args) } } diff --git a/syrette/src/di_container.rs b/syrette/src/di_container.rs index 86724e6..df51140 100644 --- a/syrette/src/di_container.rs +++ b/syrette/src/di_container.rs @@ -19,8 +19,8 @@ pub struct BindingBuilder<'a, Interface> where Interface: 'static + ?Sized, { - _di_container: &'a mut DIContainer, - _phantom_data: PhantomData, + di_container: &'a mut DIContainer, + interface_phantom: PhantomData, } impl<'a, Interface> BindingBuilder<'a, Interface> @@ -30,8 +30,8 @@ where fn new(di_container: &'a mut DIContainer) -> Self { Self { - _di_container: di_container, - _phantom_data: PhantomData, + di_container, + interface_phantom: PhantomData, } } @@ -43,7 +43,7 @@ where { let interface_typeid = TypeId::of::(); - self._di_container._bindings.insert( + self.di_container.bindings.insert( interface_typeid, Rc::new(InjectableTypeProvider::::new()), ); @@ -63,7 +63,7 @@ where let factory_impl = CastableFactory::new(factory_func); - self._di_container._bindings.insert( + self.di_container.bindings.insert( interface_typeid, Rc::new(FactoryProvider::new(FactoryPtr::new(factory_impl))), ); @@ -80,16 +80,17 @@ where /// ``` pub struct DIContainer { - _bindings: HashMap>, + bindings: HashMap>, } impl<'a> DIContainer { /// Returns a new `DIContainer`. + #[must_use] pub fn new() -> Self { Self { - _bindings: HashMap::new(), + bindings: HashMap::new(), } } @@ -102,6 +103,13 @@ impl<'a> DIContainer } /// Returns a new instance of the type bound with `Interface`. + /// + /// # Errors + /// Will return `Err` if: + /// - No binding for `Interface` exists + /// - Resolving the binding for `Interface` fails + /// - Casting the binding for `Interface` fails + /// - The binding for `Interface` is not injectable pub fn get( &self, ) -> error_stack::Result, DIContainerError> @@ -112,7 +120,7 @@ impl<'a> DIContainer let interface_name = type_name::(); - let binding = self._bindings.get(&interface_typeid).ok_or_else(|| { + let binding = self.bindings.get(&interface_typeid).ok_or_else(|| { Report::new(DIContainerError) .attach_printable(format!("No binding exists for {}", interface_name)) })?; @@ -145,6 +153,13 @@ impl<'a> DIContainer } /// Returns the factory bound with factory type `Interface`. + /// + /// # Errors + /// Will return `Err` if: + /// - No binding for `Interface` exists + /// - Resolving the binding for `Interface` fails + /// - Casting the binding for `Interface` fails + /// - The binding for `Interface` is not a factory pub fn get_factory( &self, ) -> error_stack::Result, DIContainerError> @@ -155,7 +170,7 @@ impl<'a> DIContainer let interface_name = type_name::(); - let binding = self._bindings.get(&interface_typeid).ok_or_else(|| { + let binding = self.bindings.get(&interface_typeid).ok_or_else(|| { Report::new(DIContainerError) .attach_printable(format!("No binding exists for {}", interface_name)) })?; diff --git a/syrette/src/interfaces/factory.rs b/syrette/src/interfaces/factory.rs index 3afd7a8..c97fc09 100644 --- a/syrette/src/interfaces/factory.rs +++ b/syrette/src/interfaces/factory.rs @@ -1,3 +1,4 @@ +#![allow(clippy::module_name_repetitions)] use crate::libs::intertrait::CastFrom; use crate::ptr::InterfacePtr; diff --git a/syrette/src/interfaces/injectable.rs b/syrette/src/interfaces/injectable.rs index 96d4f21..24032a6 100644 --- a/syrette/src/interfaces/injectable.rs +++ b/syrette/src/interfaces/injectable.rs @@ -5,6 +5,10 @@ use crate::DIContainer; pub trait Injectable: CastFrom { + /// Resolves the dependencies of the injectable. + /// + /// # Errors + /// Will return `Err` if resolving the dependencies fails. fn resolve( di_container: &DIContainer, ) -> error_stack::Result, ResolveError> diff --git a/syrette/src/lib.rs b/syrette/src/lib.rs index aee7fe2..992f276 100644 --- a/syrette/src/lib.rs +++ b/syrette/src/lib.rs @@ -1,4 +1,6 @@ #![feature(unboxed_closures, fn_traits)] +#![deny(clippy::all)] +#![deny(clippy::pedantic)] //! Syrette //! diff --git a/syrette/src/libs/intertrait/cast_box.rs b/syrette/src/libs/intertrait/cast_box.rs index d4da740..793efd0 100644 --- a/syrette/src/libs/intertrait/cast_box.rs +++ b/syrette/src/libs/intertrait/cast_box.rs @@ -1,13 +1,13 @@ /** * Originally from Intertrait by CodeChain * - * https://github.com/CodeChain-io/intertrait - * https://crates.io/crates/intertrait/0.2.2 + * + * * * Licensed under either of * - * Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) - * MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) + * Apache License, Version 2.0 (LICENSE-APACHE or ) + * MIT license (LICENSE-MIT or ) * at your option. */ diff --git a/syrette/src/libs/intertrait/cast_rc.rs b/syrette/src/libs/intertrait/cast_rc.rs index 58d212a..79d8d60 100644 --- a/syrette/src/libs/intertrait/cast_rc.rs +++ b/syrette/src/libs/intertrait/cast_rc.rs @@ -1,13 +1,13 @@ /** * Originally from Intertrait by CodeChain * - * https://github.com/CodeChain-io/intertrait - * https://crates.io/crates/intertrait/0.2.2 + * + * * * Licensed under either of * - * Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) - * MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) + * Apache License, Version 2.0 (LICENSE-APACHE or ) + * MIT license (LICENSE-MIT or ) * at your option. */ diff --git a/syrette/src/libs/intertrait/hasher.rs b/syrette/src/libs/intertrait/hasher.rs index 1fe7739..e7f110d 100644 --- a/syrette/src/libs/intertrait/hasher.rs +++ b/syrette/src/libs/intertrait/hasher.rs @@ -1,13 +1,15 @@ +#![allow(clippy::module_name_repetitions)] + /** * Originally from Intertrait by CodeChain * - * https://github.com/CodeChain-io/intertrait - * https://crates.io/crates/intertrait/0.2.2 + * + * * * Licensed under either of * - * Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) - * MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) + * Apache License, Version 2.0 (LICENSE-APACHE or ) + * MIT license (LICENSE-MIT or ) * at your option. */ @@ -34,11 +36,13 @@ impl Hasher for FastHasher let mut bytes = bytes; while bytes.len() > size_of::() { let (u64_bytes, remaining) = bytes.split_at(size_of::()); + self.0 ^= u64::from_ne_bytes(u64_bytes.try_into().unwrap()); - bytes = remaining + + bytes = remaining; } self.0 ^= bytes .iter() - .fold(0u64, |result, b| (result << 8) | *b as u64); + .fold(0u64, |result, b| (result << 8) | u64::from(*b)); } } diff --git a/syrette/src/libs/intertrait/mod.rs b/syrette/src/libs/intertrait/mod.rs index a3f24f1..1daca64 100644 --- a/syrette/src/libs/intertrait/mod.rs +++ b/syrette/src/libs/intertrait/mod.rs @@ -1,13 +1,13 @@ /** * Originally from Intertrait by CodeChain * - * https://github.com/CodeChain-io/intertrait - * https://crates.io/crates/intertrait/0.2.2 + * + * * * Licensed under either of * - * Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) - * MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) + * Apache License, Version 2.0 (LICENSE-APACHE or ) + * MIT license (LICENSE-MIT or ) * at your option. */ diff --git a/syrette/src/provider.rs b/syrette/src/provider.rs index c344f25..3b7e04c 100644 --- a/syrette/src/provider.rs +++ b/syrette/src/provider.rs @@ -1,3 +1,4 @@ +#![allow(clippy::module_name_repetitions)] use std::marker::PhantomData; use crate::castable_factory::AnyFactory; @@ -26,7 +27,7 @@ pub struct InjectableTypeProvider where InjectableType: Injectable, { - _phantom_data: PhantomData, + injectable_phantom: PhantomData, } impl InjectableTypeProvider @@ -36,7 +37,7 @@ where pub fn new() -> Self { Self { - _phantom_data: PhantomData, + injectable_phantom: PhantomData, } } } @@ -58,14 +59,14 @@ where pub struct FactoryProvider { - _factory: FactoryPtr, + factory: FactoryPtr, } impl FactoryProvider { pub fn new(factory: FactoryPtr) -> Self { - Self { _factory: factory } + Self { factory } } } @@ -76,6 +77,6 @@ impl IProvider for FactoryProvider _di_container: &DIContainer, ) -> error_stack::Result { - Ok(Providable::Factory(self._factory.clone())) + Ok(Providable::Factory(self.factory.clone())) } } diff --git a/syrette/src/ptr.rs b/syrette/src/ptr.rs index 164e061..414d086 100644 --- a/syrette/src/ptr.rs +++ b/syrette/src/ptr.rs @@ -1,6 +1,6 @@ +#![allow(clippy::module_name_repetitions)] use std::rc::Rc; pub type InterfacePtr = Box; pub type FactoryPtr = Rc; - diff --git a/syrette_macros/src/factory_type_alias.rs b/syrette_macros/src/factory_type_alias.rs index 82e2315..8ea7baa 100644 --- a/syrette_macros/src/factory_type_alias.rs +++ b/syrette_macros/src/factory_type_alias.rs @@ -11,6 +11,7 @@ pub struct FactoryTypeAlias impl Parse for FactoryTypeAlias { + #[allow(clippy::match_wildcard_for_single_variants)] fn parse(input: ParseStream) -> syn::Result { let type_alias = match input.parse::() { diff --git a/syrette_macros/src/injectable_impl.rs b/syrette_macros/src/injectable_impl.rs index e7d1b54..3000253 100644 --- a/syrette_macros/src/injectable_impl.rs +++ b/syrette_macros/src/injectable_impl.rs @@ -131,6 +131,7 @@ impl InjectableImpl .find(|method_item| method_item.sig.ident == method_name) } + #[allow(clippy::match_wildcard_for_single_variants)] fn get_has_fn_args_self(fn_args: &Punctuated) -> bool { fn_args.iter().any(|arg| match arg { @@ -170,7 +171,7 @@ impl InjectableImpl match opt_colon_two { Some(colon_two) => { - acc.push_str(colon_two.to_token_stream().to_string().as_str()) + acc.push_str(colon_two.to_token_stream().to_string().as_str()); } None => {} } diff --git a/syrette_macros/src/lib.rs b/syrette_macros/src/lib.rs index f686e1e..2981ea7 100644 --- a/syrette_macros/src/lib.rs +++ b/syrette_macros/src/lib.rs @@ -1,3 +1,6 @@ +#![deny(clippy::all)] +#![deny(clippy::pedantic)] + use proc_macro::TokenStream; use quote::quote; use syn::{parse, parse_macro_input}; @@ -20,6 +23,9 @@ use libs::intertrait_macros::gen_caster::generate_caster; /// /// * A interface trait the struct implements. /// +/// # Panics +/// If the attributed item is not a impl. +/// /// # Examples /// ``` /// trait IConfigReader @@ -74,6 +80,9 @@ pub fn injectable(args_stream: TokenStream, impl_stream: TokenStream) -> TokenSt /// Makes a type alias usable as a factory interface. /// +/// # Panics +/// If the attributed item is not a type alias. +/// /// # Examples /// ``` /// trait IUser diff --git a/syrette_macros/src/libs/intertrait_macros/gen_caster.rs b/syrette_macros/src/libs/intertrait_macros/gen_caster.rs index 213a75e..9126200 100644 --- a/syrette_macros/src/libs/intertrait_macros/gen_caster.rs +++ b/syrette_macros/src/libs/intertrait_macros/gen_caster.rs @@ -1,13 +1,13 @@ /** * Originally from Intertrait by CodeChain * - * https://github.com/CodeChain-io/intertrait - * https://crates.io/crates/intertrait/0.2.2 + * + * * * Licensed under either of * - * Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) - * MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) + * Apache License, Version 2.0 (LICENSE-APACHE or ) + * MIT license (LICENSE-MIT or ) * at your option. */ diff --git a/syrette_macros/src/libs/intertrait_macros/mod.rs b/syrette_macros/src/libs/intertrait_macros/mod.rs index da9775a..fa8c021 100644 --- a/syrette_macros/src/libs/intertrait_macros/mod.rs +++ b/syrette_macros/src/libs/intertrait_macros/mod.rs @@ -1,13 +1,13 @@ /** * Originally from Intertrait by CodeChain * - * https://github.com/CodeChain-io/intertrait - * https://crates.io/crates/intertrait/0.2.2 + * + * * * Licensed under either of * - * Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) - * MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) + * Apache License, Version 2.0 (LICENSE-APACHE or ) + * MIT license (LICENSE-MIT or ) * at your option. */ -- cgit v1.2.3-18-g5258