Skip to content

Commit 7f53408

Browse files
authored
Clippy warnings (#1)
* clean pedantic warnings
1 parent a5dbe78 commit 7f53408

File tree

7 files changed

+124
-58
lines changed

7 files changed

+124
-58
lines changed

src/array.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct Header {
2222
impl Header {
2323
fn as_ptr(&self) -> *const IValue {
2424
// Safety: pointers to the end of structs are allowed
25-
unsafe { (self as *const Header).add(1) as *const IValue }
25+
unsafe { (self as *const Header).add(1).cast::<IValue>() }
2626
}
2727
fn as_slice(&self) -> &[IValue] {
2828
// Safety: Header `len` must be accurate
@@ -88,7 +88,7 @@ impl Iterator for IntoIter {
8888
.read();
8989
self.index += 1;
9090
if self.index >= len {
91-
IArray::dealloc(self.header as *mut u8);
91+
IArray::dealloc(self.header.cast::<u8>());
9292
self.header = std::ptr::null_mut();
9393
}
9494
Some(res)
@@ -143,19 +143,19 @@ impl IArray {
143143

144144
fn alloc(cap: usize) -> *mut u8 {
145145
unsafe {
146-
let ptr = alloc(Self::layout(cap).unwrap()) as *mut Header;
146+
let ptr = alloc(Self::layout(cap).unwrap()).cast::<Header>();
147147
(*ptr).len = 0;
148148
(*ptr).cap = cap;
149-
ptr as *mut u8
149+
ptr.cast::<u8>()
150150
}
151151
}
152152

153153
fn realloc(ptr: *mut u8, new_cap: usize) -> *mut u8 {
154154
unsafe {
155155
let old_layout = Self::layout((*(ptr as *const Header)).cap).unwrap();
156156
let new_layout = Self::layout(new_cap).unwrap();
157-
let ptr = realloc(ptr as *mut u8, old_layout, new_layout.size());
158-
(*(ptr as *mut Header)).cap = new_cap;
157+
let ptr = realloc(ptr.cast::<u8>(), old_layout, new_layout.size());
158+
(*(ptr.cast::<Header>())).cap = new_cap;
159159
ptr
160160
}
161161
}
@@ -168,12 +168,14 @@ impl IArray {
168168
}
169169

170170
/// Constructs a new empty `IArray`. Does not allocate.
171+
#[must_use]
171172
pub fn new() -> Self {
172173
unsafe { IArray(IValue::new_ref(&EMPTY_HEADER, TypeTag::ArrayOrFalse)) }
173174
}
174175

175176
/// Constructs a new `IArray` with the specified capacity. At least that many items
176177
/// can be added to the array without reallocating.
178+
#[must_use]
177179
pub fn with_capacity(cap: usize) -> Self {
178180
if cap == 0 {
179181
Self::new()
@@ -188,29 +190,33 @@ impl IArray {
188190

189191
// Safety: must not be static
190192
unsafe fn header_mut(&mut self) -> &mut Header {
191-
&mut *(self.0.ptr() as *mut Header)
193+
&mut *(self.0.ptr().cast::<Header>())
192194
}
193195

194196
fn is_static(&self) -> bool {
195197
self.capacity() == 0
196198
}
197199
/// Returns the capacity of the array. This is the maximum number of items the array
198200
/// can hold without reallocating.
201+
#[must_use]
199202
pub fn capacity(&self) -> usize {
200203
self.header().cap
201204
}
202205

203206
/// Returns the number of items currently stored in the array.
207+
#[must_use]
204208
pub fn len(&self) -> usize {
205209
self.header().len
206210
}
207211

208212
/// Returns `true` if the array is empty.
213+
#[must_use]
209214
pub fn is_empty(&self) -> bool {
210215
self.len() == 0
211216
}
212217

213218
/// Borrows a slice of [`IValue`]s from the array
219+
#[must_use]
214220
pub fn as_slice(&self) -> &[IValue] {
215221
self.header().as_slice()
216222
}

src/de.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ impl<'de> VariantAccess<'de> for VariantDeserializer<'de> {
773773
where
774774
V: Visitor<'de>,
775775
{
776-
match self.value.map(|v| v.destructure_ref()) {
776+
match self.value.map(IValue::destructure_ref) {
777777
Some(DestructuredRef::Array(v)) => v.deserialize_any(visitor),
778778
Some(other) => Err(SError::invalid_type(other.unexpected(), &"tuple variant")),
779779
None => Err(SError::invalid_type(
@@ -791,7 +791,7 @@ impl<'de> VariantAccess<'de> for VariantDeserializer<'de> {
791791
where
792792
V: Visitor<'de>,
793793
{
794-
match self.value.map(|v| v.destructure_ref()) {
794+
match self.value.map(IValue::destructure_ref) {
795795
Some(DestructuredRef::Object(v)) => v.deserialize_any(visitor),
796796
Some(other) => Err(SError::invalid_type(other.unexpected(), &"struct variant")),
797797
None => Err(SError::invalid_type(

src/number.rs

+42-29
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn cmp_u64_to_f64(a: u64, b: f64) -> Ordering {
4747
if can_represent_as_f64(a) {
4848
// If we can represent as an f64, we can just cast and compare
4949
(a as f64).partial_cmp(&b).unwrap()
50-
} else if b <= (0x20000000000000u64 as f64) {
50+
} else if b <= (0x20000000000000_u64 as f64) {
5151
// If the floating point number is less than all non-representable
5252
// integers, and our integer is non-representable, then we know
5353
// the integer is greater.
@@ -64,7 +64,7 @@ fn cmp_u64_to_f64(a: u64, b: f64) -> Ordering {
6464

6565
impl Header {
6666
fn as_i24_unchecked(&self) -> i32 {
67-
((self.static_ as i32) << 8) | (self.short as i32)
67+
(i32::from(self.static_) << 8) | i32::from(self.short)
6868
}
6969
unsafe fn as_i64_unchecked(&self) -> &i64 {
7070
&*(self as *const _ as *const i64).add(1)
@@ -88,12 +88,12 @@ impl Header {
8888
// Safety: We only call methods appropriate for the type
8989
unsafe {
9090
match self.type_ {
91-
NumberType::Static => Some(self.static_ as i64),
92-
NumberType::I24 => Some(self.as_i24_unchecked() as i64),
91+
NumberType::Static => Some(i64::from(self.static_)),
92+
NumberType::I24 => Some(i64::from(self.as_i24_unchecked())),
9393
NumberType::I64 => Some(*self.as_i64_unchecked()),
9494
NumberType::U64 => {
9595
let v = *self.as_u64_unchecked();
96-
if v <= i64::MAX as u64 {
96+
if i64::try_from(v).is_ok() {
9797
Some(v as i64)
9898
} else {
9999
None
@@ -153,8 +153,8 @@ impl Header {
153153
// Safety: We only call methods appropriate for the type
154154
unsafe {
155155
match self.type_ {
156-
NumberType::Static => Some(self.static_ as f64),
157-
NumberType::I24 => Some(self.as_i24_unchecked() as f64),
156+
NumberType::Static => Some(f64::from(self.static_)),
157+
NumberType::I24 => Some(f64::from(self.as_i24_unchecked())),
158158
NumberType::I64 => {
159159
let v = *self.as_i64_unchecked();
160160
let can_represent = if v < 0 {
@@ -184,7 +184,7 @@ impl Header {
184184
// Safety: We only call methods appropriate for the type
185185
unsafe {
186186
match self.type_ {
187-
NumberType::Static => Some(self.static_ as f32),
187+
NumberType::Static => Some(f32::from(self.static_)),
188188
NumberType::I24 => Some(self.as_i24_unchecked() as f32),
189189
NumberType::I64 => {
190190
let v = *self.as_i64_unchecked();
@@ -210,7 +210,7 @@ impl Header {
210210
NumberType::F64 => {
211211
let v = *self.as_f64_unchecked();
212212
let u = v as f32;
213-
if v == (u as f64) {
213+
if v == f64::from(u) {
214214
Some(u)
215215
} else {
216216
None
@@ -228,8 +228,8 @@ impl Header {
228228
fn to_f64_lossy(&self) -> f64 {
229229
unsafe {
230230
match self.type_ {
231-
NumberType::Static => self.static_ as f64,
232-
NumberType::I24 => self.as_i24_unchecked() as f64,
231+
NumberType::Static => f64::from(self.static_),
232+
NumberType::I24 => f64::from(self.as_i24_unchecked()),
233233
NumberType::I64 => *self.as_i64_unchecked() as f64,
234234
NumberType::U64 => *self.as_u64_unchecked() as f64,
235235
NumberType::F64 => *self.as_f64_unchecked(),
@@ -361,7 +361,7 @@ impl INumber {
361361

362362
fn alloc(type_: NumberType) -> *mut Header {
363363
unsafe {
364-
let ptr = alloc(Self::layout(type_).unwrap()) as *mut Header;
364+
let ptr = alloc(Self::layout(type_).unwrap()).cast::<Header>();
365365
(*ptr).type_ = type_;
366366
(*ptr).static_ = 0;
367367
(*ptr).short = 0;
@@ -372,16 +372,18 @@ impl INumber {
372372
fn dealloc(ptr: *mut Header) {
373373
unsafe {
374374
let layout = Self::layout((*ptr).type_).unwrap();
375-
dealloc(ptr as *mut u8, layout);
375+
dealloc(ptr.cast::<u8>(), layout);
376376
}
377377
}
378378

379379
/// Returns the number zero (without a decimal point). Does not allocate.
380+
#[must_use]
380381
pub fn zero() -> Self {
381382
// Safety: 0 is in the static range
382383
unsafe { Self::new_static(0) }
383384
}
384385
/// Returns the number one (without a decimal point). Does not allocate.
386+
#[must_use]
385387
pub fn one() -> Self {
386388
// Safety: 1 is in the static range
387389
unsafe { Self::new_static(1) }
@@ -396,7 +398,7 @@ impl INumber {
396398
fn new_ptr(type_: NumberType) -> Self {
397399
unsafe {
398400
INumber(IValue::new_ptr(
399-
Self::alloc(type_) as *mut u8,
401+
Self::alloc(type_).cast::<u8>(),
400402
TypeTag::Number,
401403
))
402404
}
@@ -415,7 +417,7 @@ impl INumber {
415417

416418
// Value must fit in an i24
417419
fn new_short(value: i32) -> Self {
418-
if value >= STATIC_LOWER as i32 && value < STATIC_UPPER as i32 {
420+
if value >= i32::from(STATIC_LOWER) && value < i32::from(STATIC_UPPER) {
419421
// Safety: We checked the value is in the static range
420422
unsafe { Self::new_static(value as i16) }
421423
} else {
@@ -443,7 +445,7 @@ impl INumber {
443445
}
444446

445447
fn new_u64(value: u64) -> Self {
446-
if value <= i64::MAX as u64 {
448+
if i64::try_from(value).is_ok() {
447449
Self::new_i64(value as i64)
448450
} else {
449451
let mut res = Self::new_ptr(NumberType::U64);
@@ -487,48 +489,59 @@ impl INumber {
487489
}
488490

489491
/// Converts this number to an i64 if it can be represented exactly.
492+
#[must_use]
490493
pub fn to_i64(&self) -> Option<i64> {
491494
self.header().to_i64()
492495
}
493496
/// Converts this number to an f64 if it can be represented exactly.
497+
#[must_use]
494498
pub fn to_u64(&self) -> Option<u64> {
495499
self.header().to_u64()
496500
}
497501
/// Converts this number to an f64 if it can be represented exactly.
502+
#[must_use]
498503
pub fn to_f64(&self) -> Option<f64> {
499504
self.header().to_f64()
500505
}
501506
/// Converts this number to an f32 if it can be represented exactly.
507+
#[must_use]
502508
pub fn to_f32(&self) -> Option<f32> {
503509
self.header().to_f32()
504510
}
505511
/// Converts this number to an i32 if it can be represented exactly.
512+
#[must_use]
506513
pub fn to_i32(&self) -> Option<i32> {
507514
self.header().to_i64().and_then(|x| x.try_into().ok())
508515
}
509516
/// Converts this number to a u32 if it can be represented exactly.
517+
#[must_use]
510518
pub fn to_u32(&self) -> Option<u32> {
511519
self.header().to_u64().and_then(|x| x.try_into().ok())
512520
}
513521
/// Converts this number to an isize if it can be represented exactly.
522+
#[must_use]
514523
pub fn to_isize(&self) -> Option<isize> {
515524
self.header().to_i64().and_then(|x| x.try_into().ok())
516525
}
517526
/// Converts this number to a usize if it can be represented exactly.
527+
#[must_use]
518528
pub fn to_usize(&self) -> Option<usize> {
519529
self.header().to_u64().and_then(|x| x.try_into().ok())
520530
}
521531
/// Converts this number to an f64, potentially losing precision in the process.
532+
#[must_use]
522533
pub fn to_f64_lossy(&self) -> f64 {
523534
self.header().to_f64_lossy()
524535
}
525536
/// Converts this number to an f32, potentially losing precision in the process.
537+
#[must_use]
526538
pub fn to_f32_lossy(&self) -> f32 {
527539
self.to_f64_lossy() as f32
528540
}
529541

530542
/// This allows distinguishing between `1.0` and `1` in the original JSON.
531543
/// Numeric operations will otherwise treat these two values as equivalent.
544+
#[must_use]
532545
pub fn has_decimal_point(&self) -> bool {
533546
self.header().has_decimal_point()
534547
}
@@ -559,18 +572,18 @@ impl From<u64> for INumber {
559572
}
560573
impl From<u32> for INumber {
561574
fn from(v: u32) -> Self {
562-
Self::new_u64(v as u64)
575+
Self::new_u64(u64::from(v))
563576
}
564577
}
565578
impl From<u16> for INumber {
566579
fn from(v: u16) -> Self {
567-
Self::new_short(v as i32)
580+
Self::new_short(i32::from(v))
568581
}
569582
}
570583
impl From<u8> for INumber {
571584
fn from(v: u8) -> Self {
572585
// Safety: All u8s are in the static range
573-
unsafe { Self::new_static(v as i16) }
586+
unsafe { Self::new_static(i16::from(v)) }
574587
}
575588
}
576589
impl From<usize> for INumber {
@@ -586,18 +599,18 @@ impl From<i64> for INumber {
586599
}
587600
impl From<i32> for INumber {
588601
fn from(v: i32) -> Self {
589-
Self::new_i64(v as i64)
602+
Self::new_i64(i64::from(v))
590603
}
591604
}
592605
impl From<i16> for INumber {
593606
fn from(v: i16) -> Self {
594-
Self::new_short(v as i32)
607+
Self::new_short(i32::from(v))
595608
}
596609
}
597610
impl From<i8> for INumber {
598611
fn from(v: i8) -> Self {
599612
// Safety: All i8s are in the static range
600-
unsafe { Self::new_static(v as i16) }
613+
unsafe { Self::new_static(i16::from(v)) }
601614
}
602615
}
603616
impl From<isize> for INumber {
@@ -621,7 +634,7 @@ impl TryFrom<f32> for INumber {
621634
type Error = ();
622635
fn try_from(v: f32) -> Result<Self, ()> {
623636
if v.is_finite() {
624-
Ok(Self::new_f64(v as f64))
637+
Ok(Self::new_f64(f64::from(v)))
625638
} else {
626639
Err(())
627640
}
@@ -702,12 +715,12 @@ mod tests {
702715
let x: INumber = 0x1000000.into();
703716
assert_eq!(x.to_i64(), Some(0x1000000));
704717
assert_eq!(x.to_u64(), Some(0x1000000));
705-
assert_eq!(x.to_f64(), Some(16777216.0));
718+
assert_eq!(x.to_f64(), Some(16_777_216.0));
706719

707720
let x: INumber = i64::MIN.into();
708721
assert_eq!(x.to_i64(), Some(i64::MIN));
709722
assert_eq!(x.to_u64(), None);
710-
assert_eq!(x.to_f64(), Some(-9223372036854775808.0));
723+
assert_eq!(x.to_f64(), Some(-9_223_372_036_854_775_808.0));
711724

712725
let x: INumber = i64::MAX.into();
713726
assert_eq!(x.to_i64(), Some(i64::MAX));
@@ -720,9 +733,9 @@ mod tests {
720733
assert_eq!(x.to_f64(), None);
721734

722735
let x: INumber = 13369629.into();
723-
assert_eq!(x.to_i64(), Some(13369629));
724-
assert_eq!(x.to_u64(), Some(13369629));
725-
assert_eq!(x.to_f64(), Some(13369629.0));
736+
assert_eq!(x.to_i64(), Some(13_369_629));
737+
assert_eq!(x.to_u64(), Some(13_369_629));
738+
assert_eq!(x.to_f64(), Some(13_369_629.0));
726739

727740
let x: INumber = 0x800000.into();
728741
assert_eq!(x.to_i64(), Some(0x800000));
@@ -753,6 +766,6 @@ mod tests {
753766
assert!(INumber::try_from(1e30).unwrap() > INumber::from(i64::MAX));
754767
assert!(INumber::try_from(-1e30).unwrap() < INumber::from(i64::MIN));
755768
assert!(INumber::try_from(-1e30).unwrap() < INumber::from(i64::MIN));
756-
assert!(INumber::try_from(99999999000.0).unwrap() < INumber::from(99999999001u64));
769+
assert!(INumber::try_from(99_999_999_000.0).unwrap() < INumber::from(99_999_999_001_u64));
757770
}
758771
}

0 commit comments

Comments
 (0)