Enable (and fix) more clippy lints

This commit is contained in:
Graham Esau 2024-08-19 10:42:05 +01:00
parent 46947d77c7
commit 85626ab3a3
10 changed files with 31 additions and 16 deletions

1
clippy.toml Normal file
View file

@ -0,0 +1 @@
doc-valid-idents = ["OpenAPI", ".."]

View file

@ -9,7 +9,7 @@ edition = "2021"
license = "MIT"
readme = "README.md"
keywords = ["rust", "json-schema", "serde"]
categories = ["encoding"]
categories = ["encoding", "no-std"]
rust-version = "1.60"
[dependencies]

View file

@ -59,7 +59,10 @@ pub fn new_unit_enum_variant(variant: &str) -> Schema {
}
/// Create a schema for an externally tagged enum variant
#[allow(clippy::needless_pass_by_value)]
pub fn new_externally_tagged_enum_variant(variant: &str, sub_schema: Schema) -> Schema {
// TODO: this can be optimised by inserting the `sub_schema` as a `Value` rather than
// using the `json_schema!` macro which borrows and serializes the sub_schema
json_schema!({
"type": "object",
"properties": {

View file

@ -238,7 +238,7 @@ impl SchemaGenerator {
if self.used_schema_names.contains(base_name.as_ref()) {
for i in 2.. {
name = format!("{}{}", base_name, i).into();
name = format!("{base_name}{i}").into();
if !self.used_schema_names.contains(&name) {
break;
}
@ -300,7 +300,7 @@ impl SchemaGenerator {
/// Returns an iterator over the [transforms](SchemaSettings::transforms) being used by this `SchemaGenerator`.
pub fn transforms_mut(&mut self) -> impl Iterator<Item = &mut dyn GenTransform> {
self.settings.transforms.iter_mut().map(|v| v.as_mut())
self.settings.transforms.iter_mut().map(Box::as_mut)
}
/// Generates a JSON Schema for the type `T`.
@ -477,6 +477,8 @@ fn json_pointer_mut<'a>(
pointer: &str,
create_if_missing: bool,
) -> Option<&'a mut JsonMap<String, Value>> {
use serde_json::map::Entry;
let pointer = pointer.strip_prefix('/')?;
if pointer.is_empty() {
return Some(object);
@ -489,7 +491,6 @@ fn json_pointer_mut<'a>(
segment = &replaced;
}
use serde_json::map::Entry;
let next_value = match object.entry(segment) {
Entry::Occupied(o) => o.into_mut(),
Entry::Vacant(v) if create_if_missing => v.insert(Value::Object(JsonMap::default())),
@ -502,7 +503,7 @@ fn json_pointer_mut<'a>(
Some(object)
}
/// A [Transform] which implements additional traits required to be included in a [SchemaSettings].
/// A [`Transform`] which implements additional traits required to be included in a [`SchemaSettings`].
///
/// You will rarely need to use this trait directly as it is automatically implemented for any type which implements all of:
/// - [`Transform`]

View file

@ -36,7 +36,7 @@ impl<T: JsonSchema> JsonSchema for Option<T> {
*instance_type.unwrap() = Value::Array(vec![
core::mem::take(string).into(),
"null".into(),
])
]);
}
obj.into()
}

View file

@ -1,4 +1,12 @@
#![deny(unsafe_code)]
#![deny(unsafe_code, clippy::cargo, clippy::pedantic)]
#![allow(
clippy::must_use_candidate,
clippy::return_self_not_must_use,
clippy::wildcard_imports,
clippy::single_match_else,
clippy::missing_errors_doc,
clippy::module_name_repetitions
)]
#![doc = include_str!("../README.md")]
#![no_std]

View file

@ -128,6 +128,7 @@ impl Schema {
///
/// `true` is transformed into an empty schema `{}`, which successfully validates against all possible values.
/// `false` is transformed into the schema `{"not": {}}`, which does not successfully validate against any value.
#[allow(clippy::missing_panics_doc)]
pub fn ensure_object(&mut self) -> &mut Map<String, Value> {
if let Some(b) = self.as_bool() {
let mut map = Map::new();

View file

@ -146,7 +146,7 @@ impl<'a> serde::Serializer for Serializer<'a> {
*value.unwrap() = Value::Array(vec![
core::mem::take(string).into(),
"null".into(),
])
]);
}
obj.into()
}
@ -341,7 +341,7 @@ impl serde::ser::SerializeSeq for SerializeSeq<'_> {
None => self.items = Some(schema),
Some(items) => {
if items != &schema {
self.items = Some(true.into())
self.items = Some(true.into());
}
}
}

View file

@ -49,7 +49,7 @@ assert_eq!(
);
```
The same example with a `fn` transform`:
The same example with a `fn` transform:
```
# use schemars::{Schema, json_schema};
use schemars::transform::transform_subschemas;
@ -138,7 +138,7 @@ where
F: FnMut(&mut Schema),
{
fn transform(&mut self, schema: &mut Schema) {
self(schema)
self(schema);
}
}
@ -161,14 +161,14 @@ pub fn transform_subschemas<T: Transform + ?Sized>(t: &mut T, schema: &mut Schem
| "propertyNames"
| "additionalItems" => {
if let Ok(subschema) = value.try_into() {
t.transform(subschema)
t.transform(subschema);
}
}
"allOf" | "anyOf" | "oneOf" | "prefixItems" => {
if let Some(array) = value.as_array_mut() {
for value in array {
if let Ok(subschema) = value.try_into() {
t.transform(subschema)
t.transform(subschema);
}
}
}
@ -178,18 +178,18 @@ pub fn transform_subschemas<T: Transform + ?Sized>(t: &mut T, schema: &mut Schem
if let Some(array) = value.as_array_mut() {
for value in array {
if let Ok(subschema) = value.try_into() {
t.transform(subschema)
t.transform(subschema);
}
}
} else if let Ok(subschema) = value.try_into() {
t.transform(subschema)
t.transform(subschema);
}
}
"properties" | "patternProperties" | "$defs" | "definitions" => {
if let Some(obj) = value.as_object_mut() {
for value in obj.values_mut() {
if let Ok(subschema) = value.try_into() {
t.transform(subschema)
t.transform(subschema);
}
}
}

View file

@ -9,6 +9,7 @@ edition = "2021"
license = "MIT"
readme = "README.md"
keywords = ["rust", "json-schema", "serde"]
categories = ["encoding", "no-std"]
rust-version = "1.60"
[lib]