Skip to content

address clippy lints #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
20bb82c
remove needless borrows (clippy::needless_borrow)
danieleades Apr 5, 2023
1c50bd3
use pointer args (clippy::ptr_arg)
danieleades Apr 5, 2023
1fb2adf
collapse nested if-else blocks (clippy::collapsible_else_if)
danieleades Apr 5, 2023
72fd44a
remove useless conversions (clippy::useless_conversion)
danieleades Apr 5, 2023
50a27be
prefer 'is_empty' to comparison to empty slice (clippy::len_zero)
danieleades Apr 5, 2023
9959f39
remove unnecessary lazy evaluations (clippy::unnecessary_lazy_evaluat…
danieleades Apr 5, 2023
4c4a873
collapse nested match blocks (clippy::collapsible_match)
danieleades Apr 5, 2023
bfaca16
avoid making assertions on constants (clippy::assertions_on_constants)
danieleades Apr 5, 2023
59afd2a
prefer 'map' to 'and_then/ok(...)' (clippy::bind_instead_of_map)
danieleades Apr 5, 2023
7026b89
use 'to_vec' (clippy::iter_cloned_collect)
danieleades Apr 5, 2023
0a83f03
don't box a string
danieleades Apr 5, 2023
eed5583
don't manually implement derivable impls (clippy::derivable_impls)
danieleades Apr 5, 2023
17df13a
use 'ptr::add' (clippy::ptr_offset_with_cast)
danieleades Apr 5, 2023
718b4a0
don't use 'into_iter' where 'iter' will do (clippy::into_iter_on_ref)
danieleades Apr 5, 2023
fb9fe97
use inline format args (clippy::uninlined_format_args)
danieleades Apr 5, 2023
a92fc57
remove explicit iter loops (clippy::explicit_iter_loop)
danieleades Apr 5, 2023
2b971a1
use let-else blocks (clippy::manual_let_else)
danieleades Apr 5, 2023
241a40a
remove needless pass by value (clippy::needless_pass_by_value)
danieleades Apr 5, 2023
b7d4319
use lossless conversions where available (clippy::cast_lossless)
danieleades Apr 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tree-buf-macros/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn impl_struct_decode(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStrea
let mut news_parallel_rhs = quote! {};
let mut is_first = true;

for NamedField { ident, ty, .. } in fields.iter() {
for NamedField { ident, ty, .. } in &fields {
if is_first {
is_first = false;
parallel_lhs = quote! { #ident };
Expand Down
2 changes: 1 addition & 1 deletion tree-buf-macros/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn impl_struct_encode(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStrea

// See also: fadaec14-35ad-4dc1-b6dc-6106ab811669
let (prefix, suffix) = match num_fields {
0..=8 => (quote! {}, Ident::new(format!("Obj{}", num_fields).as_str(), Span::call_site())),
0..=8 => (quote! {}, Ident::new(format!("Obj{num_fields}").as_str(), Span::call_site())),
_ => (
quote! {
::tree_buf::internal::encodings::varint::encode_prefix_varint(#num_fields as u64 - 9, stream.bytes);
Expand Down
9 changes: 4 additions & 5 deletions tree-buf-macros/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use {
// TODO: Unfortunately, the current method is quite inadequate. Consider a language with no case. Consider a letter 'q' having
// neither uppercase nor lowercase. qq vs q_q is different. But, in this encoding they are the same.
pub fn canonical_ident(ident: &Ident) -> String {
let ident_str = format!("{}", ident);
let ident_str = format!("{ident}");
to_camel_case(&ident_str)
}

Expand All @@ -26,9 +26,8 @@ pub type NamedFields<'a> = Vec<NamedField<'a>>;

pub fn get_named_fields(data_struct: &DataStruct) -> NamedFields {
// TODO: Lift restriction
let fields_named = match &data_struct.fields {
Fields::Named(fields_named) => fields_named,
_ => panic!("The struct must have named fields"),
let Fields::Named(fields_named) = &data_struct.fields else {
panic!("The struct must have named fields")
};

fields_named
Expand All @@ -39,7 +38,7 @@ pub fn get_named_fields(data_struct: &DataStruct) -> NamedFields {
NamedField {
ident: field.ident.as_ref().unwrap(),
ty: &field.ty,
canon_str: canonical_ident(&ident),
canon_str: canonical_ident(ident),
}
})
.collect()
Expand Down
2 changes: 1 addition & 1 deletion tree-buf/src/experimental/scratch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ pub fn scratch<T: Encodable>() -> Scratch {
Scratch { buffers: Default::default() }
}

pub fn encode_into_with_scratch<T: Encodable>(_value: &T, _scratch: &mut Scratch, _into: &mut Vec<u8>) {
pub fn encode_into_with_scratch<T: Encodable>(_value: &T, _scratch: &mut Scratch, _into: &mut [u8]) {
todo!()
}
100 changes: 49 additions & 51 deletions tree-buf/src/experimental/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ struct Path {

impl Path {
fn c(s: &String, x: &impl fmt::Display) -> String {
let x = format!("{}", x);
let x = format!("{x}");
if s.is_empty() {
x
} else if x.is_empty() {
s.clone()
} else {
if x.is_empty() {
s.clone()
} else {
format!("{}.{}", s, x)
}
format!("{s}.{x}")
}
}

Expand Down Expand Up @@ -59,13 +57,13 @@ impl fmt::Display for SizeBreakdown {
by_type.sort_by_key(|i| usize::MAX - i.1.size);

writeln!(f, "Largest by path:")?;
for (path, agg) in by_path.iter() {
for (path, agg) in &by_path {
writeln!(f, "\t{}\n\t {}\n\t {}", agg.size, path, agg.types)?;
}

writeln!(f)?;
writeln!(f, "Largest by type:")?;
for (t, agg) in by_type.iter() {
for (t, agg) in &by_type {
writeln!(f, "\t {}x {} @ {}", agg.count, agg.size, t)?;
}

Expand Down Expand Up @@ -99,95 +97,95 @@ impl SizeBreakdown {
}

// TODO: (Security) Re-write without recursion
fn visit_array(path: Path, branch: &DynArrayBranch, breakdown: &mut SizeBreakdown) {
fn visit_array(path: &Path, branch: &DynArrayBranch, breakdown: &mut SizeBreakdown) {
match branch {
DynArrayBranch::ArrayFixed { values, len } => visit_array(path.a(&format!("[{}]", len), &"Array Fixed"), values, breakdown),
DynArrayBranch::ArrayFixed { values, len } => visit_array(&path.a(&format!("[{len}]"), &"Array Fixed"), values, breakdown),
DynArrayBranch::Array { len, values } => {
visit_array(path.a(&"len", &"Array"), len, breakdown);
visit_array(path.a(&"values", &"Array"), values, breakdown);
visit_array(&path.a(&"len", &"Array"), len, breakdown);
visit_array(&path.a(&"values", &"Array"), values, breakdown);
}
DynArrayBranch::Enum { discriminants, variants } => {
visit_array(path.a(&"discriminants", &"Enum"), discriminants, breakdown);
visit_array(&path.a(&"discriminants", &"Enum"), discriminants, breakdown);
for variant in variants.iter() {
visit_array(path.a(&variant.ident, &"Enum"), &variant.data, breakdown);
visit_array(&path.a(&variant.ident, &"Enum"), &variant.data, breakdown);
}
}
DynArrayBranch::Boolean(enc) => match enc {
ArrayBool::Packed(b) => breakdown.add(&path, "Packed Boolean", b),
ArrayBool::RLE(_first, runs) => visit_array(path.a(&"runs", &"Bool RLE"), runs, breakdown),
ArrayBool::Packed(b) => breakdown.add(path, "Packed Boolean", b),
ArrayBool::RLE(_first, runs) => visit_array(&path.a(&"runs", &"Bool RLE"), runs, breakdown),
},
DynArrayBranch::Float(f) => match f {
ArrayFloat::DoubleGorilla(b) => breakdown.add(&path, "Gorilla", b),
ArrayFloat::F32(b) => breakdown.add(&path, "Fixed F32", b),
ArrayFloat::F64(b) => breakdown.add(&path, "Fixed F64", b),
ArrayFloat::Zfp32(b) => breakdown.add(&path, "Zfp 64", b),
ArrayFloat::Zfp64(b) => breakdown.add(&path, "Zfp 32", b),
ArrayFloat::DoubleGorilla(b) => breakdown.add(path, "Gorilla", b),
ArrayFloat::F32(b) => breakdown.add(path, "Fixed F32", b),
ArrayFloat::F64(b) => breakdown.add(path, "Fixed F64", b),
ArrayFloat::Zfp32(b) => breakdown.add(path, "Zfp 64", b),
ArrayFloat::Zfp64(b) => breakdown.add(path, "Zfp 32", b),
},
DynArrayBranch::Integer(ArrayInteger { bytes, encoding }) => match encoding {
ArrayIntegerEncoding::PrefixVarInt => breakdown.add(&path, "Prefix Varint", bytes),
ArrayIntegerEncoding::Simple16 => breakdown.add(&path, "Simple16", bytes),
ArrayIntegerEncoding::U8 => breakdown.add(&path, "U8 Fixed", bytes),
ArrayIntegerEncoding::DeltaZig => breakdown.add(&path, "DeltaZig", bytes),
ArrayIntegerEncoding::PrefixVarInt => breakdown.add(path, "Prefix Varint", bytes),
ArrayIntegerEncoding::Simple16 => breakdown.add(path, "Simple16", bytes),
ArrayIntegerEncoding::U8 => breakdown.add(path, "U8 Fixed", bytes),
ArrayIntegerEncoding::DeltaZig => breakdown.add(path, "DeltaZig", bytes),
},
DynArrayBranch::Map { len, keys, values } => {
visit_array(path.a(&"len", &"Map"), len, breakdown);
visit_array(path.a(&"keys", &"Map"), keys, breakdown);
visit_array(path.a(&"values", &"Map"), values, breakdown);
visit_array(&path.a(&"len", &"Map"), len, breakdown);
visit_array(&path.a(&"keys", &"Map"), keys, breakdown);
visit_array(&path.a(&"values", &"Map"), values, breakdown);
}
DynArrayBranch::Object { fields } => {
for (name, field) in fields {
visit_array(path.a(name, &"Object"), field, breakdown);
visit_array(&path.a(name, &"Object"), field, breakdown);
}
}
DynArrayBranch::RLE { runs, values } => {
visit_array(path.a(&"runs", &"RLE"), runs, breakdown);
visit_array(path.a(&"values", &"RLE"), values, breakdown);
visit_array(&path.a(&"runs", &"RLE"), runs, breakdown);
visit_array(&path.a(&"values", &"RLE"), values, breakdown);
}
DynArrayBranch::Dictionary { indices, values } => {
visit_array(path.a(&"indices", &"Dictionary"), indices, breakdown);
visit_array(path.a(&"values", &"Dictionary"), values, breakdown);
visit_array(&path.a(&"indices", &"Dictionary"), indices, breakdown);
visit_array(&path.a(&"values", &"Dictionary"), values, breakdown);
}
DynArrayBranch::String(b) => breakdown.add(&path, "UTF-8", b),
DynArrayBranch::String(b) => breakdown.add(path, "UTF-8", b),
DynArrayBranch::BrotliUtf8 { utf8, lens } => {
breakdown.add(&path, "BrotliUtf8", utf8);
visit_array(path.a(&"lens", &"Dictionary"), lens, breakdown);
breakdown.add(path, "BrotliUtf8", utf8);
visit_array(&path.a(&"lens", &"Dictionary"), lens, breakdown);
}
DynArrayBranch::Tuple { fields } => {
for (i, field) in fields.iter().enumerate() {
visit_array(path.a(&i, &"Tuple"), field, breakdown);
visit_array(&path.a(&i, &"Tuple"), field, breakdown);
}
}
DynArrayBranch::Nullable { opt, values } => {
visit_array(path.a(&"opt", &"Nullable"), opt, breakdown);
visit_array(path.a(&"values", &"Nullable"), values, breakdown);
visit_array(&path.a(&"opt", &"Nullable"), opt, breakdown);
visit_array(&path.a(&"values", &"Nullable"), values, breakdown);
}
DynArrayBranch::Void | DynArrayBranch::Map0 | DynArrayBranch::Array0 => {}
}
}

fn visit(path: Path, branch: &DynRootBranch<'_>, breakdown: &mut SizeBreakdown) {
fn visit(path: &Path, branch: &DynRootBranch<'_>, breakdown: &mut SizeBreakdown) {
match branch {
DynRootBranch::Object { fields } => {
for (name, value) in fields.iter() {
visit(path.a(name, &"Object"), value, breakdown);
visit(&path.a(name, &"Object"), value, breakdown);
}
}
DynRootBranch::Enum { discriminant, value } => visit(path.a(discriminant, &"Enum"), value, breakdown),
DynRootBranch::Enum { discriminant, value } => visit(&path.a(discriminant, &"Enum"), value, breakdown),
DynRootBranch::Map { len: _, keys, values } => {
visit_array(path.a(&"keys", &"Map"), keys, breakdown);
visit_array(path.a(&"values", &"Values"), values, breakdown);
visit_array(&path.a(&"keys", &"Map"), keys, breakdown);
visit_array(&path.a(&"values", &"Values"), values, breakdown);
}
DynRootBranch::Tuple { fields } => {
for (i, field) in fields.iter().enumerate() {
visit(path.a(&i, &"Tuple"), field, breakdown);
visit(&path.a(&i, &"Tuple"), field, breakdown);
}
}
DynRootBranch::Map1 { key, value } => {
visit(path.a(&"key", &"Map1"), key, breakdown);
visit(path.a(&"value", &"Map1"), value, breakdown);
visit(&path.a(&"key", &"Map1"), key, breakdown);
visit(&path.a(&"value", &"Map1"), value, breakdown);
}
DynRootBranch::Array { len, values } => visit_array(path.a(&format!("[{}]", len), &"Array"), values, breakdown),
DynRootBranch::Array1(item) => visit(path.a(&"1", &"Array1"), item, breakdown),
DynRootBranch::Array { len, values } => visit_array(&path.a(&format!("[{len}]"), &"Array"), values, breakdown),
DynRootBranch::Array1(item) => visit(&path.a(&"1", &"Array1"), item, breakdown),
DynRootBranch::Boolean(_)
| DynRootBranch::Array0
| DynRootBranch::Map0
Expand Down Expand Up @@ -274,7 +272,7 @@ pub fn size_breakdown(data: &[u8]) -> DecodeResult<String> {
by_type: HashMap::new(),
total: data.len(),
};
visit(Path::default(), &root, &mut breakdown);
visit(&Path::default(), &root, &mut breakdown);

Ok(format!("{}", breakdown))
Ok(format!("{breakdown}"))
}
13 changes: 4 additions & 9 deletions tree-buf/src/internal/branch/array_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Deref for Bytes<'_> {
type Target = [u8];
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
self.0
}
}

Expand All @@ -50,7 +50,7 @@ pub struct ArrayEnumVariant<'a> {
pub data: DynArrayBranch<'a>,
}

#[derive(Debug)]
#[derive(Debug, Default)]
pub enum DynArrayBranch<'a> {
Object {
fields: HashMap<Ident<'a>, DynArrayBranch<'a>>,
Expand Down Expand Up @@ -80,6 +80,7 @@ pub enum DynArrayBranch<'a> {
},
Boolean(ArrayBool<'a>),
Float(ArrayFloat<'a>),
#[default]
Void,
String(Bytes<'a>),
BrotliUtf8 {
Expand Down Expand Up @@ -112,7 +113,7 @@ pub fn decode_next_array<'a>(bytes: &'a [u8], offset: &'_ mut usize, lens: &'_ m
use ArrayTypeId::*;

fn decode_ints<'a>(bytes: &'a [u8], offset: &'_ mut usize, lens: &'_ mut usize, encoding: ArrayIntegerEncoding) -> DecodeResult<DynArrayBranch<'a>> {
let bytes = decode_bytes_from_len(bytes, offset, lens)?.into();
let bytes = decode_bytes_from_len(bytes, offset, lens)?;
Ok(DynArrayBranch::Integer(ArrayInteger { bytes, encoding }))
}

Expand Down Expand Up @@ -276,12 +277,6 @@ pub fn decode_next_array<'a>(bytes: &'a [u8], offset: &'_ mut usize, lens: &'_ m
Ok(branch)
}

impl<'a> Default for DynArrayBranch<'a> {
fn default() -> Self {
DynArrayBranch::Void
}
}

impl_type_id!(ArrayTypeId, [
Nullable: 1,
ArrayVar: 2,
Expand Down
2 changes: 1 addition & 1 deletion tree-buf/src/internal/branch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub trait TypeId: Copy + Into<u8> + PartialEq + std::fmt::Debug {
#[cfg(feature = "decode")]
pub fn decode_root(bytes: &[u8]) -> DecodeResult<DynRootBranch<'_>> {
profile_fn!(decode_root);
if bytes.len() == 0 {
if bytes.is_empty() {
return Ok(DynRootBranch::Void);
}
let mut lens = bytes.len() - 1;
Expand Down
33 changes: 14 additions & 19 deletions tree-buf/src/internal/branch/root_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::convert::{TryFrom, TryInto};
// TODO: Other kinds of self-description may also be interesting, since this is for data self-description is higher value
// TODO: Bytes/Blog = [u8] compressed (eg: gzip), uncompressed

#[derive(Debug)]
#[derive(Debug, Default)]
pub enum DynRootBranch<'a> {
Object {
fields: HashMap<Ident<'a>, DynRootBranch<'a>>,
Expand All @@ -37,6 +37,7 @@ pub enum DynRootBranch<'a> {
Integer(RootInteger),
Boolean(bool),
Float(RootFloat),
#[default]
Void,
String(&'a str),
Map0,
Expand Down Expand Up @@ -193,12 +194,6 @@ pub fn decode_next_root<'a>(bytes: &'a [u8], offset: &'_ mut usize, lens: &'_ mu
Ok(branch)
}

impl<'a> Default for DynRootBranch<'a> {
fn default() -> Self {
DynRootBranch::Void
}
}

#[derive(Debug)]
pub enum RootInteger {
S(i64),
Expand Down Expand Up @@ -249,46 +244,46 @@ impl RootInteger {
pub fn new(bytes: &[u8], offset: &mut usize, len: usize, signed: bool) -> DecodeResult<Self> {
let bytes = decode_bytes(len, bytes, offset)?;
let ok = match (len, signed) {
(1, true) => Self::S((bytes[0] as i64) * -1),
(1, true) => Self::S(-i64::from(bytes[0])),
(1, false) => Self::U(bytes[0].into()),
(2, true) => Self::S(u16::from_le_bytes(bytes.try_into().unwrap()) as i64 * -1),
(2, true) => Self::S(-i64::from(u16::from_le_bytes(bytes.try_into().unwrap()))),
(2, false) => Self::U(u16::from_le_bytes(bytes.try_into().unwrap()).into()),
(3, false) => Self::U({
let b = [bytes[0], bytes[1], bytes[2], 0];
u32::from_le_bytes(b).into()
}),
(3, true) => Self::S({
let b = [bytes[0], bytes[1], bytes[2], 0];
u32::from_le_bytes(b) as i64 * -1
-i64::from(u32::from_le_bytes(b))
}),
(4, true) => Self::S(u32::from_le_bytes(bytes.try_into().unwrap()) as i64 * -1),
(4, true) => Self::S(-i64::from(u32::from_le_bytes(bytes.try_into().unwrap()))),
(4, false) => Self::U(u32::from_le_bytes(bytes.try_into().unwrap()).into()),
(5, false) => Self::U({
let b = [bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], 0, 0, 0];
u64::from_le_bytes(b).into()
u64::from_le_bytes(b)
}),
(5, true) => Self::S({
let b = [bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], 0, 0, 0];
u64::from_le_bytes(b) as i64 * -1
-(u64::from_le_bytes(b) as i64)
}),
(6, false) => Self::U({
let b = [bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], 0, 0];
u64::from_le_bytes(b).into()
u64::from_le_bytes(b)
}),
(6, true) => Self::S({
let b = [bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], 0, 0];
u64::from_le_bytes(b) as i64 * -1
-(u64::from_le_bytes(b) as i64)
}),
(7, false) => Self::U({
let b = [bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], 0];
u64::from_le_bytes(b).into()
u64::from_le_bytes(b)
}),
(7, true) => Self::S({
let b = [bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], 0];
u64::from_le_bytes(b) as i64 * -1
-(u64::from_le_bytes(b) as i64)
}),
(8, true) => Self::S(u64::from_le_bytes(bytes.try_into().unwrap()) as i64 * -1),
(8, false) => Self::U(u64::from_le_bytes(bytes.try_into().unwrap()).into()),
(8, true) => Self::S(-(u64::from_le_bytes(bytes.try_into().unwrap()) as i64)),
(8, false) => Self::U(u64::from_le_bytes(bytes.try_into().unwrap())),
_ => unreachable!(),
};
Ok(ok)
Expand Down
Loading