Skip to content

Commit 8737093

Browse files
committed
Fix Clippy lints
1 parent 1e67565 commit 8737093

File tree

7 files changed

+70
-33
lines changed

7 files changed

+70
-33
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
//! [`Error`]: https://doc.rust-lang.org/stable/std/error/trait.Error.html
9797
//! [`ParseError`]: https://docs.rs/bounded-integer/*/bounded_integer/struct.ParseError.html
9898
#![warn(clippy::pedantic, rust_2018_idioms, unused_qualifications)]
99+
#![allow(clippy::items_after_statements, clippy::missing_errors_doc)]
99100
#![cfg_attr(feature = "step_trait", feature(step_trait))]
100101
#![cfg_attr(feature = "__doc", feature(doc_auto_cfg))]
101102
#![no_std]

src/macro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ mod tests {
406406
use crate::bounded_integer;
407407

408408
bounded_integer! {
409-
#[cfg_attr(all(), doc = "")]
409+
#[cfg_attr(all(), doc = "")]
410410
#[deprecated]
411411
#[must_use]
412412
struct S(0, 255);

src/parse.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ macro_rules! from_str_radix_impl {
4343
yeet!(ParseErrorKind::InvalidDigit);
4444
};
4545

46+
#[allow(clippy::cast_possible_wrap)]
47+
#[allow(clippy::cast_possible_truncation)]
4648
let Some(new_result) = result.checked_mul(radix as $ty) else {
4749
yeet!(overflow_kind);
4850
};
4951

52+
#[allow(clippy::cast_possible_wrap)]
53+
#[allow(clippy::cast_possible_truncation)]
5054
let Some(new_result) = (if positive {
5155
new_result.checked_add(digit_value as $ty)
5256
} else {
@@ -119,11 +123,13 @@ pub enum ParseErrorKind {
119123
BelowMin,
120124
}
121125

126+
#[must_use]
122127
pub const fn error_below_min() -> ParseError {
123128
ParseError {
124129
kind: ParseErrorKind::BelowMin,
125130
}
126131
}
132+
#[must_use]
127133
pub const fn error_above_max() -> ParseError {
128134
ParseError {
129135
kind: ParseErrorKind::AboveMax,

src/prim_int.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![expect(clippy::must_use_candidate)]
2+
13
use core::num::NonZero;
24

35
pub trait PrimInt {
@@ -48,6 +50,8 @@ macro_rules! generate {
4850
}
4951
pub const fn rem_euclid_unsigned(lhs: $signed, rhs: NonZero<$unsigned>) -> $unsigned {
5052
// In my benchmarks, this is faster than methods involving widening.
53+
#[expect(clippy::cast_possible_wrap)]
54+
#[expect(clippy::cast_sign_loss)]
5155
if 0 <= lhs {
5256
// If `lhs` is nonnegative, just use regular unsigned remainder.
5357
(lhs as $unsigned).rem_euclid(rhs.get())

src/types.rs

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ mod tests {
7070
}
7171

7272
#[test]
73-
fn saturating() {
73+
fn new_saturating() {
7474
type Bounded = BoundedI8<3, 10>;
7575
assert_eq!(Bounded::new_saturating(i8::MIN), Bounded::MIN);
7676
assert_eq!(Bounded::new_saturating(i8::MAX), Bounded::MAX);
@@ -128,8 +128,8 @@ mod tests {
128128

129129
#[test]
130130
fn arithmetic() {
131+
#![expect(clippy::modulo_one)]
131132
type Bounded = BoundedI8<-5, 20>;
132-
133133
assert_eq!(Bounded::new(2).unwrap() + 3, 5);
134134
assert_eq!(Bounded::new(2).unwrap() - 5, -3);
135135
assert_eq!(Bounded::new(3).unwrap() * 5, 15);
@@ -144,11 +144,43 @@ mod tests {
144144
assert_eq!(Bounded::new(3).unwrap() << 1, 6);
145145
assert_eq!(Bounded::new(3).unwrap() >> 1, 1);
146146
variations!(Bounded, i8, + += - -= * *= / /= % %=);
147+
147148
assert_eq!(Bounded::new(2).unwrap().pow(3).get(), 8);
148149
assert_eq!(Bounded::new(-3).unwrap().div_euclid(2).get(), -2);
149150
assert_eq!(Bounded::new(-3).unwrap().rem_euclid(2).get(), 1);
150151
assert_eq!(Bounded::new(-3).unwrap().abs().get(), 3);
151152
assert_eq!(Bounded::new(4).unwrap().abs().get(), 4);
153+
154+
macro_rules! variations {
155+
($ty:ty, $inner:ty, $($op:tt $op_assign:tt)*) => {
156+
$(
157+
let _: $ty = <$ty>::new(0).unwrap() $op 1;
158+
let _: $ty = &<$ty>::new(0).unwrap() $op 1;
159+
let _: $ty = <$ty>::new(0).unwrap() $op &1;
160+
let _: $ty = &<$ty>::new(0).unwrap() $op &1;
161+
let _: $inner = 0 $op <$ty>::new(1).unwrap();
162+
let _: $inner = 0 $op &<$ty>::new(1).unwrap();
163+
let _: $inner = &0 $op <$ty>::new(1).unwrap();
164+
let _: $inner = &0 $op &<$ty>::new(1).unwrap();
165+
let _: $ty = <$ty>::new(0).unwrap() $op <$ty>::new(1).unwrap();
166+
let _: $ty = &<$ty>::new(0).unwrap() $op <$ty>::new(1).unwrap();
167+
let _: $ty = <$ty>::new(0).unwrap() $op &<$ty>::new(1).unwrap();
168+
let _: $ty = &<$ty>::new(0).unwrap() $op &<$ty>::new(1).unwrap();
169+
*&mut <$ty>::new(0).unwrap() $op_assign 1;
170+
*&mut <$ty>::new(0).unwrap() $op_assign &1;
171+
*&mut <$ty>::new(0).unwrap() $op_assign <$ty>::new(1).unwrap();
172+
*&mut <$ty>::new(0).unwrap() $op_assign &<$ty>::new(1).unwrap();
173+
*&mut 0 $op_assign <$ty>::new(1).unwrap();
174+
*&mut 0 $op_assign &<$ty>::new(1).unwrap();
175+
)*
176+
};
177+
}
178+
use variations;
179+
}
180+
181+
#[test]
182+
fn saturating() {
183+
type Bounded = BoundedI8<-5, 20>;
152184
assert_eq!(Bounded::new(13).unwrap().saturating_add(1).get(), 14);
153185
assert_eq!(Bounded::new(14).unwrap().saturating_add(7).get(), 20);
154186
assert_eq!(Bounded::new(-2).unwrap().saturating_sub(-1).get(), -1);
@@ -161,6 +193,11 @@ mod tests {
161193
assert_eq!(Bounded::new(8).unwrap().saturating_neg().get(), -5);
162194
assert_eq!(Bounded::new(8).unwrap().saturating_abs().get(), 8);
163195
assert_eq!(<BoundedI8<-20, 5>>::new(-6).unwrap().saturating_abs(), 5);
196+
}
197+
198+
#[test]
199+
fn checked() {
200+
type Bounded = BoundedI8<-5, 20>;
164201
assert_eq!(Bounded::new(13).unwrap().checked_add(2).unwrap().get(), 15);
165202
assert_eq!(Bounded::new(14).unwrap().checked_add(7), None);
166203
assert_eq!(Bounded::new(-2).unwrap().checked_sub(-1).unwrap().get(), -1);
@@ -192,6 +229,11 @@ mod tests {
192229
assert_eq!(Bounded::new(-3).unwrap().checked_abs().unwrap().get(), 3);
193230
assert_eq!(Bounded::new(6).unwrap().checked_abs().unwrap().get(), 6);
194231
assert_eq!(<BoundedI8<-20, 5>>::new(-6).unwrap().checked_abs(), None);
232+
}
233+
234+
#[test]
235+
fn wrapping() {
236+
type Bounded = BoundedI8<-5, 20>;
195237
assert_eq!(Bounded::new(0).unwrap().wrapping_add(0).get(), 0);
196238
assert_eq!(Bounded::new(-5).unwrap().wrapping_add(-128), -3);
197239
assert_eq!(Bounded::new(-5).unwrap().wrapping_sub(127), -2);
@@ -207,32 +249,6 @@ mod tests {
207249
assert_eq!(Bounded::new(6).unwrap().wrapping_abs(), 6);
208250
assert_eq!(<BoundedI8<-20, 5>>::new(-6).unwrap().wrapping_abs(), -20);
209251
assert_eq!(Bounded::new(5).unwrap().wrapping_pow(607), -5);
210-
211-
macro_rules! variations {
212-
($ty:ty, $inner:ty, $($op:tt $op_assign:tt)*) => {
213-
$(
214-
let _: $ty = <$ty>::new(0).unwrap() $op 1;
215-
let _: $ty = &<$ty>::new(0).unwrap() $op 1;
216-
let _: $ty = <$ty>::new(0).unwrap() $op &1;
217-
let _: $ty = &<$ty>::new(0).unwrap() $op &1;
218-
let _: $inner = 0 $op <$ty>::new(1).unwrap();
219-
let _: $inner = 0 $op &<$ty>::new(1).unwrap();
220-
let _: $inner = &0 $op <$ty>::new(1).unwrap();
221-
let _: $inner = &0 $op &<$ty>::new(1).unwrap();
222-
let _: $ty = <$ty>::new(0).unwrap() $op <$ty>::new(1).unwrap();
223-
let _: $ty = &<$ty>::new(0).unwrap() $op <$ty>::new(1).unwrap();
224-
let _: $ty = <$ty>::new(0).unwrap() $op &<$ty>::new(1).unwrap();
225-
let _: $ty = &<$ty>::new(0).unwrap() $op &<$ty>::new(1).unwrap();
226-
*&mut <$ty>::new(0).unwrap() $op_assign 1;
227-
*&mut <$ty>::new(0).unwrap() $op_assign &1;
228-
*&mut <$ty>::new(0).unwrap() $op_assign <$ty>::new(1).unwrap();
229-
*&mut <$ty>::new(0).unwrap() $op_assign &<$ty>::new(1).unwrap();
230-
*&mut 0 $op_assign <$ty>::new(1).unwrap();
231-
*&mut 0 $op_assign &<$ty>::new(1).unwrap();
232-
)*
233-
};
234-
}
235-
use variations;
236252
}
237253

238254
#[test]
@@ -244,6 +260,7 @@ mod tests {
244260
#[test]
245261
fn iter() {
246262
type Bounded = BoundedI8<-8, 8>;
263+
#[expect(clippy::trivially_copy_pass_by_ref)]
247264
fn b(&n: &i8) -> Bounded {
248265
Bounded::new(n).unwrap()
249266
}
@@ -321,6 +338,7 @@ mod tests {
321338

322339
#[test]
323340
#[cfg(feature = "num-traits02")]
341+
#[expect(clippy::too_many_lines)]
324342
fn num() {
325343
use num_traits02::{
326344
AsPrimitive, Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem,
@@ -356,8 +374,10 @@ mod tests {
356374
assert_eq!(<B as AsPrimitive<i64>>::as_(b(4)), 4i64);
357375
assert_eq!(<B as AsPrimitive<i128>>::as_(b(4)), 4i128);
358376
assert_eq!(<B as AsPrimitive<isize>>::as_(b(4)), 4isize);
359-
assert_eq!(<B as AsPrimitive<f32>>::as_(b(4)), 4f32);
360-
assert_eq!(<B as AsPrimitive<f64>>::as_(b(4)), 4f64);
377+
#[expect(clippy::float_cmp)]
378+
let () = assert_eq!(<B as AsPrimitive<f32>>::as_(b(4)), 4f32);
379+
#[expect(clippy::float_cmp)]
380+
let () = assert_eq!(<B as AsPrimitive<f64>>::as_(b(4)), 4f64);
361381

362382
assert_eq!(B::from_u8(4u8), Some(b(4)));
363383
assert_eq!(B::from_u16(4u16), Some(b(4)));

src/unsafe_api.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ macro_rules! __unsafe_api_internal {
106106
use $crate::__private::Dispatch;
107107

108108
#[allow(dead_code)]
109+
#[allow(clippy::double_must_use)]
109110
impl<$($generics)*> $ty where $($where)* {
110111
/// The smallest value this bounded integer can contain.
111112
pub const MIN_VALUE: $inner = $min;
@@ -266,6 +267,8 @@ macro_rules! __unsafe_api_internal {
266267
let Some((range, left, right)) = offsets else {
267268
// In the case where the range spans this entire type, truncating is
268269
// equivalent to taking modulo.
270+
#[allow(clippy::cast_possible_truncation)]
271+
#[allow(clippy::cast_sign_loss)]
269272
return unsafe { Self::new_unchecked(n as _) };
270273
};
271274

@@ -280,6 +283,7 @@ macro_rules! __unsafe_api_internal {
280283

281284
// Calculate `shifted mod range`. Since `range` fits in an `Unsigned`, we
282285
// know the result will too.
286+
#[allow(clippy::cast_possible_truncation)]
283287
let rem = <Dispatch<$super>>::rem_euclid_unsigned(shifted, range_t) as _;
284288

285289
let inner = <Dispatch<$inner>>::checked_add_unsigned(Self::MIN_VALUE, rem).unwrap();
@@ -1252,7 +1256,9 @@ macro_rules! __unsafe_api_internal {
12521256
}
12531257
}
12541258

1255-
#[automatically_derived]
1259+
// Disable this to prevent triggering `clippy::unsafe_derive_deserialize`. I couldn’t
1260+
// figure out how to `#[allow]` it.
1261+
// #[automatically_derived]
12561262
impl<'__de, $($generics)*> Deserialize<'__de> for $ty {
12571263
fn deserialize<D: Deserializer<'__de>>(deserializer: D) -> Result<Self, D::Error> {
12581264
Self::new(<$inner>::deserialize(deserializer)?)

tests/hygiene.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![no_std]
33
#![cfg_attr(feature = "step_trait", feature(step_trait))]
44
#![cfg(feature = "macro")]
5-
#![forbid(clippy::pedantic)]
5+
#![deny(clippy::pedantic)]
66

77
#[expect(dead_code, non_camel_case_types)]
88
struct u8 {}

0 commit comments

Comments
 (0)