From 08504535b46ec041f5ff61cf4b316cdd8037c95e Mon Sep 17 00:00:00 2001 From: eZio Pan Date: Wed, 10 Jan 2024 13:35:52 +0800 Subject: [PATCH] refactor with clippy --- build.rs | 4 ++-- src/generate/device.rs | 2 +- src/generate/mod.rs | 2 +- src/ir.rs | 11 +++-------- src/main.rs | 28 ++++++++++------------------ src/transform/rename.rs | 8 ++++---- 6 files changed, 21 insertions(+), 34 deletions(-) diff --git a/build.rs b/build.rs index f930857..4360712 100644 --- a/build.rs +++ b/build.rs @@ -37,7 +37,7 @@ fn commit_info() -> String { fn commit_hash() -> Result { Ok(String::from_utf8( Command::new("git") - .args(&["rev-parse", "--short", "HEAD"]) + .args(["rev-parse", "--short", "HEAD"]) .output()? .stdout, )?) @@ -46,7 +46,7 @@ fn commit_hash() -> Result { fn commit_date() -> Result { Ok(String::from_utf8( Command::new("git") - .args(&["log", "-1", "--date=short", "--pretty=format:%cd"]) + .args(["log", "-1", "--date=short", "--pretty=format:%cd"]) .output()? .stdout, )?) diff --git a/src/generate/device.rs b/src/generate/device.rs index 138ac8b..88a8b81 100644 --- a/src/generate/device.rs +++ b/src/generate/device.rs @@ -48,7 +48,7 @@ pub fn render(_opts: &super::Options, ir: &IR, d: &Device, path: &str) -> Result for p in sorted(&d.peripherals, |p| p.base_address) { let name = Ident::new(&p.name, span); - let address = util::hex(p.base_address as u64); + let address = util::hex(p.base_address); let doc = util::doc(&p.description); if let Some(block_name) = &p.block { diff --git a/src/generate/mod.rs b/src/generate/mod.rs index 824084f..885ab31 100644 --- a/src/generate/mod.rs +++ b/src/generate/mod.rs @@ -45,7 +45,7 @@ impl Module { res.extend(self.items.clone()); for (name, module) in sorted_map(&self.children, |name, _| name.clone()) { - let name = Ident::new(&name, span); + let name = Ident::new(name, span); let contents = module.render()?; res.extend(quote! { pub mod #name { diff --git a/src/ir.rs b/src/ir.rs index 74efb8f..7c79bed 100644 --- a/src/ir.rs +++ b/src/ir.rs @@ -3,7 +3,7 @@ use serde::{de, de::Visitor, ser::SerializeMap, Deserialize, Deserializer, Seria use std::collections::{BTreeMap, HashMap}; use std::fmt; -#[derive(Clone, Debug, PartialEq)] +#[derive(Default, Clone, Debug, PartialEq)] pub struct IR { pub devices: HashMap, pub blocks: HashMap, @@ -13,12 +13,7 @@ pub struct IR { impl IR { pub fn new() -> Self { - Self { - devices: HashMap::new(), - blocks: HashMap::new(), - fieldsets: HashMap::new(), - enums: HashMap::new(), - } + Self::default() } pub fn merge(&mut self, other: IR) { @@ -263,7 +258,7 @@ impl<'de> Visitor<'de> for IRVisitor { // into our map. while let Some(key) = access.next_key()? { let key: String = key; - let (kind, name) = key.split_once("/").ok_or(de::Error::custom("item names must be in form `kind/name`, where kind is `block`, `fieldset` or `enum`"))?; + let (kind, name) = key.split_once('/').ok_or(de::Error::custom("item names must be in form `kind/name`, where kind is `block`, `fieldset` or `enum`"))?; match kind { "block" => { let val: Block = access.next_value()?; diff --git a/src/main.rs b/src/main.rs index b7981e9..de8c135 100755 --- a/src/main.rs +++ b/src/main.rs @@ -303,11 +303,9 @@ fn fmt(args: Fmt) -> Result<()> { if args.remove_unused { let mut used_enums = HashSet::new(); - for (_, fs) in &mut ir.fieldsets { - for f in &mut fs.fields { - if let Some(enumm) = &f.enumm { - used_enums.insert(enumm.clone()); - } + for fs in ir.fieldsets.values_mut() { + for f in fs.fields.iter_mut().filter(|f| f.enumm.is_some()) { + used_enums.insert(f.enumm.as_ref().unwrap().clone()); } } @@ -325,23 +323,23 @@ fn fmt(args: Fmt) -> Result<()> { } }; - for (_, b) in &mut ir.blocks { + for b in ir.blocks.values_mut() { cleanup(&mut b.description); - for i in &mut b.items { + for i in b.items.iter_mut() { cleanup(&mut i.description); } } - for (_, b) in &mut ir.fieldsets { + for b in ir.fieldsets.values_mut() { cleanup(&mut b.description); - for i in &mut b.fields { + for i in b.fields.iter_mut() { cleanup(&mut i.description); } } - for (_, b) in &mut ir.enums { + for b in ir.enums.values_mut() { cleanup(&mut b.description); - for i in &mut b.variants { + for i in b.variants.iter_mut() { cleanup(&mut i.description); } } @@ -404,7 +402,7 @@ fn gen_block(args: GenBlock) -> Result<()> { Ok(()) } -#[derive(serde::Serialize, serde::Deserialize)] +#[derive(Default, serde::Serialize, serde::Deserialize)] struct Config { transforms: Vec, } @@ -415,9 +413,3 @@ impl FromIterator for Config { Self { transforms } } } - -impl Default for Config { - fn default() -> Self { - Self { transforms: vec![] } - } -} diff --git a/src/transform/rename.rs b/src/transform/rename.rs index 05a07a1..82cb9e4 100644 --- a/src/transform/rename.rs +++ b/src/transform/rename.rs @@ -19,10 +19,10 @@ impl Rename { } }; - super::map_device_names(ir, &renamer); - super::map_block_names(ir, &renamer); - super::map_fieldset_names(ir, &renamer); - super::map_enum_names(ir, &renamer); + super::map_device_names(ir, renamer); + super::map_block_names(ir, renamer); + super::map_fieldset_names(ir, renamer); + super::map_enum_names(ir, renamer); Ok(()) }