Skip to content

Commit d4a2ff6

Browse files
authored
ctutils: additional clippy lints (#1384)
Enables the following and fixes violations if any: - `clippy::borrow_as_ptr` - `clippy::must_use_candidate` - `clippy::ref_as_ptr` - `clippy::semicolon_if_nothing_returned` - `clippy::unwrap_in_result`
1 parent a10fe63 commit d4a2ff6

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

ctutils/src/choice.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ impl Choice {
104104

105105
/// Returns [`Choice::TRUE`] if `x == y`, and [`Choice::FALSE`] otherwise.
106106
#[inline]
107+
#[must_use]
107108
pub const fn from_i64_eq(x: i64, y: i64) -> Self {
108109
Self::from_u64_nz(x as u64 ^ y as u64).not()
109110
}
@@ -112,30 +113,35 @@ impl Choice {
112113

113114
/// Returns [`Choice::TRUE`] if `x == y`, and [`Choice::FALSE`] otherwise.
114115
#[inline]
116+
#[must_use]
115117
pub const fn from_u8_eq(x: u8, y: u8) -> Self {
116118
Self::from_u8_nz(x ^ y).not()
117119
}
118120

119121
/// Returns [`Choice::TRUE`] if `x <= y` and [`Choice::FALSE`] otherwise.
120122
#[inline]
123+
#[must_use]
121124
pub const fn from_u8_le(x: u8, y: u8) -> Self {
122125
Self::from_u8_lsb(bitle!(x, y, u8::BITS))
123126
}
124127

125128
/// Initialize from the least significant bit of a `u8`.
126129
#[inline]
130+
#[must_use]
127131
pub const fn from_u8_lsb(value: u8) -> Self {
128132
Self(value & 0x1)
129133
}
130134

131135
/// Returns [`Choice::TRUE`] if `x < y`, and [`Choice::FALSE`] otherwise.
132136
#[inline]
137+
#[must_use]
133138
pub const fn from_u8_lt(x: u8, y: u8) -> Self {
134139
Self::from_u8_lsb(bitlt!(x, y, u8::BITS))
135140
}
136141

137142
/// Returns [`Choice::TRUE`] if `value != 0`, and [`Choice::FALSE`] otherwise.
138143
#[inline]
144+
#[must_use]
139145
pub const fn from_u8_nz(value: u8) -> Self {
140146
Self::from_u8_lsb(bitnz!(value, u8::BITS))
141147
}
@@ -144,30 +150,35 @@ impl Choice {
144150

145151
/// Returns [`Choice::TRUE`] if `x == y`, and [`Choice::FALSE`] otherwise.
146152
#[inline]
153+
#[must_use]
147154
pub const fn from_u16_eq(x: u16, y: u16) -> Self {
148155
Self::from_u16_nz(x ^ y).not()
149156
}
150157

151158
/// Returns [`Choice::TRUE`] if `x <= y` and [`Choice::FALSE`] otherwise.
152159
#[inline]
160+
#[must_use]
153161
pub const fn from_u16_le(x: u16, y: u16) -> Self {
154162
Self::from_u16_lsb(bitle!(x, y, u16::BITS))
155163
}
156164

157165
/// Initialize from the least significant bit of a `u16`.
158166
#[inline]
167+
#[must_use]
159168
pub const fn from_u16_lsb(value: u16) -> Self {
160169
Self((value & 0x1) as u8)
161170
}
162171

163172
/// Returns [`Choice::TRUE`] if `x < y`, and [`Choice::FALSE`] otherwise.
164173
#[inline]
174+
#[must_use]
165175
pub const fn from_u16_lt(x: u16, y: u16) -> Self {
166176
Self::from_u16_lsb(bitlt!(x, y, u16::BITS))
167177
}
168178

169179
/// Returns [`Choice::TRUE`] if `value != 0`, and [`Choice::FALSE`] otherwise.
170180
#[inline]
181+
#[must_use]
171182
pub const fn from_u16_nz(value: u16) -> Self {
172183
Self::from_u16_lsb(bitnz!(value, u16::BITS))
173184
}
@@ -176,30 +187,35 @@ impl Choice {
176187

177188
/// Returns [`Choice::TRUE`] if `x == y`, and [`Choice::FALSE`] otherwise.
178189
#[inline]
190+
#[must_use]
179191
pub const fn from_u32_eq(x: u32, y: u32) -> Self {
180192
Self::from_u32_nz(x ^ y).not()
181193
}
182194

183195
/// Returns [`Choice::TRUE`] if `x <= y` and [`Choice::FALSE`] otherwise.
184196
#[inline]
197+
#[must_use]
185198
pub const fn from_u32_le(x: u32, y: u32) -> Self {
186199
Self::from_u32_lsb(bitle!(x, y, u32::BITS))
187200
}
188201

189202
/// Initialize from the least significant bit of a `u32`.
190203
#[inline]
204+
#[must_use]
191205
pub const fn from_u32_lsb(value: u32) -> Self {
192206
Self((value & 0x1) as u8)
193207
}
194208

195209
/// Returns [`Choice::TRUE`] if `x < y`, and [`Choice::FALSE`] otherwise.
196210
#[inline]
211+
#[must_use]
197212
pub const fn from_u32_lt(x: u32, y: u32) -> Self {
198213
Self::from_u32_lsb(bitlt!(x, y, u32::BITS))
199214
}
200215

201216
/// Returns [`Choice::TRUE`] if `value != 0`, and [`Choice::FALSE`] otherwise.
202217
#[inline]
218+
#[must_use]
203219
pub const fn from_u32_nz(value: u32) -> Self {
204220
Self::from_u32_lsb(bitnz!(value, u32::BITS))
205221
}
@@ -208,30 +224,35 @@ impl Choice {
208224

209225
/// Returns [`Choice::TRUE`] if `x == y`, and [`Choice::FALSE`] otherwise.
210226
#[inline]
227+
#[must_use]
211228
pub const fn from_u64_eq(x: u64, y: u64) -> Self {
212229
Self::from_u64_nz(x ^ y).not()
213230
}
214231

215232
/// Returns [`Choice::TRUE`] if `x <= y` and [`Choice::FALSE`] otherwise.
216233
#[inline]
234+
#[must_use]
217235
pub const fn from_u64_le(x: u64, y: u64) -> Self {
218236
Self::from_u64_lsb(bitle!(x, y, u64::BITS))
219237
}
220238

221239
/// Initialize from the least significant bit of a `u64`.
222240
#[inline]
241+
#[must_use]
223242
pub const fn from_u64_lsb(value: u64) -> Self {
224243
Self((value & 0x1) as u8)
225244
}
226245

227246
/// Returns [`Choice::TRUE`] if `x < y`, and [`Choice::FALSE`] otherwise.
228247
#[inline]
248+
#[must_use]
229249
pub const fn from_u64_lt(x: u64, y: u64) -> Self {
230250
Self::from_u64_lsb(bitlt!(x, y, u64::BITS))
231251
}
232252

233253
/// Returns [`Choice::TRUE`] if `value != 0`, and [`Choice::FALSE`] otherwise.
234254
#[inline]
255+
#[must_use]
235256
pub const fn from_u64_nz(value: u64) -> Self {
236257
Self::from_u64_lsb(bitnz!(value, u64::BITS))
237258
}
@@ -240,30 +261,35 @@ impl Choice {
240261

241262
/// Returns [`Choice::TRUE`] if `x == y`, and [`Choice::FALSE`] otherwise.
242263
#[inline]
264+
#[must_use]
243265
pub const fn from_u128_eq(x: u128, y: u128) -> Self {
244266
Self::from_u128_nz(x ^ y).not()
245267
}
246268

247269
/// Returns [`Choice::TRUE`] if `x <= y` and [`Choice::FALSE`] otherwise.
248270
#[inline]
271+
#[must_use]
249272
pub const fn from_u128_le(x: u128, y: u128) -> Self {
250273
Self::from_u128_lsb(bitle!(x, y, u128::BITS))
251274
}
252275

253276
/// Initialize from the least significant bit of a `u128`.
254277
#[inline]
278+
#[must_use]
255279
pub const fn from_u128_lsb(value: u128) -> Self {
256280
Self((value & 1) as u8)
257281
}
258282

259283
/// Returns [`Choice::TRUE`] if `x < y`, and [`Choice::FALSE`] otherwise.
260284
#[inline]
285+
#[must_use]
261286
pub const fn from_u128_lt(x: u128, y: u128) -> Self {
262287
Self::from_u128_lsb(bitlt!(x, y, u128::BITS))
263288
}
264289

265290
/// Returns [`Choice::TRUE`] if `value != 0`, and [`Choice::FALSE`] otherwise.
266291
#[inline]
292+
#[must_use]
267293
pub const fn from_u128_nz(value: u128) -> Self {
268294
Self::from_u128_lsb(bitnz!(value, u128::BITS))
269295
}
@@ -277,6 +303,7 @@ impl Choice {
277303
/// Only use this instead of the [`CtSelect`] trait in the event you're in a `const fn` context
278304
/// and can't use the trait. The former will provide better constant-time assurances.
279305
#[inline]
306+
#[must_use]
280307
pub const fn select_i64(self, a: i64, b: i64) -> i64 {
281308
self.select_u64(a as u64, b as u64) as i64
282309
}
@@ -286,6 +313,7 @@ impl Choice {
286313
/// Only use this instead of the [`CtSelect`] trait in the event you're in a `const fn` context
287314
/// and can't use the trait. The former will provide better constant-time assurances.
288315
#[inline]
316+
#[must_use]
289317
pub const fn select_u8(self, a: u8, b: u8) -> u8 {
290318
a ^ (self.to_u8_mask() & (a ^ b))
291319
}
@@ -295,6 +323,7 @@ impl Choice {
295323
/// Only use this instead of the [`CtSelect`] trait in the event you're in a `const fn` context
296324
/// and can't use the trait. The former will provide better constant-time assurances.
297325
#[inline]
326+
#[must_use]
298327
pub const fn select_u16(self, a: u16, b: u16) -> u16 {
299328
a ^ (self.to_u16_mask() & (a ^ b))
300329
}
@@ -304,6 +333,7 @@ impl Choice {
304333
/// Only use this instead of the [`CtSelect`] trait in the event you're in a `const fn` context
305334
/// and can't use the trait. The former will provide better constant-time assurances.
306335
#[inline]
336+
#[must_use]
307337
pub const fn select_u32(self, a: u32, b: u32) -> u32 {
308338
a ^ (self.to_u32_mask() & (a ^ b))
309339
}
@@ -313,6 +343,7 @@ impl Choice {
313343
/// Only use this instead of the [`CtSelect`] trait in the event you're in a `const fn` context
314344
/// and can't use the trait. The former will provide better constant-time assurances.
315345
#[inline]
346+
#[must_use]
316347
pub const fn select_u64(self, a: u64, b: u64) -> u64 {
317348
a ^ (self.to_u64_mask() & (a ^ b))
318349
}
@@ -322,6 +353,7 @@ impl Choice {
322353
/// Only use this instead of the [`CtSelect`] trait in the event you're in a `const fn` context
323354
/// and can't use the trait. The former will provide better constant-time assurances.
324355
#[inline]
356+
#[must_use]
325357
pub const fn select_u128(self, a: u128, b: u128) -> u128 {
326358
a ^ (self.to_u128_mask() & (a ^ b))
327359
}
@@ -348,12 +380,14 @@ impl Choice {
348380
/// possible, prefer fully constant-time approaches instead.
349381
/// </div>
350382
// TODO(tarcieri): `const fn` when MSRV 1.86
383+
#[must_use]
351384
pub fn to_bool(self) -> bool {
352385
self.to_u8() != 0
353386
}
354387

355388
/// Convert [`Choice`] to a `u8`, attempting to apply a "best effort" optimization barrier.
356389
// TODO(tarcieri): `const fn` when MSRV 1.86
390+
#[must_use]
357391
pub fn to_u8(self) -> u8 {
358392
// `black_box` is documented as working on a "best effort" basis. That's fine, this type is
359393
// likewise documented as only working on a "best effort" basis itself. The only way we
@@ -371,6 +405,7 @@ impl Choice {
371405
/// See the security warnings for [`Choice::to_bool`].
372406
/// </div>
373407
// TODO(tarcieri): deprecate/remove this in favor of `to_bool` when MSRV is Rust 1.86
408+
#[must_use]
374409
pub const fn to_bool_vartime(self) -> bool {
375410
self.0 != 0
376411
}
@@ -379,6 +414,7 @@ impl Choice {
379414
///
380415
/// This does not apply `black_box` to the output.
381416
// TODO(tarcieri): deprecate/remove this in favor of `to_u8` when MSRV is Rust 1.86
417+
#[must_use]
382418
pub const fn to_u8_vartime(self) -> u8 {
383419
self.0
384420
}
@@ -389,6 +425,7 @@ impl Choice {
389425
/// - `0` for `Choice::FALSE`
390426
/// - `u8::MAX` for `Choice::TRUE`
391427
#[inline]
428+
#[must_use]
392429
pub const fn to_u8_mask(self) -> u8 {
393430
self.0.wrapping_neg()
394431
}
@@ -399,6 +436,7 @@ impl Choice {
399436
/// - `0` for `Choice::FALSE`
400437
/// - `u16::MAX` for `Choice::TRUE`
401438
#[inline]
439+
#[must_use]
402440
pub const fn to_u16_mask(self) -> u16 {
403441
(self.0 as u16).wrapping_neg()
404442
}
@@ -409,6 +447,7 @@ impl Choice {
409447
/// - `0` for `Choice::FALSE`
410448
/// - `u32::MAX` for `Choice::TRUE`
411449
#[inline]
450+
#[must_use]
412451
pub const fn to_u32_mask(self) -> u32 {
413452
(self.0 as u32).wrapping_neg()
414453
}
@@ -419,6 +458,7 @@ impl Choice {
419458
/// - `0` for `Choice::FALSE`
420459
/// - `u64::MAX` for `Choice::TRUE`
421460
#[inline]
461+
#[must_use]
422462
pub const fn to_u64_mask(self) -> u64 {
423463
(self.0 as u64).wrapping_neg()
424464
}
@@ -429,6 +469,7 @@ impl Choice {
429469
/// - `0` for `Choice::FALSE`
430470
/// - `u128::MAX` for `Choice::TRUE`
431471
#[inline]
472+
#[must_use]
432473
pub const fn to_u128_mask(self) -> u128 {
433474
(self.0 as u128).wrapping_neg()
434475
}

ctutils/src/ct_option.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,13 +727,13 @@ mod tests {
727727
#[test]
728728
fn into_option() {
729729
assert_eq!(SOME.into_option(), Some(VALUE));
730-
assert_eq!(NONE.into_option(), None)
730+
assert_eq!(NONE.into_option(), None);
731731
}
732732

733733
#[test]
734734
fn into_option_copied() {
735735
assert_eq!(SOME.into_option_copied(), Some(VALUE));
736-
assert_eq!(NONE.into_option_copied(), None)
736+
assert_eq!(NONE.into_option_copied(), None);
737737
}
738738

739739
#[test]

ctutils/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,24 @@
66
)]
77
#![deny(unsafe_code)]
88
#![warn(
9+
clippy::borrow_as_ptr,
910
clippy::cast_lossless,
1011
clippy::cast_possible_truncation,
1112
clippy::cast_precision_loss,
1213
clippy::checked_conversions,
1314
clippy::implicit_saturating_sub,
1415
clippy::integer_division_remainder_used,
16+
clippy::must_use_candidate,
1517
clippy::mod_module_files,
1618
clippy::panic,
1719
clippy::panic_in_result_fn,
18-
clippy::return_self_not_must_use,
20+
clippy::ref_as_ptr,
21+
clippy::semicolon_if_nothing_returned,
1922
clippy::std_instead_of_alloc,
2023
clippy::std_instead_of_core,
2124
clippy::undocumented_unsafe_blocks,
2225
clippy::unnecessary_safety_comment,
26+
clippy::unwrap_in_result,
2327
missing_copy_implementations,
2428
missing_debug_implementations,
2529
missing_docs,

ctutils/src/slice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ where
3030
);
3131

3232
for (a, b) in dst.iter_mut().zip(src) {
33-
a.ct_assign(b, choice)
33+
a.ct_assign(b, choice);
3434
}
3535
}
3636

ctutils/src/traits/ct_select.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl CtSelect for cmp::Ordering {
134134
// a value which was originally a valid `Ordering` then cast to `i8`
135135
#[allow(trivial_casts, unsafe_code)]
136136
unsafe {
137-
*(&ret as *const i8).cast::<Self>()
137+
*(&raw const ret).cast::<Self>()
138138
}
139139
}
140140
}

0 commit comments

Comments
 (0)