Skip to content

Commit

Permalink
clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
bcantrill committed Nov 25, 2024
1 parent 8845902 commit 512d0f9
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pmbus: A crate for PMBus manipulation

This is a no_std crate that expresses the PMBus protocol, as described in
the PMBus 1.3 specifcation. This crate is intended to be generic with
the PMBus 1.3 specification. This crate is intended to be generic with
respect to implementation and usable by software that will directly
communicate with PMBus devices via SMBus/I2C as well as by software that
merely wishes to make sense of the PMBus protocol (e.g., debuggers or
Expand Down
91 changes: 54 additions & 37 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
//
use anyhow::{bail, Result};

//
// This is code that generates code, and it is therefore a bit of a mess.
// When trying to modify or extend this code, it is most helpful to look
// instead at the generated artifacts themselves: this code has sacrificed
// its own aesthetics to make the generated code (relatively) clean.
//
use convert_case::{Case, Casing};
use ron::de::from_reader;
use serde::Deserialize;
Expand Down Expand Up @@ -262,14 +268,13 @@ fn output_commands(
) -> Result<String> {
let mut s = String::new();

writeln!(&mut s, r##"
pub use num_derive::{{FromPrimitive, ToPrimitive}};
writeln!(&mut s, r##"pub use num_derive::{{FromPrimitive, ToPrimitive}};
pub use num_traits::{{FromPrimitive, ToPrimitive}};"##)?;

if shadowing.is_some() {
writeln!(&mut s, r##"
use crate::VOutModeCommandData;
use crate::Replacement;"##)?;
use crate::Replacement;
use crate::VOutModeCommandData;"##)?;
} else {
writeln!(&mut s, r##"
use crate::Error;"##)?;
Expand Down Expand Up @@ -354,9 +359,9 @@ impl CommandCode {{
match self {{"##)?;

for cmd in &cmds.all {
if cmds.structured.get(&cmd.1).is_none()
&& numerics.get(&cmd.1).is_none()
&& synonyms.get(&cmd.1).is_none()
if !cmds.structured.contains_key(&cmd.1)
&& !numerics.contains(&cmd.1)
&& !synonyms.contains(&cmd.1)
{
continue;
}
Expand Down Expand Up @@ -399,9 +404,9 @@ impl CommandCode {{
match self {{"##)?;

for cmd in &cmds.all {
if cmds.structured.get(&cmd.1).is_none()
&& numerics.get(&cmd.1).is_none()
&& synonyms.get(&cmd.1).is_none()
if !cmds.structured.contains_key(&cmd.1)
&& !numerics.contains(&cmd.1)
&& !synonyms.contains(&cmd.1)
{
continue;
}
Expand Down Expand Up @@ -444,9 +449,9 @@ impl CommandCode {{
match self {{"##)?;

for cmd in &cmds.all {
if cmds.structured.get(&cmd.1).is_none()
&& numerics.get(&cmd.1).is_none()
&& synonyms.get(&cmd.1).is_none()
if !cmds.structured.contains_key(&cmd.1)
&& !numerics.contains(&cmd.1)
&& !synonyms.contains(&cmd.1)
{
continue;
}
Expand Down Expand Up @@ -483,9 +488,9 @@ impl CommandCode {{
match self {{"##)?;

for cmd in &cmds.all {
if cmds.structured.get(&cmd.1).is_none()
&& numerics.get(&cmd.1).is_none()
&& synonyms.get(&cmd.1).is_none()
if !cmds.structured.contains_key(&cmd.1)
&& !numerics.contains(&cmd.1)
&& !synonyms.contains(&cmd.1)
{
continue;
}
Expand Down Expand Up @@ -925,7 +930,9 @@ pub mod {} {{
}
}

writeln!(&mut s, " Value::Unknown(v) => *v as u32,")?;
let cast = if bits != 32 { " as u32" } else { "" };

writeln!(&mut s, " Value::Unknown(v) => *v{cast},")?;
writeln!(&mut s, " }}\n }}\n }}")?;

writeln!(&mut s, r##"
Expand Down Expand Up @@ -1033,10 +1040,14 @@ pub mod {} {{
#[allow(clippy::identity_op)]
pub fn to_slice(&self, slice: &mut [u8]) {{"##)?;

for i in 0..bytes {
writeln!(&mut s,
"{:12}slice[{}] = ((self.0 >> {}) & 0xff) as u8;", "", i, i * 8
)?;
if bytes == 1 {
writeln!(&mut s, "{:12}slice[0] = self.0;", "")?;
} else {
for i in 0..bytes {
writeln!(&mut s,
"{:12}slice[{i}] = ((self.0 >> {}) & 0xff) as u8;", "", i * 8
)?;
}
}

writeln!(&mut s, " }}")?;
Expand Down Expand Up @@ -1265,6 +1276,7 @@ pub mod {} {{
}}
Replacement::Integer(i) => {{
#[allow(clippy::unnecessary_cast)]
if self.set_val(field, i as u{}).is_err() {{
return Err(Error::OverflowReplacement);
}}
Expand Down Expand Up @@ -1319,7 +1331,7 @@ pub mod {} {{
}}
fn raw(&self) -> (u32, Bitwidth) {{
(self.0 as u32, Bitwidth({}))
(self.0{cast}, Bitwidth({}))
}}"##, bits - 1, bits - 1, bits, bits, bits)?;

if !auxiliary {
Expand Down Expand Up @@ -1503,10 +1515,14 @@ pub mod {} {{
#[allow(clippy::identity_op)]
pub fn to_slice(&self, slice: &mut [u8]) {{"##)?;

for i in 0..bytes {
writeln!(&mut s,
"{:12}slice[{}] = ((self.0 >> {}) & 0xff) as u8;", "", i, i * 8
)?;
if bytes == 1 {
writeln!(&mut s, "{:12}slice[0] = self.0;", "")?;
} else {
for i in 0..bytes {
writeln!(&mut s,
"{:12}slice[{i}] = ((self.0 >> {}) & 0xff) as u8;", "", i * 8
)?;
}
}

writeln!(&mut s, " }}")?;
Expand Down Expand Up @@ -1819,26 +1835,26 @@ pub mod {} {{
&dyn crate::Field, &dyn crate::Value
) -> Option<Replacement>
) -> Result<(), Error> {{
let field = crate::WholeField("{} measurement", Bitwidth({}));
let field = crate::WholeField("{cmd} measurement", Bitwidth({bits}));
let val = Value(self.get()?, self.0.into());
if let Some(replacement) = iter(&field, &val) {{
if let Replacement::Float(f) = replacement {{
self.set({}(f))
self.set({units}(f))
}} else {{
Err(Error::InvalidReplacement)
}}
}} else {{
Ok(())
}}
}}"##, cmd, bits, units)?;
}}"##)?;
}

writeln!(&mut s, r##"
fn fields(
mut iter: impl FnMut(&dyn crate::Field)
) -> Result<(), Error> {{
iter(&crate::WholeField("{} measurement", Bitwidth({})));
iter(&crate::WholeField("{cmd} measurement", Bitwidth({bits})));
Ok(())
}}
Expand All @@ -1850,9 +1866,10 @@ pub mod {} {{
Ok(())
}}
#[allow(clippy::unnecessary_cast)]
fn raw(&self) -> (u32, Bitwidth) {{
(self.0 as u32, Bitwidth({}))
}}"##, cmd, bits, bits)?;
(self.0 as u32, Bitwidth({bits}))
}}"##)?;

if !auxiliary {
writeln!(&mut s, r##"
Expand Down Expand Up @@ -2259,7 +2276,7 @@ fn codegen() -> Result<()> {

let out_dir = env::var("OUT_DIR")?;
let dest_path = Path::new(&out_dir).join("commands.rs");
let mut file = File::create(&dest_path)?;
let mut file = File::create(dest_path)?;
let mut units: HashSet<Units> = HashSet::new();

let out = output_commands(&cmds, None)?;
Expand Down Expand Up @@ -2310,7 +2327,7 @@ fn codegen() -> Result<()> {
};

let dest_path = Path::new(&out_dir).join("devices.rs");
let mut dfile = File::create(&dest_path)?;
let mut dfile = File::create(dest_path)?;

let out = output_devices(&devices)?;
dfile.write_all(out.as_bytes())?;
Expand All @@ -2325,7 +2342,7 @@ fn codegen() -> Result<()> {

for (name, device) in all {
let dest_path = Path::new(&out_dir).join(format!("{}.rs", name));
let mut file = File::create(&dest_path)?;
let mut file = File::create(dest_path)?;

let fname = format!("{}.ron", &name);
let f = open_file(&fname)?;
Expand All @@ -2347,7 +2364,7 @@ fn codegen() -> Result<()> {
}

for cmd in &cmds.all {
if h.get(&cmd.0).is_none() {
if !h.contains(&cmd.0) {
dcmds.all.push(cmd.clone());
}
}
Expand Down Expand Up @@ -2448,7 +2465,7 @@ fn codegen() -> Result<()> {
}

let dest_path = Path::new(&out_dir).join("units.rs");
let mut ufile = File::create(&dest_path)?;
let mut ufile = File::create(dest_path)?;

let out = output_units(&units)?;
ufile.write_all(out.as_bytes())?;
Expand Down
3 changes: 2 additions & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[toolchain]
channel = "nightly-2024-04-04"
channel = "1.81.0"
components = ["clippy"]
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ impl Linear11 {

let n = f32::ceil(libm::log2f(n)) as i16;

if n < LINEAR11_N_MIN || n > LINEAR11_N_MAX {
if !(LINEAR11_N_MIN..=LINEAR11_N_MAX).contains(&n) {
None
} else {
let exp = f32::powi(2.0, n.into());
Expand Down Expand Up @@ -386,7 +386,7 @@ impl ULinear16 {
pub fn from_real(x: f32, exp: ULinear16Exponent) -> Option<Self> {
let val = (x / f32::powi(2.0, exp.0.into())).round();

if val > core::u16::MAX as f32 {
if val > u16::MAX as f32 {
None
} else {
Some(Self(val as u16, exp))
Expand Down

0 comments on commit 512d0f9

Please sign in to comment.