Skip to content

Commit 422e16a

Browse files
committed
fix clippy lints
1 parent 80ec808 commit 422e16a

File tree

16 files changed

+93
-70
lines changed

16 files changed

+93
-70
lines changed

.evergreen/check-clippy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
set -o errexit
44

55
. ~/.cargo/env
6-
cargo clippy --all-targets --all-features -p mongodb -- -D warnings
6+
cargo clippy --all-targets --all-features -p bson -- -D warnings

src/bson.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl Display for Bson {
159159
Bson::I64(i) => write!(fmt, "{}", i),
160160
Bson::TimeStamp(i) => {
161161
let time = (i >> 32) as i32;
162-
let inc = (i & 0xFFFFFFFF) as i32;
162+
let inc = (i & 0xFFFF_FFFF) as i32;
163163

164164
write!(fmt, "Timestamp({}, {})", time, inc)
165165
}
@@ -252,7 +252,7 @@ where
252252
T: Clone + Into<Bson>,
253253
{
254254
fn from(s: &[T]) -> Bson {
255-
Bson::Array(s.into_iter().cloned().map(|val| val.into()).collect())
255+
Bson::Array(s.iter().cloned().map(|val| val.into()).collect())
256256
}
257257
}
258258

@@ -372,7 +372,7 @@ impl From<Bson> for Value {
372372
Bson::ObjectId(v) => json!({"$oid": v.to_string()}),
373373
Bson::UtcDatetime(v) => json!({
374374
"$date": {
375-
"$numberLong": (v.timestamp() * 1000) + ((v.nanosecond() / 1000000) as i64)
375+
"$numberLong": (v.timestamp() * 1000) + ((v.nanosecond() / 1_000_000) as i64)
376376
}
377377
}),
378378
// FIXME: Don't know what is the best way to encode Symbol type
@@ -475,7 +475,7 @@ impl Bson {
475475
Bson::UtcDatetime(ref v) => {
476476
doc! {
477477
"$date": {
478-
"$numberLong" => (v.timestamp() * 1000) + v.nanosecond() as i64 / 1000000,
478+
"$numberLong" => (v.timestamp() * 1000) + v.nanosecond() as i64 / 1_000_000,
479479
}
480480
}
481481
}

src/decimal128.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ impl Decimal128 {
2424
///
2525
/// let dec128 = Decimal128::from_str("1.05E+3");
2626
/// ```
27+
#[allow(clippy::should_implement_trait)]
2728
pub fn from_str(s: &str) -> Decimal128 {
2829
Decimal128 {
2930
inner: s.parse::<d128>().expect("Invalid Decimal128 string"),
@@ -68,10 +69,25 @@ impl Decimal128 {
6869
/// let int = dec128.into_i32();
6970
/// assert_eq!(int, num);
7071
/// ```
72+
#[deprecated(since = "0.15.0", note = "Replaced by `to_i32`")]
7173
pub fn into_i32(&self) -> i32 {
7274
Into::into(self.inner)
7375
}
7476

77+
/// Construct a `Decimal128` from a `i32` number.
78+
///
79+
/// ```rust
80+
/// use bson::decimal128::Decimal128;
81+
///
82+
/// let num: i32 = 23;
83+
/// let dec128 = Decimal128::from_i32(num);
84+
/// let int = dec128.to_i32();
85+
/// assert_eq!(int, num);
86+
/// ```
87+
pub fn to_i32(&self) -> i32 {
88+
Into::into(self.inner)
89+
}
90+
7591
/// Construct a `Decimal128` from a `i32` number.
7692
///
7793
/// ```rust
@@ -82,10 +98,25 @@ impl Decimal128 {
8298
/// let int = dec128.into_u32();
8399
/// assert_eq!(int, num);
84100
/// ```
101+
#[deprecated(since = "0.15.0", note = "Replaced by `to_u32`")]
85102
pub fn into_u32(&self) -> u32 {
86103
Into::into(self.inner)
87104
}
88105

106+
/// Construct a `Decimal128` from a `i32` number.
107+
///
108+
/// ```rust
109+
/// use bson::decimal128::Decimal128;
110+
///
111+
/// let num: u32 = 23;
112+
/// let dec128 = Decimal128::from_u32(num);
113+
/// let int = dec128.to_u32();
114+
/// assert_eq!(int, num);
115+
/// ```
116+
pub fn to_u32(&self) -> u32 {
117+
Into::into(self.inner)
118+
}
119+
89120
/// Create a new Decimal128 as `0`.
90121
///
91122
/// ```rust

src/decoder/serde.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ impl<'de> Visitor<'de> for BsonVisitor {
216216
V: MapAccess<'de>,
217217
{
218218
let values = OrderedDocumentVisitor::new().visit_map(visitor)?;
219-
Ok(Bson::from_extended_document(values.into()))
219+
Ok(Bson::from_extended_document(values))
220220
}
221221

222222
#[inline]
@@ -296,15 +296,15 @@ impl<'de> Deserializer<'de> for Decoder {
296296
let len = v.len();
297297
visitor.visit_seq(SeqDecoder {
298298
iter: v.into_iter(),
299-
len: len,
299+
len,
300300
})
301301
}
302302
Bson::Document(v) => {
303303
let len = v.len();
304304
visitor.visit_map(MapDecoder {
305305
iter: v.into_iter(),
306306
value: None,
307-
len: len,
307+
len,
308308
})
309309
}
310310
Bson::Boolean(v) => visitor.visit_bool(v),
@@ -318,7 +318,7 @@ impl<'de> Deserializer<'de> for Decoder {
318318
visitor.visit_map(MapDecoder {
319319
iter: doc.into_iter(),
320320
value: None,
321-
len: len,
321+
len,
322322
})
323323
}
324324
}
@@ -477,7 +477,7 @@ impl<'de> VariantAccess<'de> for VariantDecoder {
477477
};
478478
de.deserialize_any(visitor)
479479
} else {
480-
return Err(DecoderError::InvalidType("expected a tuple".to_owned()));
480+
Err(DecoderError::InvalidType("expected a tuple".to_owned()))
481481
}
482482
}
483483

@@ -497,7 +497,7 @@ impl<'de> VariantAccess<'de> for VariantDecoder {
497497
};
498498
de.deserialize_any(visitor)
499499
} else {
500-
return Err(DecoderError::InvalidType("expected a struct".to_owned()));
500+
Err(DecoderError::InvalidType("expected a struct".to_owned()))
501501
}
502502
}
503503
}

src/encoder/serde.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub struct Encoder;
7676

7777
impl Encoder {
7878
/// Construct a new `Serializer`.
79+
#[allow(clippy::new_without_default)]
7980
pub fn new() -> Encoder {
8081
Encoder
8182
}

src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,13 @@
3030
//! use bson::{decode_document, encode_document, Bson, Document};
3131
//! use std::io::Cursor;
3232
//!
33-
//! fn main() {
34-
//! let mut doc = Document::new();
35-
//! doc.insert("foo".to_owned(), Bson::String("bar".to_owned()));
33+
//! let mut doc = Document::new();
34+
//! doc.insert("foo".to_owned(), Bson::String("bar".to_owned()));
3635
//!
37-
//! let mut buf = Vec::new();
38-
//! encode_document(&mut buf, &doc).unwrap();
36+
//! let mut buf = Vec::new();
37+
//! encode_document(&mut buf, &doc).unwrap();
3938
//!
40-
//! let doc = decode_document(&mut Cursor::new(&buf[..])).unwrap();
41-
//! }
39+
//! let doc = decode_document(&mut Cursor::new(&buf[..])).unwrap();
4240
//! ```
4341
4442
#[cfg(feature = "decimal128")]

src/oid.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,12 @@ impl ObjectId {
101101
let counter = ObjectId::gen_count()?;
102102

103103
let mut buf: [u8; 12] = [0; 12];
104-
for i in 0..TIMESTAMP_SIZE {
105-
buf[TIMESTAMP_OFFSET + i] = timestamp[i];
106-
}
107-
for i in 0..PROCESS_ID_SIZE {
108-
buf[PROCESS_ID_OFFSET + i] = process_id[i];
109-
}
110-
for i in 0..COUNTER_SIZE {
111-
buf[COUNTER_OFFSET + i] = counter[i];
112-
}
104+
buf[TIMESTAMP_OFFSET..(TIMESTAMP_SIZE + TIMESTAMP_OFFSET)]
105+
.clone_from_slice(&timestamp[..TIMESTAMP_SIZE]);
106+
buf[PROCESS_ID_OFFSET..(PROCESS_ID_SIZE + PROCESS_ID_OFFSET)]
107+
.clone_from_slice(&process_id[..PROCESS_ID_SIZE]);
108+
buf[COUNTER_OFFSET..(COUNTER_SIZE + COUNTER_OFFSET)]
109+
.clone_from_slice(&counter[..COUNTER_SIZE]);
113110

114111
Ok(ObjectId::with_bytes(buf))
115112
}
@@ -155,9 +152,9 @@ impl ObjectId {
155152
/// Retrieves the increment counter from an ObjectId.
156153
pub fn counter(&self) -> u32 {
157154
let mut buf: [u8; 4] = [0; 4];
158-
for i in 0..COUNTER_SIZE {
159-
buf[i + 1] = self.id[COUNTER_OFFSET + i];
160-
}
155+
buf[1..=COUNTER_SIZE]
156+
.clone_from_slice(&self.id[COUNTER_OFFSET..(COUNTER_SIZE + COUNTER_OFFSET)]);
157+
161158
BigEndian::read_u32(&buf)
162159
}
163160

@@ -235,9 +232,7 @@ fn count_generated_is_big_endian() {
235232
let count_bytes = count_res.unwrap();
236233

237234
let mut buf: [u8; 4] = [0; 4];
238-
for i in 0..COUNTER_SIZE {
239-
buf[i + 1] = count_bytes[i];
240-
}
235+
buf[1..=COUNTER_SIZE].clone_from_slice(&count_bytes[..COUNTER_SIZE]);
241236

242237
let count = BigEndian::read_u32(&buf);
243238
assert_eq!(start as u32, count);

src/ordered.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,28 +108,30 @@ pub struct OrderedDocumentIterator<'a> {
108108
inner: linked_hash_map::Iter<'a, String, Bson>,
109109
}
110110

111+
type DocumentMap<'a, T> = Map<OrderedDocumentIterator<'a>, fn((&'a String, &'a Bson)) -> T>;
112+
111113
/// An iterator over an OrderedDocument's keys.
112114
pub struct Keys<'a> {
113-
inner: Map<OrderedDocumentIterator<'a>, fn((&'a String, &'a Bson)) -> &'a String>,
115+
inner: DocumentMap<'a, &'a String>,
114116
}
115117

116118
/// An iterator over an OrderedDocument's values.
117119
pub struct Values<'a> {
118-
inner: Map<OrderedDocumentIterator<'a>, fn((&'a String, &'a Bson)) -> &'a Bson>,
120+
inner: DocumentMap<'a, &'a Bson>,
119121
}
120122

121123
impl<'a> Iterator for Keys<'a> {
122124
type Item = &'a String;
123125

124-
fn next(&mut self) -> Option<(&'a String)> {
126+
fn next(&mut self) -> Option<&'a String> {
125127
self.inner.next()
126128
}
127129
}
128130

129131
impl<'a> Iterator for Values<'a> {
130132
type Item = &'a Bson;
131133

132-
fn next(&mut self) -> Option<(&'a Bson)> {
134+
fn next(&mut self) -> Option<&'a Bson> {
133135
self.inner.next()
134136
}
135137
}
@@ -189,7 +191,7 @@ impl OrderedDocument {
189191
}
190192

191193
/// Gets an iterator over the entries of the map.
192-
pub fn iter<'a>(&'a self) -> OrderedDocumentIterator<'a> {
194+
pub fn iter(&self) -> OrderedDocumentIterator {
193195
self.into_iter()
194196
}
195197

@@ -535,6 +537,7 @@ pub struct OrderedDocumentVisitor {
535537
}
536538

537539
impl OrderedDocumentVisitor {
540+
#[allow(clippy::new_without_default)]
538541
pub fn new() -> OrderedDocumentVisitor {
539542
OrderedDocumentVisitor {
540543
marker: PhantomData,

tests/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
#![allow(clippy::cognitive_complexity)]
2+
13
mod modules;

tests/modules/bson.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn to_json() {
1111
doc.insert("first", Bson::I32(1));
1212
doc.insert("second", Bson::String("foo".to_owned()));
1313
doc.insert("alphanumeric", Bson::String("bar".to_owned()));
14-
let data: Value = Bson::Document(doc).clone().into();
14+
let data: Value = Bson::Document(doc).into();
1515

1616
assert!(data.is_object());
1717
let obj = data.as_object().unwrap();

0 commit comments

Comments
 (0)