Skip to content

Commit

Permalink
Lint with clippy.
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciech-graj committed Jul 27, 2024
1 parent 076b90b commit 1993aa4
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 55 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
with:
toolchain: stable
profile: minimal
components: clippy

- name: Set up Rust - Nightly
uses: actions-rs/toolchain@v1
Expand All @@ -28,6 +29,9 @@ jobs:
- name: Test
run: cargo test

- name: Clippy
run: cargo clippy

- name: Bench
working-directory: bench
run: cargo +nightly bench
Expand Down
20 changes: 11 additions & 9 deletions bin-proto-derive/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub enum Tag {
}

impl Attrs {
#[allow(clippy::too_many_lines)]
pub fn validate_enum(&self, span: Span) -> Result<()> {
if self.discriminant_type.is_none() {
return Err(Error::new(
Expand Down Expand Up @@ -147,6 +148,7 @@ impl Attrs {
impl TryFrom<&[syn::Attribute]> for Attrs {
type Error = syn::Error;

#[allow(clippy::too_many_lines)]
fn try_from(value: &[syn::Attribute]) -> Result<Self> {
let meta_lists = value.iter().filter_map(|attr| match attr.parse_meta() {
Ok(syn::Meta::List(meta_list)) => {
Expand All @@ -163,7 +165,7 @@ impl TryFrom<&[syn::Attribute]> for Attrs {

let mut attribs = Attrs::default();
for meta_list in meta_lists {
for meta in meta_list.nested.iter() {
for meta in &meta_list.nested {
match meta {
syn::NestedMeta::Meta(syn::Meta::NameValue(name_value)) => match name_value
.path
Expand All @@ -172,23 +174,23 @@ impl TryFrom<&[syn::Attribute]> for Attrs {
Some(ident) => match ident.to_string().as_str() {
"discriminant_type" => {
attribs.discriminant_type =
Some(meta_name_value_to_parse(name_value)?)
Some(meta_name_value_to_parse(name_value)?);
}
"discriminant" => {
attribs.discriminant = Some(meta_name_value_to_parse(name_value)?)
attribs.discriminant = Some(meta_name_value_to_parse(name_value)?);
}
"ctx" => attribs.ctx = Some(meta_name_value_to_parse(name_value)?),
"ctx_bounds" => {
attribs.ctx_bounds =
Some(meta_name_value_to_punctuated(name_value)?)
Some(meta_name_value_to_punctuated(name_value)?);
}
"bits" => attribs.bits = Some(meta_name_value_to_u32(name_value)?),
"write_value" => {
attribs.write_value = Some(meta_name_value_to_parse(name_value)?)
attribs.write_value = Some(meta_name_value_to_parse(name_value)?);
}
"tag" => {
attribs.tag =
Some(Tag::External(meta_name_value_to_parse(name_value)?))
Some(Tag::External(meta_name_value_to_parse(name_value)?));
}
_ => return Err(Error::new(ident.span(), "unrecognised attribute")),
},
Expand All @@ -209,7 +211,7 @@ impl TryFrom<&[syn::Attribute]> for Attrs {
syn::NestedMeta::Meta(syn::Meta::List(list)) => {
let mut typ = None;
let mut write_value = None;
for nested in list.nested.iter() {
for nested in &list.nested {
let name_value =
if let syn::NestedMeta::Meta(syn::Meta::NameValue(name_value)) =
nested
Expand All @@ -229,7 +231,7 @@ impl TryFrom<&[syn::Attribute]> for Attrs {
match ident.to_string().as_str() {
"type" => typ = Some(meta_name_value_to_parse(name_value)?),
"write_value" => {
write_value = Some(meta_name_value_to_parse(name_value)?)
write_value = Some(meta_name_value_to_parse(name_value)?);
}
_ => {
return Err(Error::new(
Expand All @@ -244,7 +246,7 @@ impl TryFrom<&[syn::Attribute]> for Attrs {
attribs.tag = Some(Tag::Prepend {
typ,
write_value: value,
})
});
}
_ => return Err(Error::new(list.span(), "Tag lacks type or value.")),
}
Expand Down
2 changes: 1 addition & 1 deletion bin-proto-derive/src/codegen/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub fn bind_fields_pattern(parent_name: &syn::Ident, fields: &syn::Fields) -> To
}
syn::Fields::Unnamed(ref fields_unnamed) => {
let binding_names: Vec<_> = (0..fields_unnamed.unnamed.len())
.map(|i| syn::Ident::new(&format!("field_{}", i), Span::call_site()))
.map(|i| syn::Ident::new(format!("field_{i}").as_str(), Span::call_site()))
.collect();

let field_refs: Vec<_> = binding_names.iter().map(|i| quote!( ref #i )).collect();
Expand Down
4 changes: 2 additions & 2 deletions bin-proto-derive/src/codegen/trait_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub enum TraitImplType {

pub fn impl_trait_for(
ast: &syn::DeriveInput,
impl_body: TokenStream,
typ: TraitImplType,
impl_body: &TokenStream,
typ: &TraitImplType,
) -> TokenStream {
let name = &ast.ident;
let attribs = match Attrs::try_from(ast.attrs.as_slice()) {
Expand Down
24 changes: 15 additions & 9 deletions bin-proto-derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![deny(clippy::pedantic)]
#![allow(clippy::module_name_repetitions)]

#[macro_use]
extern crate quote;

Expand All @@ -8,23 +11,25 @@ mod plan;
use attr::Attrs;
use codegen::trait_impl::{impl_trait_for, TraitImplType};
use proc_macro2::TokenStream;
use syn::parse_macro_input;

use crate::codegen::enums::{read_discriminant, variant_discriminant, write_discriminant};

#[derive(Clone, Copy)]
enum Operation {
Read,
Write,
}

#[proc_macro_derive(ProtocolRead, attributes(protocol))]
pub fn protocol_read(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let ast: syn::DeriveInput = syn::parse(input).expect("Failed to parse input");
let ast: syn::DeriveInput = parse_macro_input!(input as syn::DeriveInput);
impl_protocol(&ast, Operation::Read).into()
}

#[proc_macro_derive(ProtocolWrite, attributes(protocol))]
pub fn protocol_write(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let ast: syn::DeriveInput = syn::parse(input).expect("Failed to parse input");
let ast: syn::DeriveInput = parse_macro_input!(input as syn::DeriveInput);
impl_protocol(&ast, Operation::Write).into()
}

Expand Down Expand Up @@ -83,7 +88,7 @@ fn impl_for_struct(
}
};

impl_trait_for(ast, impl_body, trait_type)
impl_trait_for(ast, &impl_body, &trait_type)
}

fn impl_for_enum(
Expand Down Expand Up @@ -117,8 +122,8 @@ fn impl_for_enum(
);
let externally_tagged_read_impl = impl_trait_for(
ast,
impl_body,
TraitImplType::TaggedRead(discriminant_ty.clone()),
&impl_body,
&TraitImplType::TaggedRead(discriminant_ty.clone()),
);

let read_discriminant = read_discriminant(&attribs);
Expand All @@ -132,7 +137,7 @@ fn impl_for_enum(
<Self as ::bin_proto::TaggedRead<_, _>>::read(__io_reader, __byte_order, __ctx, __tag)
}
);
let protocol_read_impl = impl_trait_for(ast, impl_body, TraitImplType::ProtocolRead);
let protocol_read_impl = impl_trait_for(ast, &impl_body, &TraitImplType::ProtocolRead);

quote!(
#externally_tagged_read_impl
Expand All @@ -153,7 +158,7 @@ fn impl_for_enum(
}
);
let externally_tagged_write_impl =
impl_trait_for(ast, impl_body, TraitImplType::UntaggedWrite);
impl_trait_for(ast, &impl_body, &TraitImplType::UntaggedWrite);

let variant_discriminant = variant_discriminant(&plan, &attribs);
let impl_body = quote!(
Expand All @@ -164,7 +169,7 @@ fn impl_for_enum(
#variant_discriminant
}
);
let discriminable_impl = impl_trait_for(ast, impl_body, TraitImplType::Discriminable);
let discriminable_impl = impl_trait_for(ast, &impl_body, &TraitImplType::Discriminable);

let write_discriminant = write_discriminant(&attribs);
let impl_body = quote!(
Expand All @@ -178,7 +183,8 @@ fn impl_for_enum(
<Self as ::bin_proto::UntaggedWrite<_>>::write(self, __io_writer, __byte_order, __ctx)
}
);
let protocol_write_impl = impl_trait_for(ast, impl_body, TraitImplType::ProtocolWrite);
let protocol_write_impl =
impl_trait_for(ast, &impl_body, &TraitImplType::ProtocolWrite);

quote!(
#externally_tagged_write_impl
Expand Down
2 changes: 1 addition & 1 deletion bin-proto/src/bit_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ where
}

fn byte_align(&mut self) {
bitstream_io::BitRead::byte_align(self)
bitstream_io::BitRead::byte_align(self);
}

fn read_u8(&mut self) -> io::Result<u8> {
Expand Down
10 changes: 8 additions & 2 deletions bin-proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
//! arr_len: u8,
//! #[protocol(tag = "arr_len as usize")]
//! arr: Vec<u8>,
//! #[protocol(tag(type = "u16", write_value = "self.prefixed_arr.len() as u16"))]
//! #[protocol(tag(type = "u16", write_value = "self.prefixed_arr.len() as u16"))]
//! prefixed_arr: Vec<u8>,
//! #[protocol(flexible_array_member)]
//! read_to_end: Vec<u8>,
Expand All @@ -39,7 +39,7 @@
//! | 0b0001, // enum_: V1 (0001)
//! 0x02, // arr_len: 2
//! 0x21, 0x37, // arr: [0x21, 0x37]
//! 0x00, 0x01, 0x33, // prefixed_arr: [0x33]
//! 0x00, 0x01, 0x33, // prefixed_arr: [0x33]
//! 0x01, 0x02, 0x03, // read_to_end: [0x01, 0x02, 0x03]
//! ], bin_proto::ByteOrder::BigEndian).unwrap(),
//! S {
Expand All @@ -54,6 +54,12 @@
//! );
//! ```
#![deny(clippy::pedantic)]
#![allow(
clippy::module_name_repetitions,
clippy::missing_errors_doc,
clippy::implicit_hasher
)]
pub use self::bit_field::{BitFieldRead, BitFieldWrite};
pub use self::bit_read::BitRead;
pub use self::bit_write::BitWrite;
Expand Down
50 changes: 21 additions & 29 deletions bin-proto/src/types/numerics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl<Ctx> BitFieldRead<Ctx> for bool {

impl<Ctx> BitFieldWrite<Ctx> for bool {
fn write(&self, write: &mut dyn BitWrite, _: ByteOrder, _: &mut Ctx, bits: u32) -> Result<()> {
write.write_u8_bf(bits, if *self { 1 } else { 0 })?;
write.write_u8_bf(bits, (*self).into())?;
Ok(())
}
}
Expand All @@ -31,7 +31,7 @@ impl<Ctx> ProtocolRead<Ctx> for bool {

impl<Ctx> ProtocolWrite<Ctx> for bool {
fn write(&self, write: &mut dyn BitWrite, _: ByteOrder, _: &mut Ctx) -> Result<()> {
write.write_u8(if *self { 1 } else { 0 })?;
write.write_u8((*self).into())?;
Ok(())
}
}
Expand Down Expand Up @@ -62,24 +62,16 @@ impl<Ctx> ProtocolWrite<Ctx> for i8 {
}
}

macro_rules! impl_for_numeric_cast {
($e:expr, $ty:ty, $cast_ty:ty) => {
$e as $ty
};
($e:expr,) => {
$e
};
}

macro_rules! impl_protocol_for_numeric {
($ty:ty => [$read_fn:ident : $write_fn:ident] $(as $cast_ty:ty)?) => {
($ty:ty => [$read_fn:ident : $write_fn:ident]) => {
impl<Ctx> $crate::ProtocolRead<Ctx> for $ty {
#[allow(clippy::needless_question_mark)]
fn read(
read: &mut dyn $crate::BitRead,
byte_order: $crate::ByteOrder,
_: &mut Ctx,
) -> $crate::Result<Self> {
Ok(impl_for_numeric_cast!(byte_order.$read_fn(read)?, $($ty, $cast_ty)?))
Ok(byte_order.$read_fn(read)?.try_into().unwrap())
}
}

Expand All @@ -90,23 +82,23 @@ macro_rules! impl_protocol_for_numeric {
byte_order: $crate::ByteOrder,
_: &mut Ctx,
) -> $crate::Result<()> {
byte_order.$write_fn(*self $(as $cast_ty)?, write)?;
byte_order.$write_fn((*self).try_into().unwrap(), write)?;
Ok(())
}
}
};
}

macro_rules! impl_bitfield_for_numeric {
($ty:ty => [$read_fn:ident : $write_fn:ident] $(as $cast_ty:ty)?) => {
($ty:ty => [$read_fn:ident : $write_fn:ident]) => {
impl<Ctx> $crate::BitFieldRead<Ctx> for $ty {
fn read(
read: &mut dyn $crate::BitRead,
_: $crate::ByteOrder,
_: &mut Ctx,
bits: u32,
) -> $crate::Result<Self> {
Ok(impl_for_numeric_cast!($crate::BitRead::$read_fn(read, bits)?, $($ty, $cast_ty)?))
Ok($crate::BitRead::$read_fn(read, bits)?.try_into().unwrap())
}
}

Expand All @@ -118,7 +110,7 @@ macro_rules! impl_bitfield_for_numeric {
_: &mut Ctx,
bits: u32,
) -> $crate::Result<()> {
$crate::BitWrite::$write_fn(write, bits, *self $(as $cast_ty)?)?;
$crate::BitWrite::$write_fn(write, bits, (*self).try_into().unwrap())?;
Ok(())
}
}
Expand All @@ -145,24 +137,24 @@ impl_bitfield_for_numeric!(i32 => [read_i32_bf : write_i32_bf]);

#[cfg(target_pointer_width = "16")]
mod size {
impl_protocol_for_numeric!(usize => [read_u16 : write_u16] as u16);
impl_bitfield_for_numeric!(usize => [read_u16_bf : write_u16_bf] as u16);
impl_protocol_for_numeric!(isize => [read_i16 : write_i16] as i16);
impl_bitfield_for_numeric!(isize => [read_i16_bf : write_i16_bf] as i16);
impl_protocol_for_numeric!(usize => [read_u16 : write_u16]);
impl_bitfield_for_numeric!(usize => [read_u16_bf : write_u16_bf]);
impl_protocol_for_numeric!(isize => [read_i16 : write_i16]);
impl_bitfield_for_numeric!(isize => [read_i16_bf : write_i16_bf]);
}

#[cfg(target_pointer_width = "32")]
mod size {
impl_protocol_for_numeric!(usize => [read_u32 : write_u32] as u32);
impl_bitfield_for_numeric!(usize => [read_u32_bf : write_u32_bf] as u32);
impl_protocol_for_numeric!(isize => [read_i32 : write_i32] as i32);
impl_bitfield_for_numeric!(isize => [read_i32_bf : write_i32_bf] as i32);
impl_protocol_for_numeric!(usize => [read_u32 : write_u32]);
impl_bitfield_for_numeric!(usize => [read_u32_bf : write_u32_bf]);
impl_protocol_for_numeric!(isize => [read_i32 : write_i32]);
impl_bitfield_for_numeric!(isize => [read_i32_bf : write_i32_bf]);
}

#[cfg(target_pointer_width = "64")]
mod size {
impl_protocol_for_numeric!(usize => [read_u64 : write_u64] as u64);
impl_bitfield_for_numeric!(usize => [read_u64_bf : write_u64_bf] as u64);
impl_protocol_for_numeric!(isize => [read_i64 : write_i64] as i64);
impl_bitfield_for_numeric!(isize => [read_i64_bf : write_i64_bf] as i64);
impl_protocol_for_numeric!(usize => [read_u64 : write_u64]);
impl_bitfield_for_numeric!(usize => [read_u64_bf : write_u64_bf]);
impl_protocol_for_numeric!(isize => [read_i64 : write_i64]);
impl_bitfield_for_numeric!(isize => [read_i64_bf : write_i64_bf]);
}
4 changes: 2 additions & 2 deletions bin-proto/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ where
Ok(elements)
}

/// BitWrites an iterator of parcels to the stream.
/// `BitWrites` an iterator of parcels to the stream.
///
/// Does not include a length prefix.
pub fn write_items<'a, Ctx, T>(
Expand All @@ -34,7 +34,7 @@ pub fn write_items<'a, Ctx, T>(
where
T: ProtocolWrite<Ctx> + 'a,
{
for item in items.into_iter() {
for item in items {
item.write(write, byte_order, ctx)?;
}
Ok(())
Expand Down

0 comments on commit 1993aa4

Please sign in to comment.