Skip to content

Commit

Permalink
A few minor clippy suggestions
Browse files Browse the repository at this point in the history
* Added cargo.toml lints section (will be ignored by older cargo)
* fixed a number of suggestions, and added exceptions to the other ones
* Added `From<i64> for Sign`, and a few more minor improvements
  • Loading branch information
nyurik committed Dec 15, 2023
1 parent 1e8e4e7 commit 5687a2e
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 122 deletions.
18 changes: 18 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,21 @@ push = false
shared-version = true
sign-tag = true
tag = false

[workspace.lints.rust]
unused_qualifications = "warn"

[workspace.lints.clippy]
pedantic = { level = "warn", priority = -1 }
cast_possible_truncation = "allow"
cast_possible_wrap = "allow"
cast_precision_loss = "allow"
cast_sign_loss = "allow"
match_same_arms = "allow"
match_wildcard_for_single_variants = "allow"
missing_errors_doc = "allow"
module_name_repetitions = "allow"
redundant_closure_for_method_calls = "allow"
similar_names = "allow"
too_many_lines = "allow"
wildcard_imports = "allow"
2 changes: 2 additions & 0 deletions serde_with/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
lints.workspace = true

[package]
authors = [
"Jonas Bushart",
Expand Down
2 changes: 1 addition & 1 deletion serde_with/src/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl Alphabet for ImapMutf7 {
}
}

/// The character set used in BinHex 4.0 files.
/// The character set used in `BinHex` 4.0 files.
///
/// See [BinHex 4.0 Definition](http://files.stairways.com/other/binhex-40-specs-info.txt).
pub struct BinHex;
Expand Down
18 changes: 9 additions & 9 deletions serde_with/src/content/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ impl<'de> Content<'de> {
fn unexpected(&self) -> Unexpected<'_> {
match *self {
Content::Bool(b) => Unexpected::Bool(b),
Content::U8(n) => Unexpected::Unsigned(n as u64),
Content::U16(n) => Unexpected::Unsigned(n as u64),
Content::U32(n) => Unexpected::Unsigned(n as u64),
Content::U8(n) => Unexpected::Unsigned(u64::from(n)),
Content::U16(n) => Unexpected::Unsigned(u64::from(n)),
Content::U32(n) => Unexpected::Unsigned(u64::from(n)),
Content::U64(n) => Unexpected::Unsigned(n),
Content::U128(_) => Unexpected::Other("u128"),
Content::I8(n) => Unexpected::Signed(n as i64),
Content::I16(n) => Unexpected::Signed(n as i64),
Content::I32(n) => Unexpected::Signed(n as i64),
Content::I8(n) => Unexpected::Signed(i64::from(n)),
Content::I16(n) => Unexpected::Signed(i64::from(n)),
Content::I32(n) => Unexpected::Signed(i64::from(n)),
Content::I64(n) => Unexpected::Signed(n),
Content::I128(_) => Unexpected::Other("i128"),
Content::F32(f) => Unexpected::Float(f as f64),
Content::F32(f) => Unexpected::Float(f64::from(f)),
Content::F64(f) => Unexpected::Float(f),
Content::Char(c) => Unexpected::Char(c),
Content::String(ref s) => Unexpected::Str(s),
Expand Down Expand Up @@ -765,7 +765,7 @@ where
}
(variant, Some(value))
}
s @ Content::String(_) | s @ Content::Str(_) => (s, None),
s @ (Content::String(_) | Content::Str(_)) => (s, None),
other => {
return Err(DeError::invalid_type(other.unexpected(), &"string or map"));
}
Expand Down Expand Up @@ -1540,7 +1540,7 @@ where
}
(variant, Some(value))
}
ref s @ Content::String(_) | ref s @ Content::Str(_) => (s, None),
ref s @ (Content::String(_) | Content::Str(_)) => (s, None),
ref other => {
return Err(DeError::invalid_type(other.unexpected(), &"string or map"));
}
Expand Down
16 changes: 6 additions & 10 deletions serde_with/src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1578,8 +1578,7 @@ where
Err(err) => err,
};
Err(DeError::custom(format_args!(
"OneOrMany could not deserialize any variant:\n One: {}\n Many: {}",
one_err, many_err
"OneOrMany could not deserialize any variant:\n One: {one_err}\n Many: {many_err}",
)))
}
}
Expand Down Expand Up @@ -1623,8 +1622,7 @@ where
Err(err) => err,
};
Err(DeError::custom(format_args!(
"PickFirst could not deserialize any variant:\n First: {}\n Second: {}",
first_err, second_err
"PickFirst could not deserialize any variant:\n First: {first_err}\n Second: {second_err}",
)))
}
}
Expand Down Expand Up @@ -1662,8 +1660,7 @@ where
Err(err) => err,
};
Err(DeError::custom(format_args!(
"PickFirst could not deserialize any variant:\n First: {}\n Second: {}\n Third: {}",
first_err, second_err, third_err,
"PickFirst could not deserialize any variant:\n First: {first_err}\n Second: {second_err}\n Third: {third_err}",
)))
}
}
Expand Down Expand Up @@ -1708,8 +1705,7 @@ where
Err(err) => err,
};
Err(DeError::custom(format_args!(
"PickFirst could not deserialize any variant:\n First: {}\n Second: {}\n Third: {}\n Fourth: {}",
first_err, second_err, third_err, fourth_err,
"PickFirst could not deserialize any variant:\n First: {first_err}\n Second: {second_err}\n Third: {third_err}\n Fourth: {fourth_err}",
)))
}
}
Expand Down Expand Up @@ -1854,7 +1850,7 @@ impl<'de> DeserializeAs<'de, bool> for BoolFromInt<Strict> {
0 => Ok(false),
1 => Ok(true),
unexp => Err(DeError::invalid_value(
Unexpected::Unsigned(unexp as u64),
Unexpected::Unsigned(u64::from(unexp)),
&"0 or 1",
)),
}
Expand All @@ -1868,7 +1864,7 @@ impl<'de> DeserializeAs<'de, bool> for BoolFromInt<Strict> {
0 => Ok(false),
1 => Ok(true),
unexp => Err(DeError::invalid_value(
Unexpected::Signed(unexp as i64),
Unexpected::Signed(i64::from(unexp)),
&"0 or 1",
)),
}
Expand Down
8 changes: 4 additions & 4 deletions serde_with/src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ pub mod unwrap_or_skip {
///
/// The implementation supports both the [`HashSet`] and the [`BTreeSet`] from the standard library.
///
/// # Converting to serde_as
/// # Converting to `serde_as`
///
/// The same functionality can be more clearly expressed using the `serde_as` macro and [`SetPreventDuplicates`].
/// The `_` is a placeholder which works for any type which implements [`Serialize`]/[`Deserialize`].
Expand Down Expand Up @@ -294,7 +294,7 @@ pub mod sets_duplicate_value_is_error {
///
/// The implementation supports both the [`HashMap`] and the [`BTreeMap`] from the standard library.
///
/// # Converting to serde_as
/// # Converting to `serde_as`
///
/// The same functionality can be more clearly expressed using the `serde_as` macro and [`MapPreventDuplicates`].
/// The `_` is a placeholder which works for any type which implements [`Serialize`]/[`Deserialize`].
Expand Down Expand Up @@ -414,7 +414,7 @@ pub mod maps_duplicate_key_is_error {
///
/// The implementation supports both the [`HashSet`] and the [`BTreeSet`] from the standard library.
///
/// # Converting to serde_as
/// # Converting to `serde_as`
///
/// The same functionality can be more clearly expressed using the `serde_as` macro and [`SetLastValueWins`].
/// The `_` is a placeholder which works for any type which implements [`Serialize`]/[`Deserialize`].
Expand Down Expand Up @@ -502,7 +502,7 @@ pub mod sets_last_value_wins {
/// [`HashMap`]: std::collections::HashMap
/// [`BTreeMap`]: std::collections::HashMap
///
/// # Converting to serde_as
/// # Converting to `serde_as`
///
/// The same functionality can be more clearly expressed using the `serde_as` macro and [`MapFirstKeyWins`].
/// The `_` is a placeholder which works for any type which implements [`Serialize`]/[`Deserialize`].
Expand Down
2 changes: 1 addition & 1 deletion serde_with/src/ser/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ impl<STRICTNESS: Strictness> SerializeAs<bool> for BoolFromInt<STRICTNESS> {
where
S: Serializer,
{
serializer.serialize_u8(*source as u8)
serializer.serialize_u8(u8::from(*source))
}
}

Expand Down
16 changes: 8 additions & 8 deletions serde_with/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,30 +114,30 @@ where
}

pub(crate) fn duration_as_secs_f64(dur: &Duration) -> f64 {
(dur.as_secs() as f64) + (dur.subsec_nanos() as f64) / (NANOS_PER_SEC as f64)
(dur.as_secs() as f64) + f64::from(dur.subsec_nanos()) / f64::from(NANOS_PER_SEC)
}

pub(crate) fn duration_signed_from_secs_f64(secs: f64) -> Result<DurationSigned, &'static str> {
const MAX_NANOS_F64: f64 = ((u64::max_value() as u128 + 1) * (NANOS_PER_SEC as u128)) as f64;
const MAX_NANOS_F64: f64 = ((u64::MAX as u128 + 1) * (NANOS_PER_SEC as u128)) as f64;
// TODO why are the seconds converted to nanoseconds first?
// Does it make sense to just truncate the value?
let mut nanos = secs * (NANOS_PER_SEC as f64);
let mut nanos = secs * (f64::from(NANOS_PER_SEC));
if !nanos.is_finite() {
return Err("got non-finite value when converting float to duration");
}
if nanos >= MAX_NANOS_F64 {
return Err("overflow when converting float to duration");
}
let mut sign = self::duration::Sign::Positive;
let mut sign = Sign::Positive;
if nanos < 0.0 {
nanos = -nanos;
sign = self::duration::Sign::Negative;
sign = Sign::Negative;
}
let nanos = nanos as u128;
Ok(self::duration::DurationSigned::new(
Ok(DurationSigned::new(
sign,
(nanos / (NANOS_PER_SEC as u128)) as u64,
(nanos % (NANOS_PER_SEC as u128)) as u32,
(nanos / u128::from(NANOS_PER_SEC)) as u64,
(nanos % u128::from(NANOS_PER_SEC)) as u32,
))
}

Expand Down
37 changes: 19 additions & 18 deletions serde_with/src/utils/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,36 @@ pub(crate) enum Sign {

impl Sign {
#[allow(dead_code)]
pub(crate) fn is_positive(&self) -> bool {
*self == Sign::Positive
pub(crate) fn is_positive(self) -> bool {
self == Sign::Positive
}

#[allow(dead_code)]
pub(crate) fn is_negative(&self) -> bool {
*self == Sign::Negative
pub(crate) fn is_negative(self) -> bool {
self == Sign::Negative
}

pub(crate) fn apply<T>(&self, value: T) -> T
pub(crate) fn apply<T>(self, value: T) -> T
where
T: core::ops::Neg<Output = T>,
{
match *self {
match self {
Sign::Positive => value,
Sign::Negative => value.neg(),
}
}
}

impl From<i64> for Sign {
fn from(value: i64) -> Self {
if value.is_negative() {
Sign::Negative
} else {
Sign::Positive
}
}
}

#[derive(Copy, Clone)]
pub(crate) struct DurationSigned {
pub(crate) sign: Sign,
Expand Down Expand Up @@ -356,13 +366,8 @@ impl<'de> DeserializeAs<'de, DurationSigned> for DurationSeconds<i64, Strict> {
where
D: Deserializer<'de>,
{
i64::deserialize(deserializer).map(|secs: i64| {
let sign = match secs.is_negative() {
true => Sign::Negative,
false => Sign::Positive,
};
DurationSigned::new(sign, secs.abs_diff(0), 0)
})
i64::deserialize(deserializer)
.map(|secs: i64| DurationSigned::new(secs.into(), secs.abs_diff(0), 0))
}
}

Expand Down Expand Up @@ -398,11 +403,7 @@ impl<'de> DeserializeAs<'de, DurationSigned> for DurationSeconds<String, Strict>
E: DeError,
{
let secs: i64 = value.parse().map_err(DeError::custom)?;
let sign = match secs.is_negative() {
true => Sign::Negative,
false => Sign::Positive,
};
Ok(DurationSigned::new(sign, secs.abs_diff(0), 0))
Ok(DurationSigned::new(secs.into(), secs.abs_diff(0), 0))
}
}

Expand Down
4 changes: 2 additions & 2 deletions serde_with/src/with_prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ where
}

fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
self.delegate
.collect_str(&format_args!("{}{}", self.prefix, v))
let prefix = self.prefix;
self.delegate.collect_str(&format_args!("{prefix}{v}"))
}

fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> {
Expand Down
2 changes: 2 additions & 0 deletions serde_with_macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
lints.workspace = true

[package]
authors = ["Jonas Bushart"]
name = "serde_with_macros"
Expand Down
6 changes: 3 additions & 3 deletions serde_with_macros/src/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ impl Parse for ApplyInput {
}

pub fn apply(args: TokenStream, input: TokenStream) -> TokenStream {
let args = syn::parse_macro_input!(args as ApplyInput);

#[derive(FromMeta)]
struct SerdeContainerOptions {
#[darling(rename = "crate")]
alt_crate_path: Option<Path>,
}

let args = syn::parse_macro_input!(args as ApplyInput);

let container_options = match SerdeContainerOptions::from_list(&args.metas) {
Ok(v) => v,
Err(e) => {
Expand Down Expand Up @@ -101,7 +101,7 @@ fn prepare_apply_attribute_to_field(
return Ok(());
}

for matcher in input.rules.iter() {
for matcher in &input.rules {
if ty_pattern_matches_ty(&matcher.ty, &field.ty) {
field.attrs.extend(matcher.attrs.clone());
}
Expand Down
Loading

0 comments on commit 5687a2e

Please sign in to comment.