Skip to content

Commit 6d5ef30

Browse files
committed
Make clippy happy
Signed-off-by: Heinz N. Gies <[email protected]>
1 parent 871c997 commit 6d5ef30

File tree

15 files changed

+111
-55
lines changed

15 files changed

+111
-55
lines changed

simd-json-derive-int/src/args.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl Parse for FieldAttrs {
3838
other => {
3939
return Err(syn::Error::new(
4040
attr.span(),
41-
format!("unexpected attribute `{}`", other),
41+
format!("unexpected attribute `{other}`"),
4242
))
4343
}
4444
}
@@ -114,7 +114,7 @@ impl Parse for StructAttrs {
114114
other => {
115115
return Err(syn::Error::new(
116116
attr.span(),
117-
format!("unexpected rename_all type `{}`", other),
117+
format!("unexpected rename_all type `{other}`"),
118118
))
119119
}
120120
}
@@ -125,7 +125,7 @@ impl Parse for StructAttrs {
125125
other => {
126126
return Err(syn::Error::new(
127127
attr.span(),
128-
format!("unexpected field attribute `{}`", other),
128+
format!("unexpected field attribute `{other}`",),
129129
))
130130
}
131131
}

simd-json-derive-int/src/serialize/enum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub(crate) fn derive(
8989
(
9090
&v.ident,
9191
(0..v.fields.len())
92-
.map(|i| Ident::new(&format!("v{}", i), Span::call_site()))
92+
.map(|i| Ident::new(&format!("v{i}"), Span::call_site()))
9393
.collect::<Vec<_>>(),
9494
),
9595
format!(

simd-json-derive-int/src/serialize/struct/named.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ pub(crate) fn derive(
2525
}
2626
let expanded = if skip_if.iter().all(Option::is_none) {
2727
if let Some((first, rest)) = keys.split_first_mut() {
28-
*first = format!("{{{}", first);
28+
*first = format!("{{{first}");
2929
for r in rest {
30-
*r = format!(",{}", r);
30+
*r = format!(",{r}");
3131
}
3232
};
3333

src/de.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,33 @@ pub enum Error {
3131
expected(possible_field_names)
3232
)]
3333
UnknownField {
34+
/// Unknown field that was encountered
3435
unknown_field: String,
36+
/// Possible fields that are expected
3537
possible_field_names: &'static [&'static str],
3638
},
39+
/// unnamed enum field is not an array
3740
#[error("unnamed enum field `{0}` is not an array")]
3841
FieldNotAnArray(&'static str),
42+
/// unknwon enum variant
3943
#[error("unknwon enum variant `{0}`")]
4044
UnknownEnumVariant(String),
45+
/// invalid enum representation, needs to be either a string or an object
4146
#[error("invalid enum representation, needs to be either a string or an object")]
4247
InvalidEnumRepresentation,
48+
/// invalid struct representation, needs to be an object
4349
#[error("invalid struct representation, needs to be an object")]
4450
InvalidStructRepresentation,
51+
/// Unexpected e,nd of input
4552
#[error("Unexpected e,nd of input")]
4653
EOF,
54+
/// Invalid integer number
4755
#[error("Invalid integer number")]
4856
InvalidNumber(#[from] TryFromIntError),
57+
/// Custom error
4958
#[error("Custom error: {0}")]
5059
Custom(String),
60+
/// The universe is broken
5161
#[error("The universe is broken: {0}")]
5262
BrokenUniverse(#[from] std::convert::Infallible),
5363
}
@@ -58,43 +68,58 @@ impl Error {
5868
Error::Custom(msg.to_string())
5969
}
6070
/// Expected String error
71+
#[must_use]
6172
pub const fn expected_string() -> Self {
6273
Error::Json(simd_json::ErrorType::ExpectedString)
6374
}
6475
/// Expected Map error
76+
#[must_use]
6577
pub const fn expected_map() -> Self {
6678
Error::Json(simd_json::ErrorType::ExpectedMap)
6779
}
6880
/// Expected Array error
81+
#[must_use]
6982
pub const fn expected_array() -> Self {
7083
Error::Json(simd_json::ErrorType::ExpectedArray)
7184
}
7285
/// Expected Float error
86+
#[must_use]
7387
pub const fn expected_float() -> Self {
7488
Error::Json(simd_json::ErrorType::ExpectedFloat)
7589
}
7690
/// Expected Null error
91+
#[must_use]
7792
pub fn expected_null() -> Self {
7893
Error::Json(simd_json::ErrorType::ExpectedNull)
7994
}
8095
/// Expected Integer error
96+
#[must_use]
8197
pub fn expected_integer() -> Self {
8298
Error::Json(simd_json::ErrorType::ExpectedInteger)
8399
}
84100
/// Expected Boolean error
101+
#[must_use]
85102
pub fn expected_boolean() -> Self {
86103
Error::Json(simd_json::ErrorType::ExpectedBoolean)
87104
}
88105
}
89106

90107
// Deserialisation result
108+
/// Deserializer result
91109
pub type Result<T> = std::result::Result<T, Error>;
92110

111+
/// Deserialisation trait for simd-json
93112
pub trait Deserialize<'input> {
113+
/// Deserializes from a tape
114+
/// # Errors
115+
/// if deserialisation fails
94116
fn from_tape(tape: &mut Tape<'input>) -> Result<Self>
95117
where
96118
Self: Sized + 'input;
97119

120+
/// Deserializes from a u8 slice
121+
/// # Errors
122+
/// if deserialisation fails
98123
#[inline]
99124
fn from_slice(json: &'input mut [u8]) -> Result<Self>
100125
where
@@ -105,6 +130,9 @@ pub trait Deserialize<'input> {
105130
Self::from_tape(&mut itr)
106131
}
107132

133+
/// Deserializes from a u8 slice using pre-allocated buffers
134+
/// # Errors
135+
/// if deserialisation fails
108136
#[inline]
109137
fn from_slice_with_buffers(json: &'input mut [u8], buffers: &mut Buffers) -> Result<Self>
110138
where
@@ -116,6 +144,8 @@ pub trait Deserialize<'input> {
116144
}
117145

118146
#[inline]
147+
/// # Errors
148+
/// if deserialisation fails
119149
/// # Safety
120150
///
121151
/// user must not use the string afterwards

src/impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod primitives;
77
mod simdjson;
88
mod string;
99
mod tpl;
10-
use crate::{de, *};
10+
use crate::{de, io, Deserialize, DummyGenerator, Serialize, Tape, Write};
1111
use value_trait::generator::BaseGenerator;
1212

1313
impl<T> Serialize for Option<T>

src/impls/array.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::*;
1+
use crate::{de, io, Deserialize, Node, Serialize, Tape, Write};
22
use std::mem::MaybeUninit;
33
use std::ptr;
44

@@ -27,8 +27,10 @@ impl<T, const N: usize> Drop for Guard<'_, T, N> {
2727

2828
// SAFETY: this slice will contain only initialized objects.
2929
unsafe {
30-
let slice =
31-
ptr::slice_from_raw_parts_mut(self.array.as_mut_ptr() as *mut T, self.initialized);
30+
let slice = ptr::slice_from_raw_parts_mut(
31+
self.array.as_mut_ptr().cast::<T>(),
32+
self.initialized,
33+
);
3234
ptr::drop_in_place(slice);
3335
}
3436
}
@@ -52,6 +54,7 @@ where
5254

5355
if N == 0 {
5456
// Safety: N is 0, and so *const [T; N] is *const [T; 0]
57+
#[allow(clippy::ref_as_ptr)]
5558
return Ok(unsafe { ptr::read((&[]) as *const [T; N]) });
5659
}
5760

@@ -116,10 +119,10 @@ mod test {
116119
#[test]
117120
fn arr() {
118121
let s: [u8; 0] = [];
119-
assert_eq!(s.json_string().unwrap(), "[]");
120-
assert_eq!([1].json_string().unwrap(), "[1]");
121-
assert_eq!([1, 2].json_string().unwrap(), "[1,2]");
122-
assert_eq!([1, 2, 3].json_string().unwrap(), "[1,2,3]");
122+
assert_eq!(s.json_string().expect("invalid "), "[]");
123+
assert_eq!([1].json_string().expect("invalid "), "[1]");
124+
assert_eq!([1, 2].json_string().expect("invalid "), "[1,2]");
125+
assert_eq!([1, 2, 3].json_string().expect("invalid "), "[1,2,3]");
123126
}
124127
#[test]
125128
fn arr2() {
@@ -143,9 +146,9 @@ mod test {
143146
#[test]
144147
fn slice() {
145148
let s: [u8; 0] = [];
146-
assert_eq!(s.json_string().unwrap(), "[]");
147-
assert_eq!([1].json_string().unwrap(), "[1]");
148-
assert_eq!([1, 2].json_string().unwrap(), "[1,2]");
149-
assert_eq!([1, 2, 3].json_string().unwrap(), "[1,2,3]");
149+
assert_eq!(s.json_string().expect("invalid data"), "[]");
150+
assert_eq!([1].json_string().expect("invalid data"), "[1]");
151+
assert_eq!([1, 2].json_string().expect("invalid data"), "[1,2]");
152+
assert_eq!([1, 2, 3].json_string().expect("invalid data"), "[1,2,3]");
150153
}
151154
}

src/impls/chrono.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{BaseGenerator, DummyGenerator};
2-
use crate::*;
2+
use crate::{de, Deserialize, Serialize, Tape, Write};
33
use chrono::{DateTime, FixedOffset, TimeZone};
44
use std::{fmt, io};
55

src/impls/collections.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ where
7474
if let Some(simd_json::Node::Array { len, .. }) = tape.next() {
7575
let mut v = collections::VecDeque::new();
7676
for _ in 0..len {
77-
v.push_back(T::from_tape(tape)?)
77+
v.push_back(T::from_tape(tape)?);
7878
}
7979
Ok(v)
8080
} else {
@@ -95,7 +95,7 @@ where
9595
if let Some(simd_json::Node::Array { len, .. }) = tape.next() {
9696
let mut v = collections::BinaryHeap::new();
9797
for _ in 0..len {
98-
v.push(T::from_tape(tape)?)
98+
v.push(T::from_tape(tape)?);
9999
}
100100
Ok(v)
101101
} else {
@@ -349,33 +349,33 @@ mod test {
349349
#[test]
350350
fn vec() {
351351
let mut v: Vec<u8> = Vec::new();
352-
assert_eq!(v.json_string().unwrap(), "[]");
352+
assert_eq!(v.json_string().expect("invalid data"), "[]");
353353

354354
v.push(1);
355-
let mut s = v.json_string().unwrap();
355+
let mut s = v.json_string().expect("invalid data");
356356
assert_eq!(s, "[1]");
357-
let s: Vec<u8> = unsafe { Vec::from_str(s.as_mut_str()) }.unwrap();
357+
let s: Vec<u8> = unsafe { Vec::from_str(s.as_mut_str()) }.expect("invalid data");
358358
assert_eq!(s, v);
359359

360360
v.push(2);
361-
let mut s = v.json_string().unwrap();
361+
let mut s = v.json_string().expect("invalid test data");
362362
assert_eq!(s, "[1,2]");
363-
let s: Vec<u8> = unsafe { Vec::from_str(s.as_mut_str()) }.unwrap();
363+
let s: Vec<u8> = unsafe { Vec::from_str(s.as_mut_str()) }.expect("invalid test data");
364364
assert_eq!(s, v);
365365

366366
v.push(3);
367-
let mut s = v.json_string().unwrap();
367+
let mut s = v.json_string().expect("invalid test data");
368368
assert_eq!(s, "[1,2,3]");
369-
let s: Vec<u8> = unsafe { Vec::from_str(s.as_mut_str()) }.unwrap();
369+
let s: Vec<u8> = unsafe { Vec::from_str(s.as_mut_str()) }.expect("invalid test data");
370370
assert_eq!(s, v);
371371
}
372372

373373
#[test]
374374
fn range() {
375375
let r = 1..42;
376-
let mut v = r.json_vec().unwrap();
376+
let mut v = r.json_vec().expect("invalid test data");
377377
assert_eq!(br#"{"start":1,"end":42}"#, v.as_slice());
378-
let r1 = Range::from_slice(v.as_mut_slice()).unwrap();
378+
let r1 = Range::from_slice(v.as_mut_slice()).expect("invalid test data");
379379
assert_eq!(r, r1);
380380
}
381381
}

src/impls/primitives.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use std::convert::TryFrom;
44
impl Serialize for bool {
55
#[inline]
66
fn json_write<W: Write>(&self, writer: &mut W) -> Result {
7-
match *self {
8-
true => writer.write_all(b"true"),
9-
false => writer.write_all(b"false"),
7+
if *self {
8+
writer.write_all(b"true")
9+
} else {
10+
writer.write_all(b"false")
1011
}
1112
}
1213
}
@@ -96,6 +97,7 @@ ryu!(f32);
9697

9798
impl<'input> Deserialize<'input> for f64 {
9899
#[inline]
100+
#[allow(clippy::cast_precision_loss)]
99101
fn from_tape(tape: &mut Tape<'input>) -> de::Result<Self>
100102
where
101103
Self: Sized + 'input,
@@ -115,6 +117,7 @@ impl<'input> Deserialize<'input> for f64 {
115117

116118
impl<'input> Deserialize<'input> for f32 {
117119
#[inline]
120+
#[allow(clippy::cast_precision_loss, clippy::cast_possible_truncation)]
118121
fn from_tape(tape: &mut Tape<'input>) -> de::Result<Self>
119122
where
120123
Self: Sized + 'input,

src/impls/simdjson.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ impl Serialize for BorrowedValue<'_> {
2222
struct OwnedDeser<'input, 'tape>(&'tape mut crate::Tape<'input>);
2323

2424
impl OwnedDeser<'_, '_> {
25-
#[inline(always)]
2625
fn parse(&mut self) -> OwnedValue {
2726
match self.0.next() {
2827
Some(Node::Static(s)) => OwnedValue::Static(s),
@@ -32,19 +31,17 @@ impl OwnedDeser<'_, '_> {
3231
None => unreachable!("We have validated the tape in the second stage of parsing, this should never happen"),
3332
}
3433
}
35-
#[inline(always)]
3634
fn parse_array(&mut self, len: usize) -> OwnedValue {
3735
let mut res: Vec<OwnedValue> = Vec::with_capacity(len);
3836
// Rust doesn't optimize the normal loop away here
3937
// so we write our own avoiding the length
4038
// checks during push
4139
for _ in 0..len {
42-
res.push(self.parse())
40+
res.push(self.parse());
4341
}
4442
OwnedValue::Array(Box::new(res))
4543
}
4644

47-
#[inline(always)]
4845
fn parse_map(&mut self, len: usize) -> OwnedValue {
4946
let mut res = OwnedValue::object_with_capacity(len);
5047

@@ -76,7 +73,6 @@ impl<'input> Deserialize<'input> for OwnedValue {
7673
struct BorrowedDeser<'input, 'tape>(&'tape mut crate::Tape<'input>);
7774

7875
impl<'input> BorrowedDeser<'input, '_> {
79-
#[inline(always)]
8076
fn parse(&mut self) -> BorrowedValue<'input> {
8177
match self.0.next() {
8278
Some(Node::Static(s)) => BorrowedValue::Static(s),
@@ -86,7 +82,6 @@ impl<'input> BorrowedDeser<'input, '_> {
8682
None => unreachable!("We have validated the tape in the second stage of parsing, this should never happen"),
8783
}
8884
}
89-
#[inline(always)]
9085
fn parse_array(&mut self, len: usize) -> BorrowedValue<'input> {
9186
let mut res: Vec<BorrowedValue<'input>> = Vec::with_capacity(len);
9287
for _ in 0..len {
@@ -95,7 +90,6 @@ impl<'input> BorrowedDeser<'input, '_> {
9590
BorrowedValue::Array(Box::new(res))
9691
}
9792

98-
#[inline(always)]
9993
fn parse_map(&mut self, len: usize) -> BorrowedValue<'input> {
10094
let mut res = BorrowedValue::object_with_capacity(len);
10195

0 commit comments

Comments
 (0)