From 85626ab3a307386dbc20afd2bf502ba2a806688c Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Mon, 19 Aug 2024 10:42:05 +0100 Subject: [PATCH] Enable (and fix) more clippy lints --- clippy.toml | 1 + schemars/Cargo.toml | 2 +- schemars/src/_private.rs | 3 +++ schemars/src/gen.rs | 9 +++++---- schemars/src/json_schema_impls/core.rs | 2 +- schemars/src/lib.rs | 10 +++++++++- schemars/src/schema.rs | 1 + schemars/src/ser.rs | 4 ++-- schemars/src/transform.rs | 14 +++++++------- schemars_derive/Cargo.toml | 1 + 10 files changed, 31 insertions(+), 16 deletions(-) create mode 100644 clippy.toml diff --git a/clippy.toml b/clippy.toml new file mode 100644 index 0000000..8a4e1a8 --- /dev/null +++ b/clippy.toml @@ -0,0 +1 @@ +doc-valid-idents = ["OpenAPI", ".."] diff --git a/schemars/Cargo.toml b/schemars/Cargo.toml index e94d46f..9579476 100644 --- a/schemars/Cargo.toml +++ b/schemars/Cargo.toml @@ -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] diff --git a/schemars/src/_private.rs b/schemars/src/_private.rs index dc867d9..43a46db 100644 --- a/schemars/src/_private.rs +++ b/schemars/src/_private.rs @@ -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": { diff --git a/schemars/src/gen.rs b/schemars/src/gen.rs index 505baa6..98dd7b3 100644 --- a/schemars/src/gen.rs +++ b/schemars/src/gen.rs @@ -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 { - 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> { + 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`] diff --git a/schemars/src/json_schema_impls/core.rs b/schemars/src/json_schema_impls/core.rs index 3e0d994..4e2c9d6 100644 --- a/schemars/src/json_schema_impls/core.rs +++ b/schemars/src/json_schema_impls/core.rs @@ -36,7 +36,7 @@ impl JsonSchema for Option { *instance_type.unwrap() = Value::Array(vec![ core::mem::take(string).into(), "null".into(), - ]) + ]); } obj.into() } diff --git a/schemars/src/lib.rs b/schemars/src/lib.rs index 186a53f..a556182 100644 --- a/schemars/src/lib.rs +++ b/schemars/src/lib.rs @@ -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] diff --git a/schemars/src/schema.rs b/schemars/src/schema.rs index c3882d0..5b7dcd8 100644 --- a/schemars/src/schema.rs +++ b/schemars/src/schema.rs @@ -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 { if let Some(b) = self.as_bool() { let mut map = Map::new(); diff --git a/schemars/src/ser.rs b/schemars/src/ser.rs index c0bcc21..0aefc6b 100644 --- a/schemars/src/ser.rs +++ b/schemars/src/ser.rs @@ -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()); } } } diff --git a/schemars/src/transform.rs b/schemars/src/transform.rs index 5529e06..98d2add 100644 --- a/schemars/src/transform.rs +++ b/schemars/src/transform.rs @@ -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: &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: &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); } } } diff --git a/schemars_derive/Cargo.toml b/schemars_derive/Cargo.toml index 48400d6..4f20d8c 100644 --- a/schemars_derive/Cargo.toml +++ b/schemars_derive/Cargo.toml @@ -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]