Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A few minor clippy suggestions #669

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/publish-crates-io.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
# This pulls from the "Get Changelog Entry" step above, referencing it's ID to get its outputs object.
# This pulls from the "Get Changelog Entry" step above, referencing its ID to get its outputs object.
# See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps
tag_name: "v${{ steps.changelog_reader.outputs.version }}"
name: "serde_with v${{ steps.changelog_reader.outputs.version }}"
Expand Down
23 changes: 23 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,26 @@ 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"
items_after_statements = "allow"
linkedlist = "allow"
match_same_arms = "allow"
match_wildcard_for_single_variants = "allow"
missing_errors_doc = "allow"
missing_panics_doc = "allow"
module_name_repetitions = "allow"
needless_pass_by_value = "allow"
needless_raw_string_hashes = "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
4 changes: 2 additions & 2 deletions serde_with/src/chrono_0_4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl<'de> DeserializeAs<'de, NaiveDateTime> for DateTime<Utc> {
}
}

/// Convert a [`chrono_0_4::Duration`] into a [`DurationSigned`]
/// Convert a [`Duration`] into a [`DurationSigned`]
fn duration_into_duration_signed(dur: &Duration) -> DurationSigned {
match dur.to_std() {
Ok(dur) => DurationSigned::with_duration(Sign::Positive, dur),
Expand All @@ -205,7 +205,7 @@ fn duration_into_duration_signed(dur: &Duration) -> DurationSigned {
}
}

/// Convert a [`DurationSigned`] into a [`chrono_0_4::Duration`]
/// Convert a [`DurationSigned`] into a [`Duration`]
fn duration_from_duration_signed<'de, D>(dur: DurationSigned) -> Result<Duration, D::Error>
where
D: Deserializer<'de>,
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
4 changes: 2 additions & 2 deletions serde_with/src/enum_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ where
self.deserialize_seq(visitor)
}

serde::forward_to_deserialize_any! {
forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf option unit unit_struct newtype_struct tuple
tuple_struct map struct enum identifier ignored_any
Expand Down Expand Up @@ -819,7 +819,7 @@ where
visitor.visit_enum(self)
}

serde::forward_to_deserialize_any! {
forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct identifier ignored_any
Expand Down
2 changes: 1 addition & 1 deletion serde_with/src/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The crate offers four types of functionality.
## 1. A more flexible and composable replacement for the with annotation, called `serde_as`

This is an alternative to [serde's with-annotation][with-annotation], which adds flexibility and composability to the scheme.
The main downside is that it work with fewer types than [with-annotations][with-annotation].
The main downside is that it works with fewer types than [with-annotations][with-annotation].
However, all types from the Rust Standard Library should be supported in all combinations and any missing entry is a bug.

You mirror the type structure of the field you want to de/serialize.
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
39 changes: 20 additions & 19 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 Expand Up @@ -543,7 +544,7 @@ fn test_parse_float_into_time_parts() {
parse_float_into_time_parts("-123.000987")
);
assert_eq!(
Ok((Sign::Positive, 18446744073709551615, 123_456_789)),
Ok((Sign::Positive, 18_446_744_073_709_551_615, 123_456_789)),
parse_float_into_time_parts("18446744073709551615.123456789")
);

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
Loading
Loading