Skip to content

Commit fa505f1

Browse files
mbrobbelahl
andauthored
chore: fix clippy warnings (#841)
Co-authored-by: Adam Leventhal <adam.leventhal@gmail.com>
1 parent 49b46fb commit fa505f1

11 files changed

Lines changed: 83 additions & 110 deletions

File tree

typify-impl/src/convert.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ impl TypeSpace {
6666
enum_values,
6767
..
6868
} if multiple.len() == 2 && multiple.contains(&InstanceType::Null) => {
69-
let only_null = enum_values.as_ref().map_or(false, |values| {
70-
values.iter().all(serde_json::Value::is_null)
71-
});
69+
let only_null = enum_values
70+
.as_ref()
71+
.is_some_and(|values| values.iter().all(serde_json::Value::is_null));
7272

7373
if only_null {
7474
// If there are enumerated values and they're all null,

typify-impl/src/defaults.rs

Lines changed: 50 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -151,24 +151,24 @@ impl TypeEntry {
151151
}) => match tag_type {
152152
EnumTagType::External => {
153153
validate_default_for_external_enum(type_space, variants, default)
154-
.ok_or_else(|| Error::invalid_value())
154+
.ok_or_else(Error::invalid_value)
155155
}
156156
EnumTagType::Internal { tag } => {
157157
validate_default_for_internal_enum(type_space, variants, default, tag)
158-
.ok_or_else(|| Error::invalid_value())
158+
.ok_or_else(Error::invalid_value)
159159
}
160160
EnumTagType::Adjacent { tag, content } => {
161161
validate_default_for_adjacent_enum(type_space, variants, default, tag, content)
162-
.ok_or_else(|| Error::invalid_value())
162+
.ok_or_else(Error::invalid_value)
163163
}
164164
EnumTagType::Untagged => {
165165
validate_default_for_untagged_enum(type_space, variants, default)
166-
.ok_or_else(|| Error::invalid_value())
166+
.ok_or_else(Error::invalid_value)
167167
}
168168
},
169169
TypeEntryDetails::Struct(TypeEntryStruct { properties, .. }) => {
170170
validate_default_struct_props(properties, type_space, default)
171-
.ok_or_else(|| Error::invalid_value())
171+
.ok_or_else(Error::invalid_value)
172172
}
173173

174174
TypeEntryDetails::Newtype(TypeEntryNewtype { type_id, .. }) => {
@@ -242,8 +242,9 @@ impl TypeEntry {
242242
Err(Error::invalid_value())
243243
}
244244
}
245-
TypeEntryDetails::Tuple(ids) => validate_default_tuple(ids, type_space, default)
246-
.ok_or_else(|| Error::invalid_value()),
245+
TypeEntryDetails::Tuple(ids) => {
246+
validate_default_tuple(ids, type_space, default).ok_or_else(Error::invalid_value)
247+
}
247248

248249
TypeEntryDetails::Array(type_id, length) => {
249250
let Some(arr) = default.as_array() else {
@@ -648,10 +649,9 @@ mod tests {
648649
let (type_space, type_id) = get_type::<Option<u32>>();
649650
let type_entry = type_space.id_to_entry.get(&type_id).unwrap();
650651

651-
assert!(matches!(
652-
type_entry.validate_value(&type_space, &json!("forty-two")),
653-
Err(_)
654-
));
652+
assert!(type_entry
653+
.validate_value(&type_space, &json!("forty-two"))
654+
.is_err());
655655
assert!(matches!(
656656
type_entry.validate_value(&type_space, &json!(null)),
657657
Ok(DefaultKind::Intrinsic)
@@ -671,10 +671,9 @@ mod tests {
671671
extra_derives: Default::default(),
672672
};
673673

674-
assert!(matches!(
675-
type_entry.validate_value(&type_space, &json!("forty-two")),
676-
Err(_)
677-
));
674+
assert!(type_entry
675+
.validate_value(&type_space, &json!("forty-two"))
676+
.is_err());
678677
assert!(matches!(
679678
type_entry.validate_value(&type_space, &json!(null)),
680679
Ok(DefaultKind::Intrinsic)
@@ -690,10 +689,9 @@ mod tests {
690689
let (type_space, type_id) = get_type::<Vec<u32>>();
691690
let type_entry = type_space.id_to_entry.get(&type_id).unwrap();
692691

693-
assert!(matches!(
694-
type_entry.validate_value(&type_space, &json!([null])),
695-
Err(_),
696-
));
692+
assert!(type_entry
693+
.validate_value(&type_space, &json!([null]))
694+
.is_err());
697695
assert!(matches!(
698696
type_entry.validate_value(&type_space, &json!([])),
699697
Ok(DefaultKind::Intrinsic),
@@ -709,10 +707,7 @@ mod tests {
709707
let (type_space, type_id) = get_type::<HashMap<String, u32>>();
710708
let type_entry = type_space.id_to_entry.get(&type_id).unwrap();
711709

712-
assert!(matches!(
713-
type_entry.validate_value(&type_space, &json!([])),
714-
Err(_),
715-
));
710+
assert!(type_entry.validate_value(&type_space, &json!([])).is_err());
716711
assert!(matches!(
717712
type_entry.validate_value(&type_space, &json!({})),
718713
Ok(DefaultKind::Intrinsic),
@@ -728,10 +723,9 @@ mod tests {
728723
let (type_space, type_id) = get_type::<(u32, u32, String)>();
729724
let type_entry = type_space.id_to_entry.get(&type_id).unwrap();
730725

731-
assert!(matches!(
732-
type_entry.validate_value(&type_space, &json!([1, 2, "three", 4])),
733-
Err(_),
734-
));
726+
assert!(type_entry
727+
.validate_value(&type_space, &json!([1, 2, "three", 4]))
728+
.is_err());
735729
assert!(matches!(
736730
type_entry.validate_value(&type_space, &json!([1, 2, "three"])),
737731
Ok(DefaultKind::Specific),
@@ -769,10 +763,9 @@ mod tests {
769763
let (type_space, type_id) = get_type::<u32>();
770764
let type_entry = type_space.id_to_entry.get(&type_id).unwrap();
771765

772-
assert!(matches!(
773-
type_entry.validate_value(&type_space, &json!(true)),
774-
Err(_),
775-
));
766+
assert!(type_entry
767+
.validate_value(&type_space, &json!(true))
768+
.is_err());
776769
assert!(matches!(
777770
type_entry.validate_value(&type_space, &json!(0)),
778771
Ok(DefaultKind::Intrinsic),
@@ -822,8 +815,8 @@ mod tests {
822815
),
823816
Ok(DefaultKind::Specific),
824817
));
825-
assert!(matches!(
826-
type_entry.validate_value(
818+
assert!(type_entry
819+
.validate_value(
827820
&type_space,
828821
&json!(
829822
{
@@ -832,11 +825,10 @@ mod tests {
832825
"d": 7
833826
}
834827
)
835-
),
836-
Err(_),
837-
));
838-
assert!(matches!(
839-
type_entry.validate_value(
828+
)
829+
.is_err());
830+
assert!(type_entry
831+
.validate_value(
840832
&type_space,
841833
&json!(
842834
{
@@ -845,9 +837,8 @@ mod tests {
845837
"d": {}
846838
}
847839
)
848-
),
849-
Err(_),
850-
));
840+
)
841+
.is_err());
851842
}
852843

853844
#[test]
@@ -885,14 +876,10 @@ mod tests {
885876
),
886877
Ok(DefaultKind::Specific),
887878
));
888-
assert!(matches!(
889-
type_entry.validate_value(&type_space, &json!({ "A": null })),
890-
Err(_),
891-
));
892-
assert!(matches!(
893-
type_entry.validate_value(&type_space, &json!("B")),
894-
Err(_),
895-
));
879+
assert!(type_entry
880+
.validate_value(&type_space, &json!({ "A": null }))
881+
.is_err());
882+
assert!(type_entry.validate_value(&type_space, &json!("B")).is_err());
896883
}
897884

898885
#[test]
@@ -928,25 +915,23 @@ mod tests {
928915
),
929916
Ok(DefaultKind::Specific),
930917
));
931-
assert!(matches!(
932-
type_entry.validate_value(
918+
assert!(type_entry
919+
.validate_value(
933920
&type_space,
934921
&json!({
935922
"not-tag": "A"
936923
})
937-
),
938-
Err(_),
939-
));
940-
assert!(matches!(
941-
type_entry.validate_value(
924+
)
925+
.is_err());
926+
assert!(type_entry
927+
.validate_value(
942928
&type_space,
943929
&json!({
944930
"tag": "B",
945931
"cc": "where's D?"
946932
})
947-
),
948-
Err(_),
949-
));
933+
)
934+
.is_err());
950935
}
951936

952937
#[test]
@@ -992,20 +977,16 @@ mod tests {
992977
),
993978
Ok(DefaultKind::Specific),
994979
));
995-
assert!(matches!(
996-
type_entry.validate_value(&type_space, &json!("A")),
997-
Err(_),
998-
));
999-
assert!(matches!(
1000-
type_entry.validate_value(
980+
assert!(type_entry.validate_value(&type_space, &json!("A")).is_err());
981+
assert!(type_entry
982+
.validate_value(
1001983
&type_space,
1002984
&json!({
1003985
"tag": "A",
1004986
"content": null,
1005987
})
1006-
),
1007-
Err(_),
1008-
));
988+
)
989+
.is_err());
1009990
}
1010991
#[test]
1011992
fn test_enum_untagged() {
@@ -1033,9 +1014,6 @@ mod tests {
10331014
type_entry.validate_value(&type_space, &json!( { "cc": "xx", "dd": "yy" })),
10341015
Ok(DefaultKind::Specific),
10351016
));
1036-
assert!(matches!(
1037-
type_entry.validate_value(&type_space, &json!({})),
1038-
Err(_),
1039-
));
1017+
assert!(type_entry.validate_value(&type_space, &json!({})).is_err());
10401018
}
10411019
}

typify-impl/src/enums.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ mod tests {
10501050
assert_eq!(variants.len(), 5);
10511051

10521052
assert!(matches!(
1053-
variants.get(0).unwrap(),
1053+
variants.first().unwrap(),
10541054
Variant {
10551055
details: VariantDetails::Simple,
10561056
..

typify-impl/src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,7 @@ impl Error {
6363
pub type Result<T> = std::result::Result<T, Error>;
6464

6565
fn show_type_name(type_name: Option<&str>) -> &str {
66-
if let Some(type_name) = type_name {
67-
type_name
68-
} else {
69-
"<unknown type>"
70-
}
66+
type_name.unwrap_or("<unknown type>")
7167
}
7268

7369
/// Representation of a type which may have a definition or may be built-in.
@@ -987,7 +983,7 @@ impl ToTokens for TypeSpace {
987983
}
988984
}
989985

990-
impl<'a> Type<'a> {
986+
impl Type<'_> {
991987
/// The name of the type as a String.
992988
pub fn name(&self) -> String {
993989
let Type {
@@ -1167,7 +1163,7 @@ impl<'a> TypeStruct<'a> {
11671163
}
11681164
}
11691165

1170-
impl<'a> TypeNewtype<'a> {
1166+
impl TypeNewtype<'_> {
11711167
/// Get the inner type of the newtype struct.
11721168
pub fn inner(&self) -> TypeId {
11731169
self.details.type_id.clone()

typify-impl/src/merge.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ fn merge_additional_properties(
6767
) -> Option<Schema> {
6868
match (a, b) {
6969
(None, other) | (other, None) => other.cloned(),
70-
(Some(aa), Some(bb)) => {
71-
Some(try_merge_schema(aa, bb, defs).unwrap_or_else(|_| Schema::Bool(false)))
72-
}
70+
(Some(aa), Some(bb)) => Some(try_merge_schema(aa, bb, defs).unwrap_or(Schema::Bool(false))),
7371
}
7472
}
7573

@@ -893,12 +891,12 @@ fn merge_so_array(
893891
let aa_items_iter = aa_items.iter().chain(repeat(
894892
aa_additional_items
895893
.as_deref()
896-
.unwrap_or_else(|| &Schema::Bool(true)),
894+
.unwrap_or(&Schema::Bool(true)),
897895
));
898896
let bb_items_iter = bb_items.iter().chain(repeat(
899897
bb_additional_items
900898
.as_deref()
901-
.unwrap_or_else(|| &Schema::Bool(true)),
899+
.unwrap_or(&Schema::Bool(true)),
902900
));
903901
let items_iter = aa_items_iter.zip(bb_items_iter).take(items_len);
904902
let (items, allow_additional_items) =

typify-impl/src/output.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl OutputSpace {
2727
) {
2828
self.items
2929
.entry((location, order_hint.to_string()))
30-
.or_insert_with(TokenStream::new)
30+
.or_default()
3131
.extend(stream);
3232
}
3333

@@ -36,12 +36,13 @@ impl OutputSpace {
3636
.items
3737
.into_iter()
3838
.map(|((location, _), item)| (location, item))
39-
.fold(BTreeMap::new(), |mut map, (location, item)| {
40-
map.entry(location)
41-
.or_insert_with(TokenStream::new)
42-
.extend(item);
43-
map
44-
});
39+
.fold(
40+
BTreeMap::<_, TokenStream>::new(),
41+
|mut map, (location, item)| {
42+
map.entry(location).or_default().extend(item);
43+
map
44+
},
45+
);
4546

4647
let mod_streams = mods.into_iter().map(|(location, items)| match location {
4748
OutputSpaceMod::Crate => quote! {

typify-impl/src/type_entry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ impl TypeEntry {
15361536
static PATTERN: ::std::sync::LazyLock<::regress::Regex> = ::std::sync::LazyLock::new(|| {
15371537
::regress::Regex::new(#p).unwrap()
15381538
});
1539-
if (&*PATTERN).find(value).is_none() {
1539+
if PATTERN.find(value).is_none() {
15401540
return Err(#err.into());
15411541
}
15421542
}

0 commit comments

Comments
 (0)