Rollup merge of #75201 - Hirrolot:hirrolot/fix-clippy-warnings, r=varkor

Fix some Clippy warnings in librustc_serialize
This commit is contained in:
Yuki Okushi 2020-08-13 11:05:33 +09:00 committed by GitHub
commit d0414b57b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 74 deletions

View File

@ -86,11 +86,9 @@ where
{ {
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> { fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
e.emit_map(self.len(), |e| { e.emit_map(self.len(), |e| {
let mut i = 0; for (i, (key, val)) in self.iter().enumerate() {
for (key, val) in self {
e.emit_map_elt_key(i, |e| key.encode(e))?; e.emit_map_elt_key(i, |e| key.encode(e))?;
e.emit_map_elt_val(i, |e| val.encode(e))?; e.emit_map_elt_val(i, |e| val.encode(e))?;
i += 1;
} }
Ok(()) Ok(())
}) })
@ -121,10 +119,8 @@ where
{ {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_seq(self.len(), |s| { s.emit_seq(self.len(), |s| {
let mut i = 0; for (i, e) in self.iter().enumerate() {
for e in self {
s.emit_seq_elt(i, |s| e.encode(s))?; s.emit_seq_elt(i, |s| e.encode(s))?;
i += 1;
} }
Ok(()) Ok(())
}) })
@ -154,11 +150,9 @@ where
{ {
fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> { fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
e.emit_map(self.len(), |e| { e.emit_map(self.len(), |e| {
let mut i = 0; for (i, (key, val)) in self.iter().enumerate() {
for (key, val) in self {
e.emit_map_elt_key(i, |e| key.encode(e))?; e.emit_map_elt_key(i, |e| key.encode(e))?;
e.emit_map_elt_val(i, |e| val.encode(e))?; e.emit_map_elt_val(i, |e| val.encode(e))?;
i += 1;
} }
Ok(()) Ok(())
}) })
@ -192,10 +186,8 @@ where
{ {
fn encode<E: Encoder>(&self, s: &mut E) -> Result<(), E::Error> { fn encode<E: Encoder>(&self, s: &mut E) -> Result<(), E::Error> {
s.emit_seq(self.len(), |s| { s.emit_seq(self.len(), |s| {
let mut i = 0; for (i, e) in self.iter().enumerate() {
for e in self {
s.emit_seq_elt(i, |s| e.encode(s))?; s.emit_seq_elt(i, |s| e.encode(s))?;
i += 1;
} }
Ok(()) Ok(())
}) })
@ -227,11 +219,9 @@ where
{ {
fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> { fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
e.emit_map(self.len(), |e| { e.emit_map(self.len(), |e| {
let mut i = 0; for (i, (key, val)) in self.iter().enumerate() {
for (key, val) in self {
e.emit_map_elt_key(i, |e| key.encode(e))?; e.emit_map_elt_key(i, |e| key.encode(e))?;
e.emit_map_elt_val(i, |e| val.encode(e))?; e.emit_map_elt_val(i, |e| val.encode(e))?;
i += 1;
} }
Ok(()) Ok(())
}) })
@ -265,10 +255,8 @@ where
{ {
fn encode<E: Encoder>(&self, s: &mut E) -> Result<(), E::Error> { fn encode<E: Encoder>(&self, s: &mut E) -> Result<(), E::Error> {
s.emit_seq(self.len(), |s| { s.emit_seq(self.len(), |s| {
let mut i = 0; for (i, e) in self.iter().enumerate() {
for e in self {
s.emit_seq_elt(i, |s| e.encode(s))?; s.emit_seq_elt(i, |s| e.encode(s))?;
i += 1;
} }
Ok(()) Ok(())
}) })

View File

@ -78,7 +78,6 @@
//! data_vector: Vec<u8>, //! data_vector: Vec<u8>,
//! } //! }
//! //!
//! fn main() {
//! let object = TestStruct { //! let object = TestStruct {
//! data_int: 1, //! data_int: 1,
//! data_str: "homura".to_string(), //! data_str: "homura".to_string(),
@ -90,7 +89,6 @@
//! //!
//! // Deserialize using `json::decode` //! // Deserialize using `json::decode`
//! let decoded: TestStruct = json::decode(&encoded[..]).unwrap(); //! let decoded: TestStruct = json::decode(&encoded[..]).unwrap();
//! }
//! ``` //! ```
//! //!
//! ## Using the `ToJson` trait //! ## Using the `ToJson` trait
@ -125,7 +123,6 @@
//! val: Json, //! val: Json,
//! } //! }
//! //!
//! fn main() {
//! let num = ComplexNum { a: 0.0001, b: 12.539 }; //! let num = ComplexNum { a: 0.0001, b: 12.539 };
//! let data: String = json::encode(&ComplexNumRecord{ //! let data: String = json::encode(&ComplexNumRecord{
//! uid: 1, //! uid: 1,
@ -134,7 +131,6 @@
//! }).unwrap(); //! }).unwrap();
//! println!("data: {}", data); //! println!("data: {}", data);
//! // data: {"uid":1,"dsc":"test","val":"0.0001+12.539i"}; //! // data: {"uid":1,"dsc":"test","val":"0.0001+12.539i"};
//! }
//! ``` //! ```
//! //!
//! ### Verbose example of `ToJson` usage //! ### Verbose example of `ToJson` usage
@ -164,7 +160,6 @@
//! } //! }
//! } //! }
//! //!
//! fn main() {
//! // Serialize using `ToJson` //! // Serialize using `ToJson`
//! let input_data = TestStruct { //! let input_data = TestStruct {
//! data_int: 1, //! data_int: 1,
@ -176,7 +171,6 @@
//! //!
//! // Deserialize like before //! // Deserialize like before
//! let decoded: TestStruct = json::decode(&json_str).unwrap(); //! let decoded: TestStruct = json::decode(&json_str).unwrap();
//! }
//! ``` //! ```
use self::DecoderError::*; use self::DecoderError::*;
@ -1269,34 +1263,22 @@ impl Json {
/// Returns `true` if the Json value is a `Number`. /// Returns `true` if the Json value is a `Number`.
pub fn is_number(&self) -> bool { pub fn is_number(&self) -> bool {
match *self { matches!(*self, Json::I64(_) | Json::U64(_) | Json::F64(_))
Json::I64(_) | Json::U64(_) | Json::F64(_) => true,
_ => false,
}
} }
/// Returns `true` if the Json value is a `i64`. /// Returns `true` if the Json value is a `i64`.
pub fn is_i64(&self) -> bool { pub fn is_i64(&self) -> bool {
match *self { matches!(*self, Json::I64(_))
Json::I64(_) => true,
_ => false,
}
} }
/// Returns `true` if the Json value is a `u64`. /// Returns `true` if the Json value is a `u64`.
pub fn is_u64(&self) -> bool { pub fn is_u64(&self) -> bool {
match *self { matches!(*self, Json::U64(_))
Json::U64(_) => true,
_ => false,
}
} }
/// Returns `true` if the Json value is a `f64`. /// Returns `true` if the Json value is a `f64`.
pub fn is_f64(&self) -> bool { pub fn is_f64(&self) -> bool {
match *self { matches!(*self, Json::F64(_))
Json::F64(_) => true,
_ => false,
}
} }
/// If the Json value is a number, returns or cast it to a `i64`; /// If the Json value is a number, returns or cast it to a `i64`;
@ -1416,6 +1398,7 @@ enum ParserState {
/// structure of the JSON stream. /// structure of the JSON stream.
/// ///
/// An example is `foo.bar[3].x`. /// An example is `foo.bar[3].x`.
#[derive(Default)]
pub struct Stack { pub struct Stack {
stack: Vec<InternalStackElement>, stack: Vec<InternalStackElement>,
str_buffer: Vec<u8>, str_buffer: Vec<u8>,
@ -1442,7 +1425,7 @@ enum InternalStackElement {
impl Stack { impl Stack {
pub fn new() -> Stack { pub fn new() -> Stack {
Stack { stack: Vec::new(), str_buffer: Vec::new() } Self::default()
} }
/// Returns The number of elements in the Stack. /// Returns The number of elements in the Stack.
@ -1547,10 +1530,7 @@ impl Stack {
// Used by Parser to test whether the top-most element is an index. // Used by Parser to test whether the top-most element is an index.
fn last_is_index(&self) -> bool { fn last_is_index(&self) -> bool {
match self.stack.last() { matches!(self.stack.last(), Some(InternalIndex(_)))
Some(InternalIndex(_)) => true,
_ => false,
}
} }
// Used by Parser to increment the index of the top-most element. // Used by Parser to increment the index of the top-most element.

View File

@ -118,13 +118,13 @@ impl serialize::Encoder for Encoder {
#[inline] #[inline]
fn emit_f64(&mut self, v: f64) -> EncodeResult { fn emit_f64(&mut self, v: f64) -> EncodeResult {
let as_u64: u64 = unsafe { ::std::mem::transmute(v) }; let as_u64: u64 = v.to_bits();
self.emit_u64(as_u64) self.emit_u64(as_u64)
} }
#[inline] #[inline]
fn emit_f32(&mut self, v: f32) -> EncodeResult { fn emit_f32(&mut self, v: f32) -> EncodeResult {
let as_u32: u32 = unsafe { ::std::mem::transmute(v) }; let as_u32: u32 = v.to_bits();
self.emit_u32(as_u32) self.emit_u32(as_u32)
} }