Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some Clippy warnings in librustc_serialize #75201

Merged
merged 1 commit into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 6 additions & 18 deletions src/librustc_serialize/collection_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,9 @@ where
{
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
e.emit_map(self.len(), |e| {
let mut i = 0;
for (key, val) in self {
for (i, (key, val)) in self.iter().enumerate() {
e.emit_map_elt_key(i, |e| key.encode(e))?;
e.emit_map_elt_val(i, |e| val.encode(e))?;
i += 1;
}
Ok(())
})
Expand Down Expand Up @@ -121,10 +119,8 @@ where
{
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_seq(self.len(), |s| {
let mut i = 0;
for e in self {
for (i, e) in self.iter().enumerate() {
s.emit_seq_elt(i, |s| e.encode(s))?;
i += 1;
}
Ok(())
})
Expand Down Expand Up @@ -154,11 +150,9 @@ where
{
fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
e.emit_map(self.len(), |e| {
let mut i = 0;
for (key, val) in self {
for (i, (key, val)) in self.iter().enumerate() {
e.emit_map_elt_key(i, |e| key.encode(e))?;
e.emit_map_elt_val(i, |e| val.encode(e))?;
i += 1;
}
Ok(())
})
Expand Down Expand Up @@ -192,10 +186,8 @@ where
{
fn encode<E: Encoder>(&self, s: &mut E) -> Result<(), E::Error> {
s.emit_seq(self.len(), |s| {
let mut i = 0;
for e in self {
for (i, e) in self.iter().enumerate() {
s.emit_seq_elt(i, |s| e.encode(s))?;
i += 1;
}
Ok(())
})
Expand Down Expand Up @@ -227,11 +219,9 @@ where
{
fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
e.emit_map(self.len(), |e| {
let mut i = 0;
for (key, val) in self {
for (i, (key, val)) in self.iter().enumerate() {
e.emit_map_elt_key(i, |e| key.encode(e))?;
e.emit_map_elt_val(i, |e| val.encode(e))?;
i += 1;
}
Ok(())
})
Expand Down Expand Up @@ -265,10 +255,8 @@ where
{
fn encode<E: Encoder>(&self, s: &mut E) -> Result<(), E::Error> {
s.emit_seq(self.len(), |s| {
let mut i = 0;
for e in self {
for (i, e) in self.iter().enumerate() {
s.emit_seq_elt(i, |s| e.encode(s))?;
i += 1;
}
Ok(())
})
Expand Down
88 changes: 34 additions & 54 deletions src/librustc_serialize/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,17 @@
//! data_vector: Vec<u8>,
//! }
//!
//! fn main() {
//! let object = TestStruct {
//! data_int: 1,
//! data_str: "homura".to_string(),
//! data_vector: vec![2,3,4,5],
//! };
//! let object = TestStruct {
//! data_int: 1,
//! data_str: "homura".to_string(),
//! data_vector: vec![2,3,4,5],
//! };
//!
//! // Serialize using `json::encode`
//! let encoded = json::encode(&object).unwrap();
//! // Serialize using `json::encode`
//! let encoded = json::encode(&object).unwrap();
//!
//! // Deserialize using `json::decode`
//! let decoded: TestStruct = json::decode(&encoded[..]).unwrap();
//! }
//! // Deserialize using `json::decode`
//! let decoded: TestStruct = json::decode(&encoded[..]).unwrap();
//! ```
//!
//! ## Using the `ToJson` trait
Expand Down Expand Up @@ -125,16 +123,14 @@
//! val: Json,
//! }
//!
//! fn main() {
//! let num = ComplexNum { a: 0.0001, b: 12.539 };
//! let data: String = json::encode(&ComplexNumRecord{
//! uid: 1,
//! dsc: "test".to_string(),
//! val: num.to_json(),
//! }).unwrap();
//! println!("data: {}", data);
//! // data: {"uid":1,"dsc":"test","val":"0.0001+12.539i"};
//! }
//! let num = ComplexNum { a: 0.0001, b: 12.539 };
//! let data: String = json::encode(&ComplexNumRecord{
//! uid: 1,
//! dsc: "test".to_string(),
//! val: num.to_json(),
//! }).unwrap();
//! println!("data: {}", data);
//! // data: {"uid":1,"dsc":"test","val":"0.0001+12.539i"};
//! ```
//!
//! ### Verbose example of `ToJson` usage
Expand Down Expand Up @@ -164,19 +160,17 @@
//! }
//! }
//!
//! fn main() {
//! // Serialize using `ToJson`
//! let input_data = TestStruct {
//! data_int: 1,
//! data_str: "madoka".to_string(),
//! data_vector: vec![2,3,4,5],
//! };
//! let json_obj: Json = input_data.to_json();
//! let json_str: String = json_obj.to_string();
//! // Serialize using `ToJson`
//! let input_data = TestStruct {
//! data_int: 1,
//! data_str: "madoka".to_string(),
//! data_vector: vec![2,3,4,5],
//! };
//! let json_obj: Json = input_data.to_json();
//! let json_str: String = json_obj.to_string();
//!
//! // Deserialize like before
//! let decoded: TestStruct = json::decode(&json_str).unwrap();
//! }
//! // Deserialize like before
//! let decoded: TestStruct = json::decode(&json_str).unwrap();
//! ```

use self::DecoderError::*;
Expand Down Expand Up @@ -1269,34 +1263,22 @@ impl Json {

/// Returns `true` if the Json value is a `Number`.
pub fn is_number(&self) -> bool {
match *self {
Json::I64(_) | Json::U64(_) | Json::F64(_) => true,
_ => false,
}
matches!(*self, Json::I64(_) | Json::U64(_) | Json::F64(_))
}

/// Returns `true` if the Json value is a `i64`.
pub fn is_i64(&self) -> bool {
match *self {
Json::I64(_) => true,
_ => false,
}
matches!(*self, Json::I64(_))
}

/// Returns `true` if the Json value is a `u64`.
pub fn is_u64(&self) -> bool {
match *self {
Json::U64(_) => true,
_ => false,
}
matches!(*self, Json::U64(_))
}

/// Returns `true` if the Json value is a `f64`.
pub fn is_f64(&self) -> bool {
match *self {
Json::F64(_) => true,
_ => false,
}
matches!(*self, Json::F64(_))
}

/// If the Json value is a number, returns or cast it to a `i64`;
Expand Down Expand Up @@ -1416,6 +1398,7 @@ enum ParserState {
/// structure of the JSON stream.
///
/// An example is `foo.bar[3].x`.
#[derive(Default)]
pub struct Stack {
stack: Vec<InternalStackElement>,
str_buffer: Vec<u8>,
Expand All @@ -1442,7 +1425,7 @@ enum InternalStackElement {

impl Stack {
pub fn new() -> Stack {
Stack { stack: Vec::new(), str_buffer: Vec::new() }
Self::default()
}

/// Returns The number of elements in the Stack.
Expand Down Expand Up @@ -1547,10 +1530,7 @@ impl Stack {

// Used by Parser to test whether the top-most element is an index.
fn last_is_index(&self) -> bool {
match self.stack.last() {
Some(InternalIndex(_)) => true,
_ => false,
}
matches!(self.stack.last(), Some(InternalIndex(_)))
}

// Used by Parser to increment the index of the top-most element.
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_serialize/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ impl serialize::Encoder for Encoder {

#[inline]
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)
}

#[inline]
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)
}

Expand Down