Skip to content

Commit d49fc3b

Browse files
Fixed failing Clippy lints (#88)
1 parent 43e4d69 commit d49fc3b

File tree

8 files changed

+42
-32
lines changed

8 files changed

+42
-32
lines changed

src/ascii_char.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,12 +679,12 @@ impl AsciiChar {
679679

680680
/// Replaces letters `a` to `z` with `A` to `Z`
681681
pub fn make_ascii_uppercase(&mut self) {
682-
*self = self.to_ascii_uppercase()
682+
*self = self.to_ascii_uppercase();
683683
}
684684

685685
/// Replaces letters `A` to `Z` with `a` to `z`
686686
pub fn make_ascii_lowercase(&mut self) {
687-
*self = self.to_ascii_lowercase()
687+
*self = self.to_ascii_lowercase();
688688
}
689689

690690
/// Maps letters a-z to A-Z and returns any other character unchanged.
@@ -800,6 +800,7 @@ impl ToAsciiCharError {
800800
/// Returns a description for this error, like `std::error::Error::description`.
801801
#[inline]
802802
#[must_use]
803+
#[allow(clippy::unused_self)]
803804
pub const fn description(&self) -> &'static str {
804805
ERRORMSG_CHAR
805806
}

src/ascii_str.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -308,19 +308,20 @@ impl AsciiStr {
308308
#[inline]
309309
#[must_use]
310310
pub fn first(&self) -> Option<AsciiChar> {
311-
self.slice.first().cloned()
311+
self.slice.first().copied()
312312
}
313313

314314
/// Returns the last character if the string is not empty.
315315
#[inline]
316316
#[must_use]
317317
pub fn last(&self) -> Option<AsciiChar> {
318-
self.slice.last().cloned()
318+
self.slice.last().copied()
319319
}
320320

321321
/// Converts a [`Box<AsciiStr>`] into a [`AsciiString`] without copying or allocating.
322322
#[cfg(feature = "alloc")]
323323
#[inline]
324+
#[must_use]
324325
pub fn into_ascii_string(self: Box<Self>) -> AsciiString {
325326
let slice = Box::<[AsciiChar]>::from(self);
326327
AsciiString::from(slice.into_vec())
@@ -581,7 +582,7 @@ impl<'a> Iterator for Chars<'a> {
581582
type Item = AsciiChar;
582583
#[inline]
583584
fn next(&mut self) -> Option<AsciiChar> {
584-
self.0.next().cloned()
585+
self.0.next().copied()
585586
}
586587
fn size_hint(&self) -> (usize, Option<usize>) {
587588
self.0.size_hint()
@@ -590,7 +591,7 @@ impl<'a> Iterator for Chars<'a> {
590591
impl<'a> DoubleEndedIterator for Chars<'a> {
591592
#[inline]
592593
fn next_back(&mut self) -> Option<AsciiChar> {
593-
self.0.next_back().cloned()
594+
self.0.next_back().copied()
594595
}
595596
}
596597
impl<'a> ExactSizeIterator for Chars<'a> {
@@ -820,6 +821,7 @@ impl AsAsciiStrError {
820821
/// Returns a description for this error, like `std::error::Error::description`.
821822
#[inline]
822823
#[must_use]
824+
#[allow(clippy::unused_self)]
823825
pub const fn description(&self) -> &'static str {
824826
ERRORMSG_STR
825827
}
@@ -1051,7 +1053,7 @@ impl AsAsciiStr for [AsciiChar] {
10511053

10521054
#[inline]
10531055
fn get_ascii(&self, index: usize) -> Option<AsciiChar> {
1054-
self.get(index).cloned()
1056+
self.get(index).copied()
10551057
}
10561058
}
10571059
impl AsMutAsciiStr for [AsciiChar] {
@@ -1173,7 +1175,7 @@ impl AsMutAsciiStr for str {
11731175
// Valid ascii slice
11741176
Some(slice) if slice.is_ascii() => {
11751177
// SAFETY: All bytes are ascii, so this cast is valid
1176-
let ptr = slice.as_mut_ptr() as *mut AsciiChar;
1178+
let ptr = slice.as_mut_ptr().cast::<AsciiChar>();
11771179
let len = slice.len();
11781180

11791181
// SAFETY: The pointer is valid for `len` elements, as it came
@@ -1249,8 +1251,8 @@ mod tests {
12491251
let ascii_str = arr.as_ref().into();
12501252
let mut mut_arr = arr; // Note: We need a second copy to prevent overlapping mutable borrows.
12511253
let mut_ascii_str = mut_arr.as_mut().into();
1252-
let mut_arr_mut_ref: &mut [AsciiChar] = &mut [AsciiChar::A];
1253-
let mut string_bytes = [b'A'];
1254+
let mut_arr_mut_ref: &mut [AsciiChar] = &mut [AsciiChar::A];
1255+
let mut string_bytes = [b'A'];
12541256
let string_mut = unsafe { core::str::from_utf8_unchecked_mut(&mut string_bytes) }; // SAFETY: 'A' is a valid string.
12551257
let string_mut_bytes: &mut [u8] = &mut [b'A'];
12561258

@@ -1374,6 +1376,7 @@ mod tests {
13741376
}
13751377

13761378
#[test]
1379+
#[allow(clippy::redundant_slicing)]
13771380
fn index() {
13781381
let mut arr = [AsciiChar::A, AsciiChar::B, AsciiChar::C, AsciiChar::D];
13791382
{
@@ -1442,7 +1445,7 @@ mod tests {
14421445
b'h', b'e', b'l', b'l', b'o', b' ', b'w', b'o', b'r', b'l', b'd', b'\0',
14431446
];
14441447
let ascii = AsciiStr::from_ascii(chars).unwrap();
1445-
for (achar, byte) in ascii.chars().zip(chars.iter().cloned()) {
1448+
for (achar, byte) in ascii.chars().zip(chars.iter().copied()) {
14461449
assert_eq!(achar, byte);
14471450
}
14481451
}

src/ascii_string.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl AsciiString {
116116
{
117117
let mut bytes = bytes.into();
118118
// SAFETY: The caller guarantees all bytes are valid ascii bytes.
119-
let ptr = bytes.as_mut_ptr() as *mut AsciiChar;
119+
let ptr = bytes.as_mut_ptr().cast::<AsciiChar>();
120120
let length = bytes.len();
121121
let capacity = bytes.capacity();
122122
mem::forget(bytes);
@@ -169,7 +169,7 @@ impl AsciiString {
169169
/// ```
170170
#[inline]
171171
pub fn push_str(&mut self, string: &AsciiStr) {
172-
self.vec.extend(string.chars())
172+
self.vec.extend(string.chars());
173173
}
174174

175175
/// Inserts the given ASCII string at the given place in this ASCII string buffer.
@@ -220,7 +220,7 @@ impl AsciiString {
220220
/// ```
221221
#[inline]
222222
pub fn reserve(&mut self, additional: usize) {
223-
self.vec.reserve(additional)
223+
self.vec.reserve(additional);
224224
}
225225

226226
/// Reserves the minimum capacity for exactly `additional` more bytes to be inserted in the
@@ -243,7 +243,7 @@ impl AsciiString {
243243
#[inline]
244244

245245
pub fn reserve_exact(&mut self, additional: usize) {
246-
self.vec.reserve_exact(additional)
246+
self.vec.reserve_exact(additional);
247247
}
248248

249249
/// Shrinks the capacity of this ASCII string buffer to match it's length.
@@ -261,7 +261,7 @@ impl AsciiString {
261261
#[inline]
262262

263263
pub fn shrink_to_fit(&mut self) {
264-
self.vec.shrink_to_fit()
264+
self.vec.shrink_to_fit();
265265
}
266266

267267
/// Adds the given ASCII character to the end of the ASCII string.
@@ -278,7 +278,7 @@ impl AsciiString {
278278
#[inline]
279279

280280
pub fn push(&mut self, ch: AsciiChar) {
281-
self.vec.push(ch)
281+
self.vec.push(ch);
282282
}
283283

284284
/// Shortens a ASCII string to the specified length.
@@ -296,7 +296,7 @@ impl AsciiString {
296296
#[inline]
297297

298298
pub fn truncate(&mut self, new_len: usize) {
299-
self.vec.truncate(new_len)
299+
self.vec.truncate(new_len);
300300
}
301301

302302
/// Removes the last character from the ASCII string buffer and returns it.
@@ -357,7 +357,7 @@ impl AsciiString {
357357
#[inline]
358358

359359
pub fn insert(&mut self, idx: usize, ch: AsciiChar) {
360-
self.vec.insert(idx, ch)
360+
self.vec.insert(idx, ch);
361361
}
362362

363363
/// Returns the number of bytes in this ASCII string.
@@ -402,14 +402,15 @@ impl AsciiString {
402402
#[inline]
403403

404404
pub fn clear(&mut self) {
405-
self.vec.clear()
405+
self.vec.clear();
406406
}
407407

408408
/// Converts this [`AsciiString`] into a [`Box`]`<`[`AsciiStr`]`>`.
409409
///
410410
/// This will drop any excess capacity
411411
#[cfg(feature = "alloc")]
412412
#[inline]
413+
#[must_use]
413414
pub fn into_boxed_ascii_str(self) -> Box<AsciiStr> {
414415
let slice = self.vec.into_boxed_slice();
415416
Box::from(slice)
@@ -490,15 +491,17 @@ impl From<Vec<AsciiChar>> for AsciiString {
490491
impl From<AsciiChar> for AsciiString {
491492
#[inline]
492493
fn from(ch: AsciiChar) -> Self {
493-
AsciiString {vec: vec![ch]}
494+
AsciiString { vec: vec![ch] }
494495
}
495496
}
496497

498+
// FIXME? Turn this into a `From` impl
499+
#[allow(clippy::from_over_into)]
497500
impl Into<Vec<u8>> for AsciiString {
498501
fn into(mut self) -> Vec<u8> {
499502
// SAFETY: All ascii bytes are valid `u8`, as we are `repr(u8)`.
500503
// Note: We forget `self` to avoid `self.vec` from being deallocated.
501-
let ptr = self.vec.as_mut_ptr() as *mut u8;
504+
let ptr = self.vec.as_mut_ptr().cast::<u8>();
502505
let length = self.vec.len();
503506
let capacity = self.vec.capacity();
504507
mem::forget(self);
@@ -520,10 +523,12 @@ impl<'a> From<&'a AsciiStr> for AsciiString {
520523
impl<'a> From<&'a [AsciiChar]> for AsciiString {
521524
#[inline]
522525
fn from(s: &'a [AsciiChar]) -> AsciiString {
523-
s.iter().cloned().collect()
526+
s.iter().copied().collect()
524527
}
525528
}
526529

530+
// FIXME? Turn this into a `From` impl
531+
#[allow(clippy::from_over_into)]
527532
impl Into<String> for AsciiString {
528533
#[inline]
529534
fn into(self) -> String {
@@ -666,7 +671,7 @@ impl<A: AsRef<AsciiStr>> Extend<A> for AsciiString {
666671
let (lower_bound, _) = iterator.size_hint();
667672
self.reserve(lower_bound);
668673
for item in iterator {
669-
self.push_str(item.as_ref())
674+
self.push_str(item.as_ref());
670675
}
671676
}
672677
}
@@ -899,7 +904,7 @@ impl<'a> IntoAsciiString for &'a CStr {
899904
.map_err(|FromAsciiError { error, owner }| FromAsciiError {
900905
// SAFETY: We don't discard the NULL byte from the original
901906
// string, so we ensure that it's null terminated
902-
owner: unsafe { CStr::from_ptr(owner.as_ptr() as *const _) },
907+
owner: unsafe { CStr::from_ptr(owner.as_ptr().cast()) },
903908
error,
904909
})
905910
.map(|mut s| {
@@ -954,7 +959,7 @@ mod tests {
954959
use alloc::vec::Vec;
955960
#[cfg(feature = "std")]
956961
use std::ffi::CString;
957-
use ::{AsciiChar, AsciiStr};
962+
use {AsciiChar, AsciiStr};
958963

959964
#[test]
960965
fn into_string() {
@@ -965,7 +970,7 @@ mod tests {
965970
#[test]
966971
fn into_bytes() {
967972
let v = AsciiString::from_ascii(&[40_u8, 32, 59][..]).unwrap();
968-
assert_eq!(Into::<Vec<u8>>::into(v), vec![40_u8, 32, 59])
973+
assert_eq!(Into::<Vec<u8>>::into(v), vec![40_u8, 32, 59]);
969974
}
970975

971976
#[test]

src/free_functions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use ascii_char::{AsciiChar, ToAsciiChar};
1717
pub fn caret_encode<C: Copy + Into<u8>>(c: C) -> Option<AsciiChar> {
1818
// The formula is explained in the Wikipedia article.
1919
let c = c.into() ^ 0b0100_0000;
20-
if c >= b'?' && c <= b'_' {
20+
if (b'?'..=b'_').contains(&c) {
2121
// SAFETY: All bytes between '?' (0x3F) and '_' (0x5f) are valid ascii characters.
2222
Some(unsafe { c.to_ascii_char_unchecked() })
2323
} else {

src/serialization/ascii_char.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ mod tests {
6060
#[test]
6161
fn basic() {
6262
fn assert_serialize<T: Serialize>() {}
63-
assert_serialize::<AsciiChar>();
6463
fn assert_deserialize<'de, T: Deserialize<'de>>() {}
64+
assert_serialize::<AsciiChar>();
6565
assert_deserialize::<AsciiChar>();
6666
}
6767

src/serialization/ascii_str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ mod tests {
5252
#[test]
5353
fn basic() {
5454
fn assert_serialize<T: Serialize>() {}
55-
assert_serialize::<&AsciiStr>();
5655
fn assert_deserialize<'de, T: Deserialize<'de>>() {}
56+
assert_serialize::<&AsciiStr>();
5757
assert_deserialize::<&AsciiStr>();
5858
}
5959

src/serialization/ascii_string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl<'de> Visitor<'de> for AsciiStringVisitor {
3232
}
3333

3434
fn visit_bytes<E: Error>(self, v: &[u8]) -> Result<Self::Value, E> {
35-
AsciiString::from_ascii(v).map_err(|_| Error::invalid_value(Unexpected::Bytes(&v), &self))
35+
AsciiString::from_ascii(v).map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self))
3636
}
3737

3838
fn visit_byte_buf<E: Error>(self, v: Vec<u8>) -> Result<Self::Value, E> {
@@ -117,8 +117,8 @@ mod tests {
117117
#[test]
118118
fn basic() {
119119
fn assert_serialize<T: Serialize>() {}
120-
assert_serialize::<AsciiString>();
121120
fn assert_deserialize<'de, T: Deserialize<'de>>() {}
121+
assert_serialize::<AsciiString>();
122122
assert_deserialize::<AsciiString>();
123123
}
124124

tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ fn compare_ascii_str_str() {
9494
}
9595

9696
#[test]
97+
#[allow(clippy::redundant_slicing)]
9798
fn compare_ascii_str_slice() {
9899
let b = b"abc".as_ascii_str().unwrap();
99100
let c = b"ab".as_ascii_str().unwrap();

0 commit comments

Comments
 (0)