Skip to content

Commit

Permalink
Code hygiene according to clippy recommendations.
Browse files Browse the repository at this point in the history
  • Loading branch information
mamrhein committed Jul 26, 2024
1 parent c1b7651 commit 936ca23
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 36 deletions.
1 change: 1 addition & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
allowed-duplicate-crates = ["syn"]
8 changes: 4 additions & 4 deletions qty-macros/src/quantity_attr_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,8 @@ fn codegen_qty_single_unit(
fn iter() -> impl Iterator<Item = Self> {
Self::VARIANTS.iter().cloned()
}
fn name(&self) -> String { #unit_name.to_string() }
fn symbol(&self) -> String { #unit_symbol.to_string() }
fn name(&self) -> String { #unit_name.to_owned() }
fn symbol(&self) -> String { #unit_symbol.to_owned() }
fn si_prefix(&self) -> Option<SIPrefix> { None }
}
#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -533,7 +533,7 @@ fn codegen_fn_name(units: &Vec<UnitDef>) -> TokenStream {
let unit_name = unit.name.clone();
code = quote!(
#code
Self::#unit_ident => #unit_name.to_string(),
Self::#unit_ident => #unit_name.to_owned(),
)
}
quote!(
Expand All @@ -552,7 +552,7 @@ fn codegen_fn_symbol(units: &Vec<UnitDef>) -> TokenStream {
let unit_symbol = unit.symbol.clone();
code = quote!(
#code
Self::#unit_ident => #unit_symbol.to_string(),
Self::#unit_ident => #unit_symbol.to_owned(),
)
}
quote!(
Expand Down
1 change: 1 addition & 0 deletions src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub trait Converter<Q: Quantity> {
/// * to_unit: Q::UnitType,
/// * factor: AmountT,
/// * offset: AmountT
///
/// defining the conversion
/// to_amount = from_amount * factor + offset
#[derive(Debug)]
Expand Down
40 changes: 9 additions & 31 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#![warn(clippy::integer_division)]
#![warn(clippy::manual_assert)]
#![warn(clippy::match_same_arms)]
// #![warn(clippy::mismatching_type_param_order)] TODO: enable when got stable
#![warn(clippy::mismatching_type_param_order)]
#![warn(clippy::missing_const_for_fn)]
#![warn(clippy::missing_errors_doc)]
#![warn(clippy::missing_panics_doc)]
Expand All @@ -58,10 +58,7 @@

extern crate alloc;

use alloc::{
format,
string::{String, ToString},
};
use alloc::{borrow::ToOwned, format, string::String};
use core::{
cmp::Ordering,
fmt,
Expand Down Expand Up @@ -136,12 +133,7 @@ pub trait Unit:
/// there is no such unit.
#[must_use]
fn from_symbol(symbol: &str) -> Option<Self> {
for unit in Self::iter() {
if unit.symbol() == symbol {
return Some(unit);
}
}
None
Self::iter().find(|&unit| unit.symbol() == symbol)
}

/// Returns the name of `self`.
Expand Down Expand Up @@ -178,12 +170,7 @@ pub trait LinearScaledUnit: Unit {
/// if there is no such unit.
#[must_use]
fn from_scale(amnt: AmountT) -> Option<Self> {
for unit in Self::iter() {
if unit.scale() == amnt {
return Some(unit);
}
}
None
Self::iter().find(|&unit| unit.scale() == amnt)
}

/// Returns `true` if `self` is the reference unit of its unit type.
Expand All @@ -208,6 +195,7 @@ pub trait Quantity: Copy + Sized + Mul<AmountT> {
type UnitType: Unit<QuantityType = Self>;

/// Returns an iterator over the variants of `Self::UnitType`.
#[must_use]
fn iter_units() -> impl Iterator<Item = Self::UnitType> {
Self::UnitType::iter()
}
Expand All @@ -216,12 +204,7 @@ pub trait Quantity: Copy + Sized + Mul<AmountT> {
/// there is no such unit.
#[must_use]
fn unit_from_symbol(symbol: &str) -> Option<Self::UnitType> {
for unit in Self::iter_units() {
if unit.symbol() == symbol {
return Some(unit);
}
}
None
Self::iter_units().find(|&unit| unit.symbol() == symbol)
}

/// Returns a new instance of the type implementing `Quantity`.
Expand Down Expand Up @@ -341,12 +324,7 @@ where
/// there is no such unit.
#[must_use]
fn unit_from_scale(amnt: AmountT) -> Option<Self::UnitType> {
for unit in Self::iter_units() {
if unit.scale() == amnt {
return Some(unit);
}
}
None
Self::iter_units().find(|&unit| unit.scale() == amnt)
}

/// Returns `factor` so that `factor` * `unit` == `self`.
Expand Down Expand Up @@ -446,10 +424,10 @@ impl Unit for One {
Self::VARIANTS.iter().cloned()
}
fn name(&self) -> String {
"One".to_string()
"One".to_owned()
}
fn symbol(&self) -> String {
"".to_string()
"".to_owned()
}
fn si_prefix(&self) -> Option<SIPrefix> {
None
Expand Down
2 changes: 1 addition & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! This module reexports all macros and types needed to define a quantity.

#[doc(hidden)]
pub use alloc::string::{String, ToString};
pub use alloc::{borrow::ToOwned, string::String};
#[doc(hidden)]
pub use core::cmp::Ordering;
#[doc(hidden)]
Expand Down

0 comments on commit 936ca23

Please sign in to comment.