diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7062361cb..cc9689f44 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,7 +33,7 @@ jobs: strategy: fail-fast: false matrix: - rust: [beta, stable, 1.53.0, 1.46.0, 1.45.0, 1.40.0, 1.38.0, 1.36.0] + rust: [beta, stable, 1.56.1, 1.53.0, 1.46.0, 1.45.0, 1.40.0, 1.38.0, 1.36.0] os: [ubuntu] include: - rust: stable @@ -51,9 +51,9 @@ jobs: - run: cargo check --manifest-path tests/crate/Cargo.toml --no-default-features --features alloc - run: cargo check --manifest-path tests/crate/Cargo.toml --no-default-features --features alloc,raw_value - run: cargo check --features preserve_order - if: matrix.rust != '1.45.0' && matrix.rust != '1.40.0' && matrix.rust != '1.38.0' && matrix.rust != '1.36.0' + if: matrix.rust != '1.53.0' && matrix.rust != '1.46.0' && matrix.rust != '1.45.0' && matrix.rust != '1.40.0' && matrix.rust != '1.38.0' && matrix.rust != '1.36.0' - run: cargo check --manifest-path tests/crate/Cargo.toml --no-default-features --features alloc,preserve_order - if: matrix.rust != '1.45.0' && matrix.rust != '1.40.0' && matrix.rust != '1.38.0' && matrix.rust != '1.36.0' + if: matrix.rust != '1.53.0' && matrix.rust != '1.46.0' && matrix.rust != '1.45.0' && matrix.rust != '1.40.0' && matrix.rust != '1.38.0' && matrix.rust != '1.36.0' - name: Build without std run: | rustup target add aarch64-unknown-none @@ -101,5 +101,15 @@ jobs: steps: - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@nightly - - run: cargo install cargo-fuzz --debug + - uses: dtolnay/install@cargo-fuzz - run: cargo fuzz build -O + + outdated: + name: Outdated + runs-on: ubuntu-latest + if: github.event_name != 'pull_request' + steps: + - uses: actions/checkout@v3 + - uses: dtolnay/install@cargo-outdated + - run: cargo outdated --workspace --exit-code 1 + - run: cargo outdated --manifest-path fuzz/Cargo.toml --exit-code 1 diff --git a/Cargo.toml b/Cargo.toml index 832881648..28da336a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "serde_json" -version = "1.0.81" # remember to update html_root_url +version = "1.0.82" # remember to update html_root_url authors = ["Erick Tryzelaar ", "David Tolnay "] license = "MIT OR Apache-2.0" description = "A JSON serialization file format" diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 78925766c..c475cd9f1 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -1,9 +1,8 @@ [package] name = "serde_json-fuzz" version = "0.0.0" -authors = ["David Korczynski "] publish = false -edition = "2018" +edition = "2021" [package.metadata] cargo-fuzz = true @@ -15,6 +14,7 @@ serde_json = { path = ".." } [[bin]] name = "from_slice" path = "fuzz_targets/from_slice.rs" +test = false +doc = false -# Prevent this from interfering with workspaces [workspace] diff --git a/src/error.rs b/src/error.rs index 6390c4368..1875ef08b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -284,9 +284,9 @@ impl Error { impl Display for ErrorCode { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - ErrorCode::Message(ref msg) => f.write_str(msg), - ErrorCode::Io(ref err) => Display::fmt(err, f), + match self { + ErrorCode::Message(msg) => f.write_str(msg), + ErrorCode::Io(err) => Display::fmt(err, f), ErrorCode::EofWhileParsingList => f.write_str("EOF while parsing a list"), ErrorCode::EofWhileParsingObject => f.write_str("EOF while parsing an object"), ErrorCode::EofWhileParsingString => f.write_str("EOF while parsing a string"), @@ -318,8 +318,8 @@ impl Display for ErrorCode { impl serde::de::StdError for Error { #[cfg(feature = "std")] fn source(&self) -> Option<&(dyn error::Error + 'static)> { - match self.err.code { - ErrorCode::Io(ref err) => Some(err), + match &self.err.code { + ErrorCode::Io(err) => Some(err), _ => None, } } @@ -438,7 +438,7 @@ fn parse_line_col(msg: &mut String) -> Option<(usize, usize)> { } fn starts_with_digit(slice: &str) -> bool { - match slice.as_bytes().get(0) { + match slice.as_bytes().first() { None => false, Some(&byte) => byte >= b'0' && byte <= b'9', } diff --git a/src/lib.rs b/src/lib.rs index 37d323da5..7fc351213 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -300,7 +300,7 @@ //! [macro]: https://docs.serde.rs/serde_json/macro.json.html //! [`serde-json-core`]: https://github.com/rust-embedded-community/serde-json-core -#![doc(html_root_url = "https://docs.rs/serde_json/1.0.81")] +#![doc(html_root_url = "https://docs.rs/serde_json/1.0.82")] // Ignored clippy lints #![allow( clippy::collapsible_else_if, @@ -308,6 +308,7 @@ clippy::deprecated_cfg_attr, clippy::doc_markdown, clippy::excessive_precision, + clippy::explicit_auto_deref, clippy::float_cmp, clippy::manual_range_contains, clippy::match_like_matches_macro, diff --git a/src/map.rs b/src/map.rs index bf8f4dc9f..87cf54566 100644 --- a/src/map.rs +++ b/src/map.rs @@ -323,10 +323,10 @@ impl Eq for Map {} /// # /// # let val = &Value::String("".to_owned()); /// # let _ = -/// match *val { -/// Value::String(ref s) => Some(s.as_str()), -/// Value::Array(ref arr) => arr[0].as_str(), -/// Value::Object(ref map) => map["type"].as_str(), +/// match val { +/// Value::String(s) => Some(s.as_str()), +/// Value::Array(arr) => arr[0].as_str(), +/// Value::Object(map) => map["type"].as_str(), /// _ => None, /// } /// # ; @@ -530,9 +530,9 @@ impl<'a> Entry<'a> { /// assert_eq!(map.entry("serde").key(), &"serde"); /// ``` pub fn key(&self) -> &String { - match *self { - Entry::Vacant(ref e) => e.key(), - Entry::Occupied(ref e) => e.key(), + match self { + Entry::Vacant(e) => e.key(), + Entry::Occupied(e) => e.key(), } } diff --git a/src/read.rs b/src/read.rs index 1319d89c9..fc3a3ca74 100644 --- a/src/read.rs +++ b/src/read.rs @@ -252,7 +252,7 @@ where Some(ch) => { #[cfg(feature = "raw_value")] { - if let Some(ref mut buf) = self.raw_buffer { + if let Some(buf) = &mut self.raw_buffer { buf.push(ch); } } @@ -263,7 +263,7 @@ where Some(Ok(ch)) => { #[cfg(feature = "raw_value")] { - if let Some(ref mut buf) = self.raw_buffer { + if let Some(buf) = &mut self.raw_buffer { buf.push(ch); } } @@ -298,7 +298,7 @@ where #[cfg(feature = "raw_value")] fn discard(&mut self) { if let Some(ch) = self.ch.take() { - if let Some(ref mut buf) = self.raw_buffer { + if let Some(buf) = &mut self.raw_buffer { buf.push(ch); } } diff --git a/src/ser.rs b/src/ser.rs index db77cd883..64cb00e1a 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -533,11 +533,8 @@ where where T: ?Sized + Serialize, { - match *self { - Compound::Map { - ref mut ser, - ref mut state, - } => { + match self { + Compound::Map { ser, state } => { tri!(ser .formatter .begin_array_value(&mut ser.writer, *state == State::First) @@ -671,11 +668,8 @@ where where T: ?Sized + Serialize, { - match *self { - Compound::Map { - ref mut ser, - ref mut state, - } => { + match self { + Compound::Map { ser, state } => { tri!(ser .formatter .begin_object_key(&mut ser.writer, *state == State::First) @@ -702,8 +696,8 @@ where where T: ?Sized + Serialize, { - match *self { - Compound::Map { ref mut ser, .. } => { + match self { + Compound::Map { ser, .. } => { tri!(ser .formatter .begin_object_value(&mut ser.writer) @@ -753,10 +747,10 @@ where where T: ?Sized + Serialize, { - match *self { + match self { Compound::Map { .. } => ser::SerializeMap::serialize_entry(self, key, value), #[cfg(feature = "arbitrary_precision")] - Compound::Number { ref mut ser, .. } => { + Compound::Number { ser, .. } => { if key == crate::number::TOKEN { tri!(value.serialize(NumberStrEmitter(ser))); Ok(()) @@ -765,7 +759,7 @@ where } } #[cfg(feature = "raw_value")] - Compound::RawValue { ref mut ser, .. } => { + Compound::RawValue { ser, .. } => { if key == crate::raw::TOKEN { tri!(value.serialize(RawValueStrEmitter(ser))); Ok(()) diff --git a/src/value/de.rs b/src/value/de.rs index 75e49df55..cc1d38565 100644 --- a/src/value/de.rs +++ b/src/value/de.rs @@ -648,8 +648,8 @@ macro_rules! deserialize_value_ref_number { where V: Visitor<'de>, { - match *self { - Value::Number(ref n) => n.deserialize_any(visitor), + match self { + Value::Number(n) => n.deserialize_any(visitor), _ => Err(self.invalid_type(&visitor)), } } @@ -659,8 +659,8 @@ macro_rules! deserialize_value_ref_number { where V: Visitor<'de>, { - match *self { - Value::Number(ref n) => n.$method(visitor), + match self { + Value::Number(n) => n.$method(visitor), _ => self.deserialize_any(visitor), } } @@ -710,13 +710,13 @@ impl<'de> serde::Deserializer<'de> for &'de Value { where V: Visitor<'de>, { - match *self { + match self { Value::Null => visitor.visit_unit(), - Value::Bool(v) => visitor.visit_bool(v), - Value::Number(ref n) => n.deserialize_any(visitor), - Value::String(ref v) => visitor.visit_borrowed_str(v), - Value::Array(ref v) => visit_array_ref(v, visitor), - Value::Object(ref v) => visit_object_ref(v, visitor), + Value::Bool(v) => visitor.visit_bool(*v), + Value::Number(n) => n.deserialize_any(visitor), + Value::String(v) => visitor.visit_borrowed_str(v), + Value::Array(v) => visit_array_ref(v, visitor), + Value::Object(v) => visit_object_ref(v, visitor), } } @@ -755,8 +755,8 @@ impl<'de> serde::Deserializer<'de> for &'de Value { where V: Visitor<'de>, { - let (variant, value) = match *self { - Value::Object(ref value) => { + let (variant, value) = match self { + Value::Object(value) => { let mut iter = value.into_iter(); let (variant, value) = match iter.next() { Some(v) => v, @@ -776,8 +776,8 @@ impl<'de> serde::Deserializer<'de> for &'de Value { } (variant, Some(value)) } - Value::String(ref variant) => (variant, None), - ref other => { + Value::String(variant) => (variant, None), + other => { return Err(serde::de::Error::invalid_type( other.unexpected(), &"string or map", @@ -831,8 +831,8 @@ impl<'de> serde::Deserializer<'de> for &'de Value { where V: Visitor<'de>, { - match *self { - Value::String(ref v) => visitor.visit_borrowed_str(v), + match self { + Value::String(v) => visitor.visit_borrowed_str(v), _ => Err(self.invalid_type(&visitor)), } } @@ -848,9 +848,9 @@ impl<'de> serde::Deserializer<'de> for &'de Value { where V: Visitor<'de>, { - match *self { - Value::String(ref v) => visitor.visit_borrowed_str(v), - Value::Array(ref v) => visit_array_ref(v, visitor), + match self { + Value::String(v) => visitor.visit_borrowed_str(v), + Value::Array(v) => visit_array_ref(v, visitor), _ => Err(self.invalid_type(&visitor)), } } @@ -883,8 +883,8 @@ impl<'de> serde::Deserializer<'de> for &'de Value { where V: Visitor<'de>, { - match *self { - Value::Array(ref v) => visit_array_ref(v, visitor), + match self { + Value::Array(v) => visit_array_ref(v, visitor), _ => Err(self.invalid_type(&visitor)), } } @@ -912,8 +912,8 @@ impl<'de> serde::Deserializer<'de> for &'de Value { where V: Visitor<'de>, { - match *self { - Value::Object(ref v) => visit_object_ref(v, visitor), + match self { + Value::Object(v) => visit_object_ref(v, visitor), _ => Err(self.invalid_type(&visitor)), } } @@ -927,9 +927,9 @@ impl<'de> serde::Deserializer<'de> for &'de Value { where V: Visitor<'de>, { - match *self { - Value::Array(ref v) => visit_array_ref(v, visitor), - Value::Object(ref v) => visit_object_ref(v, visitor), + match self { + Value::Array(v) => visit_array_ref(v, visitor), + Value::Object(v) => visit_object_ref(v, visitor), _ => Err(self.invalid_type(&visitor)), } } @@ -1274,11 +1274,11 @@ impl Value { #[cold] fn unexpected(&self) -> Unexpected { - match *self { + match self { Value::Null => Unexpected::Unit, - Value::Bool(b) => Unexpected::Bool(b), - Value::Number(ref n) => n.unexpected(), - Value::String(ref s) => Unexpected::Str(s), + Value::Bool(b) => Unexpected::Bool(*b), + Value::Number(n) => n.unexpected(), + Value::String(s) => Unexpected::Str(s), Value::Array(_) => Unexpected::Seq, Value::Object(_) => Unexpected::Map, } diff --git a/src/value/from.rs b/src/value/from.rs index 7b37ef688..858a6e48a 100644 --- a/src/value/from.rs +++ b/src/value/from.rs @@ -268,3 +268,15 @@ impl From<()> for Value { Value::Null } } + +impl From> for Value +where + T: Into, +{ + fn from(opt: Option) -> Self { + match opt { + None => Value::Null, + Some(value) => Into::into(value), + } + } +} diff --git a/src/value/index.rs b/src/value/index.rs index 0d90a5d9a..c74042b75 100644 --- a/src/value/index.rs +++ b/src/value/index.rs @@ -53,20 +53,20 @@ pub trait Index: private::Sealed { impl Index for usize { fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> { - match *v { - Value::Array(ref vec) => vec.get(*self), + match v { + Value::Array(vec) => vec.get(*self), _ => None, } } fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> { - match *v { - Value::Array(ref mut vec) => vec.get_mut(*self), + match v { + Value::Array(vec) => vec.get_mut(*self), _ => None, } } fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value { - match *v { - Value::Array(ref mut vec) => { + match v { + Value::Array(vec) => { let len = vec.len(); vec.get_mut(*self).unwrap_or_else(|| { panic!( @@ -82,23 +82,23 @@ impl Index for usize { impl Index for str { fn index_into<'v>(&self, v: &'v Value) -> Option<&'v Value> { - match *v { - Value::Object(ref map) => map.get(self), + match v { + Value::Object(map) => map.get(self), _ => None, } } fn index_into_mut<'v>(&self, v: &'v mut Value) -> Option<&'v mut Value> { - match *v { - Value::Object(ref mut map) => map.get_mut(self), + match v { + Value::Object(map) => map.get_mut(self), _ => None, } } fn index_or_insert<'v>(&self, v: &'v mut Value) -> &'v mut Value { - if let Value::Null = *v { + if let Value::Null = v { *v = Value::Object(Map::new()); } - match *v { - Value::Object(ref mut map) => map.entry(self.to_owned()).or_insert(Value::Null), + match v { + Value::Object(map) => map.entry(self.to_owned()).or_insert(Value::Null), _ => panic!("cannot access key {:?} in JSON {}", self, Type(v)), } } diff --git a/src/value/mod.rs b/src/value/mod.rs index 3f00c9585..4793e93ec 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -176,17 +176,17 @@ pub enum Value { impl Debug for Value { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - match *self { + match self { Value::Null => formatter.debug_tuple("Null").finish(), - Value::Bool(v) => formatter.debug_tuple("Bool").field(&v).finish(), - Value::Number(ref v) => Debug::fmt(v, formatter), - Value::String(ref v) => formatter.debug_tuple("String").field(v).finish(), - Value::Array(ref v) => { + Value::Bool(v) => formatter.debug_tuple("Bool").field(v).finish(), + Value::Number(v) => Debug::fmt(v, formatter), + Value::String(v) => formatter.debug_tuple("String").field(v).finish(), + Value::Array(v) => { formatter.write_str("Array(")?; Debug::fmt(v, formatter)?; formatter.write_str(")") } - Value::Object(ref v) => { + Value::Object(v) => { formatter.write_str("Object(")?; Debug::fmt(v, formatter)?; formatter.write_str(")") @@ -365,8 +365,8 @@ impl Value { /// assert_eq!(v["b"].as_object(), None); /// ``` pub fn as_object(&self) -> Option<&Map> { - match *self { - Value::Object(ref map) => Some(map), + match self { + Value::Object(map) => Some(map), _ => None, } } @@ -383,8 +383,8 @@ impl Value { /// assert_eq!(v, json!({ "a": {} })); /// ``` pub fn as_object_mut(&mut self) -> Option<&mut Map> { - match *self { - Value::Object(ref mut map) => Some(map), + match self { + Value::Object(map) => Some(map), _ => None, } } @@ -424,8 +424,8 @@ impl Value { /// assert_eq!(v["b"].as_array(), None); /// ``` pub fn as_array(&self) -> Option<&Vec> { - match *self { - Value::Array(ref array) => Some(&*array), + match self { + Value::Array(array) => Some(array), _ => None, } } @@ -442,8 +442,8 @@ impl Value { /// assert_eq!(v, json!({ "a": [] })); /// ``` pub fn as_array_mut(&mut self) -> Option<&mut Vec> { - match *self { - Value::Array(ref mut list) => Some(list), + match self { + Value::Array(list) => Some(list), _ => None, } } @@ -491,8 +491,8 @@ impl Value { /// println!("The value is: {}", v["a"].as_str().unwrap()); /// ``` pub fn as_str(&self) -> Option<&str> { - match *self { - Value::String(ref s) => Some(s), + match self { + Value::String(s) => Some(s), _ => None, } } @@ -537,8 +537,8 @@ impl Value { /// assert!(!v["c"].is_i64()); /// ``` pub fn is_i64(&self) -> bool { - match *self { - Value::Number(ref n) => n.is_i64(), + match self { + Value::Number(n) => n.is_i64(), _ => false, } } @@ -562,8 +562,8 @@ impl Value { /// assert!(!v["c"].is_u64()); /// ``` pub fn is_u64(&self) -> bool { - match *self { - Value::Number(ref n) => n.is_u64(), + match self { + Value::Number(n) => n.is_u64(), _ => false, } } @@ -588,8 +588,8 @@ impl Value { /// assert!(!v["c"].is_f64()); /// ``` pub fn is_f64(&self) -> bool { - match *self { - Value::Number(ref n) => n.is_f64(), + match self { + Value::Number(n) => n.is_f64(), _ => false, } } @@ -608,8 +608,8 @@ impl Value { /// assert_eq!(v["c"].as_i64(), None); /// ``` pub fn as_i64(&self) -> Option { - match *self { - Value::Number(ref n) => n.as_i64(), + match self { + Value::Number(n) => n.as_i64(), _ => None, } } @@ -627,8 +627,8 @@ impl Value { /// assert_eq!(v["c"].as_u64(), None); /// ``` pub fn as_u64(&self) -> Option { - match *self { - Value::Number(ref n) => n.as_u64(), + match self { + Value::Number(n) => n.as_u64(), _ => None, } } @@ -646,8 +646,8 @@ impl Value { /// assert_eq!(v["c"].as_f64(), Some(-64.0)); /// ``` pub fn as_f64(&self) -> Option { - match *self { - Value::Number(ref n) => n.as_f64(), + match self { + Value::Number(n) => n.as_f64(), _ => None, } } diff --git a/src/value/ser.rs b/src/value/ser.rs index 098703767..c142dacbf 100644 --- a/src/value/ser.rs +++ b/src/value/ser.rs @@ -18,14 +18,14 @@ impl Serialize for Value { where S: ::serde::Serializer, { - match *self { + match self { Value::Null => serializer.serialize_unit(), - Value::Bool(b) => serializer.serialize_bool(b), - Value::Number(ref n) => n.serialize(serializer), - Value::String(ref s) => serializer.serialize_str(s), - Value::Array(ref v) => v.serialize(serializer), + Value::Bool(b) => serializer.serialize_bool(*b), + Value::Number(n) => n.serialize(serializer), + Value::String(s) => serializer.serialize_str(s), + Value::Array(v) => v.serialize(serializer), #[cfg(any(feature = "std", feature = "alloc"))] - Value::Object(ref m) => { + Value::Object(m) => { use serde::ser::SerializeMap; let mut map = tri!(serializer.serialize_map(Some(m.len()))); for (k, v) in m { @@ -384,10 +384,8 @@ impl serde::ser::SerializeMap for SerializeMap { where T: ?Sized + Serialize, { - match *self { - SerializeMap::Map { - ref mut next_key, .. - } => { + match self { + SerializeMap::Map { next_key, .. } => { *next_key = Some(tri!(key.serialize(MapKeySerializer))); Ok(()) } @@ -402,11 +400,8 @@ impl serde::ser::SerializeMap for SerializeMap { where T: ?Sized + Serialize, { - match *self { - SerializeMap::Map { - ref mut map, - ref mut next_key, - } => { + match self { + SerializeMap::Map { map, next_key } => { let key = next_key.take(); // Panic because this indicates a bug in the program rather than an // expected failure. @@ -622,10 +617,10 @@ impl serde::ser::SerializeStruct for SerializeMap { where T: ?Sized + Serialize, { - match *self { + match self { SerializeMap::Map { .. } => serde::ser::SerializeMap::serialize_entry(self, key, value), #[cfg(feature = "arbitrary_precision")] - SerializeMap::Number { ref mut out_value } => { + SerializeMap::Number { out_value } => { if key == crate::number::TOKEN { *out_value = Some(value.serialize(NumberValueEmitter)?); Ok(()) @@ -634,7 +629,7 @@ impl serde::ser::SerializeStruct for SerializeMap { } } #[cfg(feature = "raw_value")] - SerializeMap::RawValue { ref mut out_value } => { + SerializeMap::RawValue { out_value } => { if key == crate::raw::TOKEN { *out_value = Some(value.serialize(RawValueEmitter)?); Ok(()) diff --git a/tests/macros/mod.rs b/tests/macros/mod.rs index 8ac4619f7..aaf820f07 100644 --- a/tests/macros/mod.rs +++ b/tests/macros/mod.rs @@ -1,3 +1,5 @@ +#![allow(unused_macro_rules)] + macro_rules! json_str { ([]) => { "[]" diff --git a/tests/test.rs b/tests/test.rs index b11635e75..13d30c490 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,6 +1,7 @@ #![cfg(not(feature = "preserve_order"))] #![allow( clippy::cast_precision_loss, + clippy::derive_partial_eq_without_eq, clippy::excessive_precision, clippy::float_cmp, clippy::items_after_statements,