Enable (and fix) more clippy lints
This commit is contained in:
parent
46947d77c7
commit
85626ab3a3
10 changed files with 31 additions and 16 deletions
1
clippy.toml
Normal file
1
clippy.toml
Normal file
|
@ -0,0 +1 @@
|
||||||
|
doc-valid-idents = ["OpenAPI", ".."]
|
|
@ -9,7 +9,7 @@ edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
keywords = ["rust", "json-schema", "serde"]
|
keywords = ["rust", "json-schema", "serde"]
|
||||||
categories = ["encoding"]
|
categories = ["encoding", "no-std"]
|
||||||
rust-version = "1.60"
|
rust-version = "1.60"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -59,7 +59,10 @@ pub fn new_unit_enum_variant(variant: &str) -> Schema {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a schema for an externally tagged enum variant
|
/// 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 {
|
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!({
|
json_schema!({
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
|
|
|
@ -238,7 +238,7 @@ impl SchemaGenerator {
|
||||||
|
|
||||||
if self.used_schema_names.contains(base_name.as_ref()) {
|
if self.used_schema_names.contains(base_name.as_ref()) {
|
||||||
for i in 2.. {
|
for i in 2.. {
|
||||||
name = format!("{}{}", base_name, i).into();
|
name = format!("{base_name}{i}").into();
|
||||||
if !self.used_schema_names.contains(&name) {
|
if !self.used_schema_names.contains(&name) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -300,7 +300,7 @@ impl SchemaGenerator {
|
||||||
|
|
||||||
/// Returns an iterator over the [transforms](SchemaSettings::transforms) being used by this `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> {
|
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`.
|
/// Generates a JSON Schema for the type `T`.
|
||||||
|
@ -477,6 +477,8 @@ fn json_pointer_mut<'a>(
|
||||||
pointer: &str,
|
pointer: &str,
|
||||||
create_if_missing: bool,
|
create_if_missing: bool,
|
||||||
) -> Option<&'a mut JsonMap<String, Value>> {
|
) -> Option<&'a mut JsonMap<String, Value>> {
|
||||||
|
use serde_json::map::Entry;
|
||||||
|
|
||||||
let pointer = pointer.strip_prefix('/')?;
|
let pointer = pointer.strip_prefix('/')?;
|
||||||
if pointer.is_empty() {
|
if pointer.is_empty() {
|
||||||
return Some(object);
|
return Some(object);
|
||||||
|
@ -489,7 +491,6 @@ fn json_pointer_mut<'a>(
|
||||||
segment = &replaced;
|
segment = &replaced;
|
||||||
}
|
}
|
||||||
|
|
||||||
use serde_json::map::Entry;
|
|
||||||
let next_value = match object.entry(segment) {
|
let next_value = match object.entry(segment) {
|
||||||
Entry::Occupied(o) => o.into_mut(),
|
Entry::Occupied(o) => o.into_mut(),
|
||||||
Entry::Vacant(v) if create_if_missing => v.insert(Value::Object(JsonMap::default())),
|
Entry::Vacant(v) if create_if_missing => v.insert(Value::Object(JsonMap::default())),
|
||||||
|
@ -502,7 +503,7 @@ fn json_pointer_mut<'a>(
|
||||||
Some(object)
|
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:
|
/// You will rarely need to use this trait directly as it is automatically implemented for any type which implements all of:
|
||||||
/// - [`Transform`]
|
/// - [`Transform`]
|
||||||
|
|
|
@ -36,7 +36,7 @@ impl<T: JsonSchema> JsonSchema for Option<T> {
|
||||||
*instance_type.unwrap() = Value::Array(vec![
|
*instance_type.unwrap() = Value::Array(vec![
|
||||||
core::mem::take(string).into(),
|
core::mem::take(string).into(),
|
||||||
"null".into(),
|
"null".into(),
|
||||||
])
|
]);
|
||||||
}
|
}
|
||||||
obj.into()
|
obj.into()
|
||||||
}
|
}
|
||||||
|
|
|
@ -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")]
|
#![doc = include_str!("../README.md")]
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
|
|
|
@ -128,6 +128,7 @@ impl Schema {
|
||||||
///
|
///
|
||||||
/// `true` is transformed into an empty schema `{}`, which successfully validates against all possible values.
|
/// `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.
|
/// `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> {
|
pub fn ensure_object(&mut self) -> &mut Map<String, Value> {
|
||||||
if let Some(b) = self.as_bool() {
|
if let Some(b) = self.as_bool() {
|
||||||
let mut map = Map::new();
|
let mut map = Map::new();
|
||||||
|
|
|
@ -146,7 +146,7 @@ impl<'a> serde::Serializer for Serializer<'a> {
|
||||||
*value.unwrap() = Value::Array(vec![
|
*value.unwrap() = Value::Array(vec![
|
||||||
core::mem::take(string).into(),
|
core::mem::take(string).into(),
|
||||||
"null".into(),
|
"null".into(),
|
||||||
])
|
]);
|
||||||
}
|
}
|
||||||
obj.into()
|
obj.into()
|
||||||
}
|
}
|
||||||
|
@ -341,7 +341,7 @@ impl serde::ser::SerializeSeq for SerializeSeq<'_> {
|
||||||
None => self.items = Some(schema),
|
None => self.items = Some(schema),
|
||||||
Some(items) => {
|
Some(items) => {
|
||||||
if items != &schema {
|
if items != &schema {
|
||||||
self.items = Some(true.into())
|
self.items = Some(true.into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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::{Schema, json_schema};
|
||||||
use schemars::transform::transform_subschemas;
|
use schemars::transform::transform_subschemas;
|
||||||
|
@ -138,7 +138,7 @@ where
|
||||||
F: FnMut(&mut Schema),
|
F: FnMut(&mut Schema),
|
||||||
{
|
{
|
||||||
fn transform(&mut self, schema: &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"
|
| "propertyNames"
|
||||||
| "additionalItems" => {
|
| "additionalItems" => {
|
||||||
if let Ok(subschema) = value.try_into() {
|
if let Ok(subschema) = value.try_into() {
|
||||||
t.transform(subschema)
|
t.transform(subschema);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"allOf" | "anyOf" | "oneOf" | "prefixItems" => {
|
"allOf" | "anyOf" | "oneOf" | "prefixItems" => {
|
||||||
if let Some(array) = value.as_array_mut() {
|
if let Some(array) = value.as_array_mut() {
|
||||||
for value in array {
|
for value in array {
|
||||||
if let Ok(subschema) = value.try_into() {
|
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() {
|
if let Some(array) = value.as_array_mut() {
|
||||||
for value in array {
|
for value in array {
|
||||||
if let Ok(subschema) = value.try_into() {
|
if let Ok(subschema) = value.try_into() {
|
||||||
t.transform(subschema)
|
t.transform(subschema);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if let Ok(subschema) = value.try_into() {
|
} else if let Ok(subschema) = value.try_into() {
|
||||||
t.transform(subschema)
|
t.transform(subschema);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"properties" | "patternProperties" | "$defs" | "definitions" => {
|
"properties" | "patternProperties" | "$defs" | "definitions" => {
|
||||||
if let Some(obj) = value.as_object_mut() {
|
if let Some(obj) = value.as_object_mut() {
|
||||||
for value in obj.values_mut() {
|
for value in obj.values_mut() {
|
||||||
if let Ok(subschema) = value.try_into() {
|
if let Ok(subschema) = value.try_into() {
|
||||||
t.transform(subschema)
|
t.transform(subschema);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
keywords = ["rust", "json-schema", "serde"]
|
keywords = ["rust", "json-schema", "serde"]
|
||||||
|
categories = ["encoding", "no-std"]
|
||||||
rust-version = "1.60"
|
rust-version = "1.60"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue