Skip to content

Commit

Permalink
Enforce (some) code style with clippy in the fuzzer
Browse files Browse the repository at this point in the history
  • Loading branch information
juntyr committed Oct 3, 2023
1 parent 9fd5255 commit cf1bbdb
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 23 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
- run: cargo clippy --features integer128 -- -D warnings
- run: cargo clippy --features indexmap -- -D warnings
- run: cargo clippy --all-features -- -D warnings
- run: cd fuzz && cargo clippy --all -- -D warnings

rustfmt:
name: "Format: stable"
Expand All @@ -69,7 +70,8 @@ jobs:
profile: minimal
components: rustfmt
override: true
- run: cargo fmt -- --check
- run: cargo fmt --all -- --check
- run: cd fuzz && cargo fmt --all -- --check

coverage:
name: "Coverage: stable"
Expand Down
16 changes: 16 additions & 0 deletions fuzz/fuzz_targets/arbitrary.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
#![deny(clippy::correctness)]
#![deny(clippy::suspicious)]
#![deny(clippy::complexity)]
#![deny(clippy::perf)]
#![deny(clippy::style)]
#![warn(clippy::pedantic)]
#![deny(clippy::unwrap_used)]
#![deny(clippy::expect_used)]
#![allow(clippy::panic)]
#![deny(clippy::todo)]
#![deny(clippy::unimplemented)]
#![deny(clippy::unreachable)]
#![allow(unsafe_code)]
#![allow(clippy::match_same_arms)]
#![allow(clippy::too_many_lines)]
#![allow(clippy::items_after_statements)]
#![no_main]

use libfuzzer_sys::fuzz_target;
Expand Down
49 changes: 27 additions & 22 deletions fuzz/fuzz_targets/bench/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const LONG_NAME_COST_THRESHOLD: usize = 8_usize;

const ARRAY_UNINIT_LEN: usize = usize::MAX;

const FLATTEN_CONFLICT_MSG: &'static str = "ron::fuzz::FlattenFieldConflict";
const FLATTEN_CONFLICT_MSG: &str = "ron::fuzz::FlattenFieldConflict";

pub fn roundtrip_arbitrary_typed_ron_or_panic(data: &[u8]) -> Option<TypedSerdeData> {
if let Ok(typed_value) = TypedSerdeData::arbitrary(&mut Unstructured::new(data)) {
Expand Down Expand Up @@ -104,6 +104,7 @@ pub fn roundtrip_arbitrary_typed_ron_or_panic(data: &[u8]) -> Option<TypedSerdeD
// NOTE: Keep synchronised with ron::value::raw::RAW_VALUE_TOKEN
const RAW_VALUE_TOKEN: &str = "$ron::private::RawValue";

#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, PartialEq, Arbitrary)]
struct ArbitraryPrettyConfig {
/// Limit the pretty-ness up to the given depth.
Expand Down Expand Up @@ -341,6 +342,7 @@ impl<'a> Serialize for BorrowedTypedSerdeData<'a> {
&mut self,
key: &T,
) -> Result<(), Self::Error> {
#[allow(clippy::unwrap_used)] // FIXME
let key_str = ron::to_string(key).unwrap();
if self.keys.contains(&key_str) {
return Err(serde::ser::Error::custom(FLATTEN_CONFLICT_MSG));
Expand All @@ -366,6 +368,7 @@ impl<'a> Serialize for BorrowedTypedSerdeData<'a> {
key: &K,
value: &V,
) -> Result<(), Self::Error> {
#[allow(clippy::unwrap_used)] // FIXME
let key_str = ron::to_string(key).unwrap();
if self.keys.contains(&key_str) {
return Err(serde::ser::Error::custom(FLATTEN_CONFLICT_MSG));
Expand Down Expand Up @@ -396,6 +399,7 @@ impl<'a> Serialize for BorrowedTypedSerdeData<'a> {
},
))?;
} else {
#[allow(clippy::unwrap_used)] // FIXME
let field_str = ron::to_string(field).unwrap();
if flattened_keys.contains(&field_str) {
return Err(serde::ser::Error::custom(FLATTEN_CONFLICT_MSG));
Expand Down Expand Up @@ -689,6 +693,7 @@ impl<'a> Serialize for BorrowedTypedSerdeData<'a> {
&mut self,
key: &T,
) -> Result<(), Self::Error> {
#[allow(clippy::unwrap_used)] // FIXME
let key_str = ron::to_string(key).unwrap();
if self.keys.contains(&key_str) {
return Err(serde::ser::Error::custom(
Expand Down Expand Up @@ -716,6 +721,7 @@ impl<'a> Serialize for BorrowedTypedSerdeData<'a> {
key: &K,
value: &V,
) -> Result<(), Self::Error> {
#[allow(clippy::unwrap_used)] // FIXME
let key_str = ron::to_string(key).unwrap();
if self.keys.contains(&key_str) {
return Err(serde::ser::Error::custom(
Expand Down Expand Up @@ -761,6 +767,7 @@ impl<'a> Serialize for BorrowedTypedSerdeData<'a> {
),
)?;
} else {
#[allow(clippy::unwrap_used)] // FIXME
let field_str = ron::to_string(field).unwrap();
if flattened_keys.contains(&field_str) {
return Err(serde::ser::Error::custom(
Expand Down Expand Up @@ -810,6 +817,7 @@ impl<'a> Serialize for BorrowedTypedSerdeData<'a> {
),
)?;
} else {
#[allow(clippy::unwrap_used)] // FIXME
let field_str = ron::to_string(field).unwrap();
if flattened_keys.contains(&field_str) {
return Err(serde::ser::Error::custom(
Expand Down Expand Up @@ -867,6 +875,7 @@ impl<'a> Serialize for BorrowedTypedSerdeData<'a> {
),
)?;
} else {
#[allow(clippy::unwrap_used)] // FIXME
let field_str = ron::to_string(field).unwrap();
if flattened_keys.contains(&field_str) {
return Err(serde::ser::Error::custom(
Expand Down Expand Up @@ -976,8 +985,7 @@ impl<'a, 'de> DeserializeSeed<'de> for BorrowedTypedSerdeData<'a> {
Ok(())
} else {
Err(serde::de::Error::custom(format!(
"expected {:?} found {:?}",
check, value
"expected {check:?} found {value:?}"
)))
}
}
Expand All @@ -1004,24 +1012,22 @@ impl<'a, 'de> DeserializeSeed<'de> for BorrowedTypedSerdeData<'a> {
(SerdeDataType::F32, SerdeDataValue::F32(v)) => {
let value = f32::deserialize(deserializer)?;

if (v.is_nan() && value.is_nan()) || (value == *v) {
if (v.is_nan() && value.is_nan()) || (value.to_bits() == v.to_bits()) {
Ok(())
} else {
Err(serde::de::Error::custom(format!(
"expected {:?} found {:?}",
v, value
"expected {v:?} found {value:?}"
)))
}
}
(SerdeDataType::F64, SerdeDataValue::F64(v)) => {
let value = f64::deserialize(deserializer)?;

if (v.is_nan() && value.is_nan()) || (value == *v) {
if (v.is_nan() && value.is_nan()) || (value.to_bits() == v.to_bits()) {
Ok(())
} else {
Err(serde::de::Error::custom(format!(
"expected {:?} found {:?}",
v, value
"expected {v:?} found {value:?}"
)))
}
}
Expand Down Expand Up @@ -3196,11 +3202,11 @@ impl<'a, 'de> DeserializeSeed<'de> for BorrowedTypedSerdeData<'a> {
Ok(())
}
DelayedVariantIdentifier::Index(v) => Err(serde::de::Error::custom(
format!("expected variant index {} found {}", variant_index, v),
format!("expected variant index {variant_index} found {v}"),
)),
DelayedVariantIdentifier::Str(ref v) if v == variant => Ok(()),
DelayedVariantIdentifier::Str(ref v) => Err(serde::de::Error::custom(
format!("expected variant identifier {} found {}", variant, v),
format!("expected variant identifier {variant} found {v}"),
)),
DelayedVariantIdentifier::Bytes(ref v) if v == variant.as_bytes() => {
Ok(())
Expand Down Expand Up @@ -4047,6 +4053,7 @@ impl<'a, 'de> DeserializeSeed<'de> for BorrowedTypedSerdeData<'a> {
}));
}

#[allow(clippy::redundant_else)]
if self.flatten.iter().any(|x| *x) {
let Some(()) =
seq.next_element_seed(FlattenStructVariantSeed {
Expand Down Expand Up @@ -4179,7 +4186,7 @@ impl<'a, 'de> DeserializeSeed<'de> for BorrowedTypedSerdeData<'a> {

let expecting = match (ty, value) {
(SerdeDataVariantType::Unit, SerdeDataVariantValue::Unit) => {
format!("unit variant {}::{}", name, variant)
format!("unit variant {name}::{variant}")
}
(
SerdeDataVariantType::TaggedOther,
Expand All @@ -4188,19 +4195,19 @@ impl<'a, 'de> DeserializeSeed<'de> for BorrowedTypedSerdeData<'a> {
index: _,
},
) => {
format!("unit variant {}::{}", name, variant)
format!("unit variant {name}::{variant}")
}
(
SerdeDataVariantType::Newtype { .. },
SerdeDataVariantValue::Newtype { .. },
) => format!("newtype variant {}::{}", name, variant),
) => format!("newtype variant {name}::{variant}"),
(SerdeDataVariantType::Tuple { .. }, SerdeDataVariantValue::Struct { .. }) => {
return Err(serde::de::Error::custom(
"invalid serde internally tagged tuple variant",
))
}
(SerdeDataVariantType::Struct { .. }, SerdeDataVariantValue::Struct { .. }) => {
format!("struct variant {}::{}", name, variant)
format!("struct variant {name}::{variant}")
}
_ => return Err(serde::de::Error::custom("invalid serde enum data")),
};
Expand All @@ -4222,11 +4229,11 @@ impl<'a, 'de> DeserializeSeed<'de> for BorrowedTypedSerdeData<'a> {
Ok(())
}
DelayedVariantIdentifier::Index(v) => Err(serde::de::Error::custom(
format!("expected variant index {} found {}", variant_index, v),
format!("expected variant index {variant_index} found {v}"),
)),
DelayedVariantIdentifier::Str(ref v) if v == variant => Ok(()),
DelayedVariantIdentifier::Str(ref v) => Err(serde::de::Error::custom(
format!("expected variant identifier {} found {}", variant, v),
format!("expected variant identifier {variant} found {v}"),
)),
DelayedVariantIdentifier::Bytes(ref v) if v == variant.as_bytes() => {
Ok(())
Expand Down Expand Up @@ -5167,7 +5174,7 @@ impl<'a> SerdeDataType<'a> {
{
return Err(arbitrary::Error::IncorrectFormat);
}
if matches!(representation, SerdeEnumRepresentation::InternallyTagged { tag: _ } if !inner.supported_inside_internally_tagged_newtype(&value, pretty, false))
if matches!(representation, SerdeEnumRepresentation::InternallyTagged { tag: _ } if !inner.supported_inside_internally_tagged_newtype(&value, false))
{
return Err(arbitrary::Error::IncorrectFormat);
}
Expand Down Expand Up @@ -5201,7 +5208,7 @@ impl<'a> SerdeDataType<'a> {
}

let mut tuple = Vec::with_capacity(fields.len());
for ty in fields.iter_mut() {
for ty in &mut *fields {
tuple.push(ty.arbitrary_value(u, pretty)?);
}
let value = SerdeDataVariantValue::Struct { fields: tuple };
Expand Down Expand Up @@ -5446,7 +5453,6 @@ impl<'a> SerdeDataType<'a> {
fn supported_inside_internally_tagged_newtype(
&self,
value: &SerdeDataValue,
pretty: &PrettyConfig,
inside_untagged_newtype_variant: bool,
) -> bool {
// See https://github.com/serde-rs/serde/blob/ddc1ee564b33aa584e5a66817aafb27c3265b212/serde/src/private/ser.rs#L94-L336
Expand Down Expand Up @@ -5490,7 +5496,6 @@ impl<'a> SerdeDataType<'a> {
if let SerdeDataValue::Newtype { inner: value } = value {
ty.supported_inside_internally_tagged_newtype(
value,
pretty,
inside_untagged_newtype_variant,
)
} else {
Expand Down Expand Up @@ -5536,7 +5541,7 @@ impl<'a> SerdeDataType<'a> {
SerdeDataVariantValue::Newtype { inner: value },
) => {
if matches!(representation, SerdeEnumRepresentation::Untagged) {
ty.supported_inside_internally_tagged_newtype(value, pretty, true)
ty.supported_inside_internally_tagged_newtype(value, true)
} else {
true
}
Expand Down
17 changes: 17 additions & 0 deletions fuzz/fuzz_targets/bench/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
#![deny(clippy::correctness)]
#![deny(clippy::suspicious)]
#![deny(clippy::complexity)]
#![deny(clippy::perf)]
#![deny(clippy::style)]
#![warn(clippy::pedantic)]
#![deny(clippy::unwrap_used)]
#![deny(clippy::expect_used)]
#![allow(clippy::panic)]
#![deny(clippy::todo)]
#![deny(clippy::unimplemented)]
#![deny(clippy::unreachable)]
#![allow(unsafe_code)]
#![allow(clippy::match_same_arms)]
#![allow(clippy::too_many_lines)]
#![allow(clippy::items_after_statements)]

use std::{
collections::{hash_map::DefaultHasher, HashMap, VecDeque},
fs,
Expand Down
13 changes: 13 additions & 0 deletions fuzz/fuzz_targets/from_str.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
#![deny(clippy::correctness)]
#![deny(clippy::suspicious)]
#![deny(clippy::complexity)]
#![deny(clippy::perf)]
#![deny(clippy::style)]
#![warn(clippy::pedantic)]
#![deny(clippy::unwrap_used)]
#![deny(clippy::expect_used)]
#![deny(clippy::panic)]
#![deny(clippy::todo)]
#![deny(clippy::unimplemented)]
#![deny(clippy::unreachable)]
#![deny(unsafe_code)]
#![no_main]

use libfuzzer_sys::fuzz_target;
Expand Down

0 comments on commit cf1bbdb

Please sign in to comment.