From 824993ca76d1233576bb9d4677a9c8bce24626fd Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Sun, 26 Feb 2023 19:59:17 +0000 Subject: [PATCH] Apply clippy fixes --- schemars/src/gen.rs | 2 +- schemars/src/ser.rs | 6 ++-- schemars/tests/examples.rs | 2 +- schemars/tests/validate.rs | 2 +- schemars_derive/src/attr/schemars_to_serde.rs | 14 +++++----- schemars_derive/src/attr/validation.rs | 28 +++++++++---------- 6 files changed, 26 insertions(+), 28 deletions(-) diff --git a/schemars/src/gen.rs b/schemars/src/gen.rs index 8ae1740..58eaa61 100644 --- a/schemars/src/gen.rs +++ b/schemars/src/gen.rs @@ -263,7 +263,7 @@ impl SchemaGenerator { /// The keys of the returned `Map` are the [schema names](JsonSchema::schema_name), and the values are the schemas /// themselves. pub fn take_definitions(&mut self) -> Map { - std::mem::replace(&mut self.definitions, Map::default()) + std::mem::take(&mut self.definitions) } /// Returns an iterator over the [visitors](SchemaSettings::visitors) being used by this `SchemaGenerator`. diff --git a/schemars/src/ser.rs b/schemars/src/ser.rs index fc2bba0..3f69bef 100644 --- a/schemars/src/ser.rs +++ b/schemars/src/ser.rs @@ -128,7 +128,7 @@ impl<'a> serde::Serializer for Serializer<'a> { self.serialize_none() } - fn serialize_some(mut self, value: &T) -> Result + fn serialize_some(self, value: &T) -> Result where T: serde::Serialize, { @@ -153,7 +153,7 @@ impl<'a> serde::Serializer for Serializer<'a> { if self.gen.settings().option_add_null_type { schema = match schema { Schema::Bool(true) => Schema::Bool(true), - Schema::Bool(false) => <()>::json_schema(&mut self.gen), + Schema::Bool(false) => <()>::json_schema(self.gen), Schema::Object(SchemaObject { instance_type: Some(ref mut instance_type), .. @@ -163,7 +163,7 @@ impl<'a> serde::Serializer for Serializer<'a> { } schema => SchemaObject { subschemas: Some(Box::new(SubschemaValidation { - any_of: Some(vec![schema, <()>::json_schema(&mut self.gen)]), + any_of: Some(vec![schema, <()>::json_schema(self.gen)]), ..Default::default() })), ..Default::default() diff --git a/schemars/tests/examples.rs b/schemars/tests/examples.rs index 2557e1c..bbe9af5 100644 --- a/schemars/tests/examples.rs +++ b/schemars/tests/examples.rs @@ -17,7 +17,7 @@ fn eight() -> i32 { 8 } -fn null() -> () {} +fn null() {} #[test] fn examples() -> TestResult { diff --git a/schemars/tests/validate.rs b/schemars/tests/validate.rs index 58ef080..e381734 100644 --- a/schemars/tests/validate.rs +++ b/schemars/tests/validate.rs @@ -4,7 +4,7 @@ use std::collections::HashMap; use util::*; // In real code, this would typically be a Regex, potentially created in a `lazy_static!`. -static STARTS_WITH_HELLO: &'static str = r"^[Hh]ello\b"; +static STARTS_WITH_HELLO: &str = r"^[Hh]ello\b"; const MIN: u32 = 1; const MAX: u32 = 1000; diff --git a/schemars_derive/src/attr/schemars_to_serde.rs b/schemars_derive/src/attr/schemars_to_serde.rs index 781f5da..7878a2c 100644 --- a/schemars_derive/src/attr/schemars_to_serde.rs +++ b/schemars_derive/src/attr/schemars_to_serde.rs @@ -46,14 +46,14 @@ pub fn process_serde_attrs(input: &mut syn::DeriveInput) -> Result<(), Vec(ctxt: &Ctxt, variants: impl Iterator) { for v in variants { - process_attrs(&ctxt, &mut v.attrs); - process_serde_field_attrs(&ctxt, v.fields.iter_mut()); + process_attrs(ctxt, &mut v.attrs); + process_serde_field_attrs(ctxt, v.fields.iter_mut()); } } fn process_serde_field_attrs<'a>(ctxt: &Ctxt, fields: impl Iterator) { for f in fields { - process_attrs(&ctxt, &mut f.attrs); + process_attrs(ctxt, &mut f.attrs); } } @@ -71,10 +71,10 @@ fn process_attrs(ctxt: &Ctxt, attrs: &mut Vec) { // Copy appropriate #[schemars(...)] attributes to #[serde(...)] attributes let (mut serde_meta, mut schemars_meta_names): (Vec<_>, HashSet<_>) = schemars_attrs .iter() - .flat_map(|at| get_meta_items(&ctxt, at)) + .flat_map(|at| get_meta_items(ctxt, at)) .flatten() .filter_map(|meta| { - let keyword = get_meta_ident(&ctxt, &meta).ok()?; + let keyword = get_meta_ident(ctxt, &meta).ok()?; if keyword.ends_with("with") || !SERDE_KEYWORDS.contains(&keyword.as_ref()) { None } else { @@ -91,10 +91,10 @@ fn process_attrs(ctxt: &Ctxt, attrs: &mut Vec) { // Re-add #[serde(...)] attributes that weren't overridden by #[schemars(...)] attributes for meta in serde_attrs .into_iter() - .flat_map(|at| get_meta_items(&ctxt, &at)) + .flat_map(|at| get_meta_items(ctxt, &at)) .flatten() { - if let Ok(i) = get_meta_ident(&ctxt, &meta) { + if let Ok(i) = get_meta_ident(ctxt, &meta) { if !schemars_meta_names.contains(&i) && SERDE_KEYWORDS.contains(&i.as_ref()) && i != "bound" diff --git a/schemars_derive/src/attr/validation.rs b/schemars_derive/src/attr/validation.rs index 1a93a2a..a089a1d 100644 --- a/schemars_derive/src/attr/validation.rs +++ b/schemars_derive/src/attr/validation.rs @@ -112,7 +112,7 @@ impl ValidationAttrs { } else if self.length_equal.is_some() { mutual_exclusive_error(&nv.path, "equal") } else { - self.length_min = str_or_num_to_expr(&errors, "min", &nv.lit); + self.length_min = str_or_num_to_expr(errors, "min", &nv.lit); } } NestedMeta::Meta(Meta::NameValue(nv)) if nv.path.is_ident("max") => { @@ -121,7 +121,7 @@ impl ValidationAttrs { } else if self.length_equal.is_some() { mutual_exclusive_error(&nv.path, "equal") } else { - self.length_max = str_or_num_to_expr(&errors, "max", &nv.lit); + self.length_max = str_or_num_to_expr(errors, "max", &nv.lit); } } NestedMeta::Meta(Meta::NameValue(nv)) if nv.path.is_ident("equal") => { @@ -133,14 +133,14 @@ impl ValidationAttrs { mutual_exclusive_error(&nv.path, "max") } else { self.length_equal = - str_or_num_to_expr(&errors, "equal", &nv.lit); + str_or_num_to_expr(errors, "equal", &nv.lit); } } meta => { if !ignore_errors { errors.error_spanned_by( meta, - format!("unknown item in schemars length attribute"), + "unknown item in schemars length attribute".to_string(), ); } } @@ -155,21 +155,21 @@ impl ValidationAttrs { if self.range_min.is_some() { duplicate_error(&nv.path) } else { - self.range_min = str_or_num_to_expr(&errors, "min", &nv.lit); + self.range_min = str_or_num_to_expr(errors, "min", &nv.lit); } } NestedMeta::Meta(Meta::NameValue(nv)) if nv.path.is_ident("max") => { if self.range_max.is_some() { duplicate_error(&nv.path) } else { - self.range_max = str_or_num_to_expr(&errors, "max", &nv.lit); + self.range_max = str_or_num_to_expr(errors, "max", &nv.lit); } } meta => { if !ignore_errors { errors.error_spanned_by( meta, - format!("unknown item in schemars range attribute"), + "unknown item in schemars range attribute".to_string(), ); } } @@ -247,7 +247,7 @@ impl ValidationAttrs { if !ignore_errors { errors.error_spanned_by( meta, - format!("unknown item in schemars regex attribute"), + "unknown item in schemars regex attribute".to_string(), ); } } @@ -261,8 +261,8 @@ impl ValidationAttrs { if path.is_ident("contains") => { match (&self.contains, &self.regex) { - (Some(_), _) => duplicate_error(&path), - (None, Some(_)) => mutual_exclusive_error(&path, "regex"), + (Some(_), _) => duplicate_error(path), + (None, Some(_)) => mutual_exclusive_error(path, "regex"), (None, None) => { self.contains = get_lit_str(errors, attr_type, "contains", lit) .map(|litstr| litstr.value()) @@ -292,9 +292,7 @@ impl ValidationAttrs { if !ignore_errors { errors.error_spanned_by( meta, - format!( - "unknown item in schemars contains attribute" - ), + "unknown item in schemars contains attribute".to_string(), ); } } @@ -319,7 +317,7 @@ impl ValidationAttrs { if let Some(length_min) = self .length_min .as_ref() - .or_else(|| self.length_equal.as_ref()) + .or(self.length_equal.as_ref()) { string_validation.push(quote! { validation.min_length = Some(#length_min as u32); @@ -332,7 +330,7 @@ impl ValidationAttrs { if let Some(length_max) = self .length_max .as_ref() - .or_else(|| self.length_equal.as_ref()) + .or(self.length_equal.as_ref()) { string_validation.push(quote! { validation.max_length = Some(#length_max as u32);