Skip to content

Commit

Permalink
Merge pull request #813 from rust-embedded/clippy-happy
Browse files Browse the repository at this point in the history
Make clippy happy
  • Loading branch information
burrbull authored Feb 18, 2024
2 parents fe1e230 + 2176940 commit 41b9ca1
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 66 deletions.
11 changes: 4 additions & 7 deletions ci/svd2rust-regress/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,16 @@ pub struct Test {
impl Test {
fn run(&self, opts: &Opts) -> Result<(), anyhow::Error> {
match self {
Self { url: Some(url), .. } => {}
Self { url: Some(_), .. } => {}
Self {
svd_file: Some(svd_file),
..
} => {}
Self {
chip: Some(chip), ..
svd_file: Some(_), ..
} => {}
Self { chip: Some(_), .. } => {}
_ => unreachable!("clap should not allow this"),
}
let test = if let (Some(url), Some(arch)) = (&self.url, &self.arch) {
tests::TestCase {
arch: svd2rust::Target::parse(&arch)?,
arch: svd2rust::Target::parse(arch)?,
mfgr: tests::Manufacturer::Unknown,
chip: self
.chip
Expand Down
2 changes: 1 addition & 1 deletion ci/svd2rust-regress/src/svd_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{anyhow, Context, Result};
use svd2rust::{util::Case, Target};

use crate::{command::CommandExt, tests::TestCase, Opts, TestAll};
use crate::{command::CommandExt, tests::TestCase, Opts};
use std::io::prelude::*;
use std::path::PathBuf;
use std::process::Command;
Expand Down
8 changes: 4 additions & 4 deletions src/generate/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
Peripheral::Single(_p) => {
let p_name = util::name_of(p, config.ignore_groups);
let p_feature = feature_format.apply(&p_name);
let p_ty = ident(&p_name, &config, "peripheral", span);
let p_singleton = ident(&p_name, &config, "peripheral_singleton", span);
let p_ty = ident(&p_name, config, "peripheral", span);
let p_singleton = ident(&p_name, config, "peripheral_singleton", span);
if config.feature_peripheral {
feature_attribute.extend(quote! { #[cfg(feature = #p_feature)] })
};
Expand All @@ -253,8 +253,8 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
Peripheral::Array(p, dim_element) => {
for p_name in names(p, dim_element) {
let p_feature = feature_format.apply(&p_name);
let p_ty = ident(&p_name, &config, "peripheral", span);
let p_singleton = ident(&p_name, &config, "peripheral_singleton", span);
let p_ty = ident(&p_name, config, "peripheral", span);
let p_singleton = ident(&p_name, config, "peripheral_singleton", span);
if config.feature_peripheral {
feature_attribute.extend(quote! { #[cfg(feature = #p_feature)] })
};
Expand Down
2 changes: 1 addition & 1 deletion src/generate/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn render(
}
pos += 1;

let i_ty = ident(&interrupt.0.name, &config, "interrupt", span);
let i_ty = ident(&interrupt.0.name, config, "interrupt", span);
let description = format!(
"{} - {}",
interrupt.0.value,
Expand Down
36 changes: 18 additions & 18 deletions src/generate/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result

let name = util::name_of(&p, config.ignore_groups);
let span = Span::call_site();
let p_ty = ident(&name, &config, "peripheral", span);
let p_ty = ident(&name, config, "peripheral", span);
let name_str = p_ty.to_string();
let address = util::hex(p.base_address + config.base_address_shift);
let description = util::respace(p.description.as_ref().unwrap_or(&p.name));
Expand Down Expand Up @@ -85,7 +85,7 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
for pi in svd::peripheral::expand(p, dim) {
let name = &pi.name;
let description = pi.description.as_deref().unwrap_or(&p.name);
let p_ty = ident(name, &config, "peripheral", span);
let p_ty = ident(name, config, "peripheral", span);
let name_str = p_ty.to_string();
let doc_alias = (&name_str != name).then(|| quote!(#[doc(alias = #name)]));
let address = util::hex(pi.base_address + config.base_address_shift);
Expand Down Expand Up @@ -640,8 +640,8 @@ fn register_or_cluster_block(

let mut doc_alias = None;
let block_ty = if let Some(name) = name {
let ty = ident(name, &config, "cluster", span);
if ty.to_string() != name {
let ty = ident(name, config, "cluster", span);
if ty != name {
doc_alias = Some(quote!(#[doc(alias = #name)]));
}
ty
Expand Down Expand Up @@ -996,12 +996,12 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
util::replace_suffix(&cluster.name, "")
};
let span = Span::call_site();
let ty = name_to_ty(ident(&ty_name, &config, "cluster", span));
let ty = name_to_ty(ident(&ty_name, config, "cluster", span));

match cluster {
Cluster::Single(info) => {
let doc = make_comment(cluster_size, info.address_offset, &description);
let name: Ident = ident(&info.name, &config, "cluster_accessor", span);
let name: Ident = ident(&info.name, config, "cluster_accessor", span);
let syn_field = new_syn_field(name.clone(), ty.clone());
cluster_expanded.push(RegisterBlockField {
syn_field,
Expand Down Expand Up @@ -1050,7 +1050,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
} else {
&ty_name
},
&config,
config,
"cluster_accessor",
span,
);
Expand Down Expand Up @@ -1083,7 +1083,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
});
if !sequential_indexes_from0 || !ends_with_index {
for (i, ci) in svd::cluster::expand(info, array_info).enumerate() {
let idx_name = ident(&ci.name, &config, "cluster_accessor", span);
let idx_name = ident(&ci.name, config, "cluster_accessor", span);
let doc = make_comment(
cluster_size,
ci.address_offset,
Expand Down Expand Up @@ -1125,7 +1125,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
ci.address_offset,
ci.description.as_deref().unwrap_or(&ci.name),
);
let name = ident(&ci.name, &config, "cluster_accessor", span);
let name = ident(&ci.name, config, "cluster_accessor", span);
let syn_field = new_syn_field(name.clone(), ty.clone());

cluster_expanded.push(RegisterBlockField {
Expand Down Expand Up @@ -1176,8 +1176,8 @@ fn expand_register(
Register::Single(info) => {
let doc = make_comment(register_size, info.address_offset, &description);
let span = Span::call_site();
let ty = name_to_ty(ident(&ty_str, &config, "register", span));
let name: Ident = ident(&ty_name, &config, "register_accessor", span);
let ty = name_to_ty(ident(&ty_str, config, "register", span));
let name: Ident = ident(&ty_name, config, "register_accessor", span);
let syn_field = new_syn_field(name.clone(), ty.clone());
register_expanded.push(RegisterBlockField {
syn_field,
Expand Down Expand Up @@ -1232,18 +1232,18 @@ fn expand_register(
let array_convertible = ac && sequential_addresses;
let array_proxy_convertible = ac && disjoint_sequential_addresses;
let span = Span::call_site();
let ty = name_to_ty(ident(&ty_str, &config, "register", span));
let ty = name_to_ty(ident(&ty_str, config, "register", span));

if array_convertible || array_proxy_convertible {
let accessor_name = if let Some(dim_name) = array_info.dim_name.as_ref() {
ident(
&util::fullname(dim_name, &info.alternate_group, config.ignore_groups),
&config,
config,
"register_accessor",
span,
)
} else {
ident(&ty_name, &config, "register_accessor", span)
ident(&ty_name, config, "register_accessor", span)
};
let doc = make_comment(
register_size * array_info.dim,
Expand Down Expand Up @@ -1276,7 +1276,7 @@ fn expand_register(
for (i, ri) in svd::register::expand(info, array_info).enumerate() {
let idx_name = ident(
&util::fullname(&ri.name, &info.alternate_group, config.ignore_groups),
&config,
config,
"cluster_accessor",
span,
);
Expand Down Expand Up @@ -1321,7 +1321,7 @@ fn expand_register(
info.address_offset,
ri.description.as_deref().unwrap_or(&ri.name),
);
let name = ident(&ri.name, &config, "register_accessor", span);
let name = ident(&ri.name, config, "register_accessor", span);
let syn_field = new_syn_field(name.clone(), ty.clone());

register_expanded.push(RegisterBlockField {
Expand Down Expand Up @@ -1404,7 +1404,7 @@ fn cluster_block(
// name_snake_case needs to take into account array type.
let span = Span::call_site();
let mod_ty = ident(&mod_name, config, "cluster_mod", span);
let block_ty = ident(&mod_name, &config, "cluster", span);
let block_ty = ident(&mod_name, config, "cluster", span);

if let Some(dpath) = dpath {
let dparent = dpath.parent().unwrap();
Expand All @@ -1418,7 +1418,7 @@ fn cluster_block(
derived
.path
.segments
.push(path_segment(ident(&dname, &config, "cluster", span)));
.push(path_segment(ident(&dname, config, "cluster", span)));
mod_derived
.path
.segments
Expand Down
22 changes: 11 additions & 11 deletions src/generate/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use anyhow::{anyhow, Result};
use syn::punctuated::Punctuated;

fn regspec(name: &str, config: &Config, span: Span) -> Ident {
ident(name, &config, "register_spec", span)
ident(name, config, "register_spec", span)
}

fn field_accessor(name: &str, config: &Config, span: Span) -> Ident {
Expand Down Expand Up @@ -60,8 +60,8 @@ pub fn render(
}
}
let span = Span::call_site();
let reg_ty = ident(&name, &config, "register", span);
let doc_alias = (&reg_ty.to_string() != &name).then(|| quote!(#[doc(alias = #name)]));
let reg_ty = ident(&name, config, "register", span);
let doc_alias = (reg_ty.to_string().as_str() != name).then(|| quote!(#[doc(alias = #name)]));
let mod_ty = ident(&name, config, "register_mod", span);
let description = util::escape_special_chars(
util::respace(&register.description.clone().unwrap_or_else(|| {
Expand All @@ -82,7 +82,7 @@ pub fn render(
derived
.path
.segments
.push(path_segment(ident(&dname, &config, "register", span)));
.push(path_segment(ident(&dname, config, "register", span)));
mod_derived
.path
.segments
Expand Down Expand Up @@ -656,7 +656,7 @@ pub fn fields(
{
ident(
evs.name.as_deref().unwrap_or(&name),
&config,
config,
"enum_name",
span,
)
Expand All @@ -666,7 +666,7 @@ pub fn fields(
};

// name of read proxy type
let reader_ty = ident(&name, &config, "field_reader", span);
let reader_ty = ident(&name, config, "field_reader", span);

// if it's enumeratedValues and it's derived from base, don't derive the read proxy
// as the base has already dealt with this;
Expand Down Expand Up @@ -861,7 +861,7 @@ pub fn fields(
evs_r = Some(evs);
// generate pub use field_1 reader as field_2 reader
let base_field = util::replace_suffix(&base.field.name, "");
let base_r = ident(&base_field, &config, "field_reader", span);
let base_r = ident(&base_field, config, "field_reader", span);
if !reader_derives.contains(&reader_ty) {
let base_path = base_syn_path(base, &fpath, &base_r, config)?;
mod_items.extend(quote! {
Expand Down Expand Up @@ -977,14 +977,14 @@ pub fn fields(
} else {
"enum_name"
};
ident(evs.name.as_deref().unwrap_or(&name), &config, fmt, span)
ident(evs.name.as_deref().unwrap_or(&name), config, fmt, span)
} else {
// raw_field_value_write_ty
fty.clone()
};

// name of write proxy type
let writer_ty = ident(&name, &config, "field_writer", span);
let writer_ty = ident(&name, config, "field_writer", span);

let mut proxy_items = TokenStream::new();
let mut unsafety = unsafety(f.write_constraint.as_ref(), width);
Expand Down Expand Up @@ -1136,7 +1136,7 @@ pub fn fields(

// generate pub use field_1 writer as field_2 writer
let base_field = util::replace_suffix(&base.field.name, "");
let base_w = ident(&base_field, &config, "field_writer", span);
let base_w = ident(&base_field, config, "field_writer", span);
if !writer_derives.contains(&writer_ty) {
let base_path = base_syn_path(base, &fpath, &base_w, config)?;
mod_items.extend(quote! {
Expand Down Expand Up @@ -1304,7 +1304,7 @@ impl Variant {
.description
.clone()
.unwrap_or_else(|| format!("`{value:b}`")),
pc: ident(&ev.name, &config, "enum_value", span),
pc: ident(&ev.name, config, "enum_value", span),
is_sc,
sc,
value,
Expand Down
10 changes: 5 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn parse_configs(app: Command) -> Result<Config> {

if let Some(ident_formats) = ident_formats.get_many::<String>("ident_format") {
for f in ident_formats {
let mut f = f.split(":");
let mut f = f.split(':');
if let (Some(n), Some(p), Some(c), Some(s)) = (f.next(), f.next(), f.next(), f.next()) {
let case = match c {
"" | "unchanged" | "svd" => None,
Expand Down Expand Up @@ -335,23 +335,23 @@ Ignore this option if you are not building your own FPGA based soft-cores."),
let mut features = Vec::new();
if config.feature_group {
features.extend(
util::group_names(&device, &feature_format)
util::group_names(&device, feature_format)
.iter()
.map(|s| format!("{s} = []\n")),
);
let add_groups: Vec<_> = util::group_names(&device, &feature_format)
let add_groups: Vec<_> = util::group_names(&device, feature_format)
.iter()
.map(|s| format!("\"{s}\""))
.collect();
features.push(format!("all-groups = [{}]\n", add_groups.join(",")))
}
if config.feature_peripheral {
features.extend(
util::peripheral_names(&device, &feature_format)
util::peripheral_names(&device, feature_format)
.iter()
.map(|s| format!("{s} = []\n")),
);
let add_peripherals: Vec<_> = util::peripheral_names(&device, &feature_format)
let add_peripherals: Vec<_> = util::peripheral_names(&device, feature_format)
.iter()
.map(|s| format!("\"{s}\""))
.collect();
Expand Down
34 changes: 15 additions & 19 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,22 @@ fn to_pascal_case(s: &str) -> String {
if let Some(&"") = parts.peek() {
string.push('_');
}
loop {
if let Some(p) = parts.next() {
if p.is_empty() {
continue;
while let Some(p) = parts.next() {
if p.is_empty() {
continue;
}
string.push_str(&p.to_pascal_case());
match parts.peek() {
Some(nxt)
if p.ends_with(|s: char| s.is_numeric())
&& nxt.starts_with(|s: char| s.is_numeric()) =>
{
string.push('_');
}
string.push_str(&p.to_pascal_case());
match parts.peek() {
Some(nxt)
if p.ends_with(|s: char| s.is_numeric())
&& nxt.starts_with(|s: char| s.is_numeric()) =>
{
string.push('_');
}
Some(&"") => {
string.push('_');
}
_ => {}
Some(&"") => {
string.push('_');
}
} else {
break;
_ => {}
}
}
string
Expand Down Expand Up @@ -310,7 +306,7 @@ pub fn block_path_to_ty(
span,
)));
for ps in &bpath.path {
segments.push(path_segment(ident(&ps, config, "cluster_mod", span)));
segments.push(path_segment(ident(ps, config, "cluster_mod", span)));
}
type_path(segments)
}
Expand Down

0 comments on commit 41b9ca1

Please sign in to comment.