diff --git a/src/ascii_char.rs b/src/ascii_char.rs index f726df6..6cc02ca 100644 --- a/src/ascii_char.rs +++ b/src/ascii_char.rs @@ -679,12 +679,12 @@ impl AsciiChar { /// Replaces letters `a` to `z` with `A` to `Z` pub fn make_ascii_uppercase(&mut self) { - *self = self.to_ascii_uppercase() + *self = self.to_ascii_uppercase(); } /// Replaces letters `A` to `Z` with `a` to `z` pub fn make_ascii_lowercase(&mut self) { - *self = self.to_ascii_lowercase() + *self = self.to_ascii_lowercase(); } /// Maps letters a-z to A-Z and returns any other character unchanged. @@ -800,6 +800,7 @@ impl ToAsciiCharError { /// Returns a description for this error, like `std::error::Error::description`. #[inline] #[must_use] + #[allow(clippy::unused_self)] pub const fn description(&self) -> &'static str { ERRORMSG_CHAR } diff --git a/src/ascii_str.rs b/src/ascii_str.rs index 4c63eee..9c391a2 100644 --- a/src/ascii_str.rs +++ b/src/ascii_str.rs @@ -308,19 +308,20 @@ impl AsciiStr { #[inline] #[must_use] pub fn first(&self) -> Option { - self.slice.first().cloned() + self.slice.first().copied() } /// Returns the last character if the string is not empty. #[inline] #[must_use] pub fn last(&self) -> Option { - self.slice.last().cloned() + self.slice.last().copied() } /// Converts a [`Box`] into a [`AsciiString`] without copying or allocating. #[cfg(feature = "alloc")] #[inline] + #[must_use] pub fn into_ascii_string(self: Box) -> AsciiString { let slice = Box::<[AsciiChar]>::from(self); AsciiString::from(slice.into_vec()) @@ -581,7 +582,7 @@ impl<'a> Iterator for Chars<'a> { type Item = AsciiChar; #[inline] fn next(&mut self) -> Option { - self.0.next().cloned() + self.0.next().copied() } fn size_hint(&self) -> (usize, Option) { self.0.size_hint() @@ -590,7 +591,7 @@ impl<'a> Iterator for Chars<'a> { impl<'a> DoubleEndedIterator for Chars<'a> { #[inline] fn next_back(&mut self) -> Option { - self.0.next_back().cloned() + self.0.next_back().copied() } } impl<'a> ExactSizeIterator for Chars<'a> { @@ -820,6 +821,7 @@ impl AsAsciiStrError { /// Returns a description for this error, like `std::error::Error::description`. #[inline] #[must_use] + #[allow(clippy::unused_self)] pub const fn description(&self) -> &'static str { ERRORMSG_STR } @@ -1051,7 +1053,7 @@ impl AsAsciiStr for [AsciiChar] { #[inline] fn get_ascii(&self, index: usize) -> Option { - self.get(index).cloned() + self.get(index).copied() } } impl AsMutAsciiStr for [AsciiChar] { @@ -1173,7 +1175,7 @@ impl AsMutAsciiStr for str { // Valid ascii slice Some(slice) if slice.is_ascii() => { // SAFETY: All bytes are ascii, so this cast is valid - let ptr = slice.as_mut_ptr() as *mut AsciiChar; + let ptr = slice.as_mut_ptr().cast::(); let len = slice.len(); // SAFETY: The pointer is valid for `len` elements, as it came @@ -1249,8 +1251,8 @@ mod tests { let ascii_str = arr.as_ref().into(); let mut mut_arr = arr; // Note: We need a second copy to prevent overlapping mutable borrows. let mut_ascii_str = mut_arr.as_mut().into(); - let mut_arr_mut_ref: &mut [AsciiChar] = &mut [AsciiChar::A]; - let mut string_bytes = [b'A']; + let mut_arr_mut_ref: &mut [AsciiChar] = &mut [AsciiChar::A]; + let mut string_bytes = [b'A']; let string_mut = unsafe { core::str::from_utf8_unchecked_mut(&mut string_bytes) }; // SAFETY: 'A' is a valid string. let string_mut_bytes: &mut [u8] = &mut [b'A']; @@ -1374,6 +1376,7 @@ mod tests { } #[test] + #[allow(clippy::redundant_slicing)] fn index() { let mut arr = [AsciiChar::A, AsciiChar::B, AsciiChar::C, AsciiChar::D]; { @@ -1442,7 +1445,7 @@ mod tests { b'h', b'e', b'l', b'l', b'o', b' ', b'w', b'o', b'r', b'l', b'd', b'\0', ]; let ascii = AsciiStr::from_ascii(chars).unwrap(); - for (achar, byte) in ascii.chars().zip(chars.iter().cloned()) { + for (achar, byte) in ascii.chars().zip(chars.iter().copied()) { assert_eq!(achar, byte); } } diff --git a/src/ascii_string.rs b/src/ascii_string.rs index f2296b8..14c3bbb 100644 --- a/src/ascii_string.rs +++ b/src/ascii_string.rs @@ -116,7 +116,7 @@ impl AsciiString { { let mut bytes = bytes.into(); // SAFETY: The caller guarantees all bytes are valid ascii bytes. - let ptr = bytes.as_mut_ptr() as *mut AsciiChar; + let ptr = bytes.as_mut_ptr().cast::(); let length = bytes.len(); let capacity = bytes.capacity(); mem::forget(bytes); @@ -169,7 +169,7 @@ impl AsciiString { /// ``` #[inline] pub fn push_str(&mut self, string: &AsciiStr) { - self.vec.extend(string.chars()) + self.vec.extend(string.chars()); } /// Returns the number of bytes that this ASCII string buffer can hold without reallocating. @@ -201,7 +201,7 @@ impl AsciiString { /// ``` #[inline] pub fn reserve(&mut self, additional: usize) { - self.vec.reserve(additional) + self.vec.reserve(additional); } /// Reserves the minimum capacity for exactly `additional` more bytes to be inserted in the @@ -224,7 +224,7 @@ impl AsciiString { #[inline] pub fn reserve_exact(&mut self, additional: usize) { - self.vec.reserve_exact(additional) + self.vec.reserve_exact(additional); } /// Shrinks the capacity of this ASCII string buffer to match it's length. @@ -242,7 +242,7 @@ impl AsciiString { #[inline] pub fn shrink_to_fit(&mut self) { - self.vec.shrink_to_fit() + self.vec.shrink_to_fit(); } /// Adds the given ASCII character to the end of the ASCII string. @@ -259,7 +259,7 @@ impl AsciiString { #[inline] pub fn push(&mut self, ch: AsciiChar) { - self.vec.push(ch) + self.vec.push(ch); } /// Shortens a ASCII string to the specified length. @@ -277,7 +277,7 @@ impl AsciiString { #[inline] pub fn truncate(&mut self, new_len: usize) { - self.vec.truncate(new_len) + self.vec.truncate(new_len); } /// Removes the last character from the ASCII string buffer and returns it. @@ -338,7 +338,7 @@ impl AsciiString { #[inline] pub fn insert(&mut self, idx: usize, ch: AsciiChar) { - self.vec.insert(idx, ch) + self.vec.insert(idx, ch); } /// Returns the number of bytes in this ASCII string. @@ -383,7 +383,7 @@ impl AsciiString { #[inline] pub fn clear(&mut self) { - self.vec.clear() + self.vec.clear(); } /// Converts this [`AsciiString`] into a [`Box`]`<`[`AsciiStr`]`>`. @@ -391,6 +391,7 @@ impl AsciiString { /// This will drop any excess capacity #[cfg(feature = "alloc")] #[inline] + #[must_use] pub fn into_boxed_ascii_str(self) -> Box { let slice = self.vec.into_boxed_slice(); Box::from(slice) @@ -471,15 +472,17 @@ impl From> for AsciiString { impl From for AsciiString { #[inline] fn from(ch: AsciiChar) -> Self { - AsciiString {vec: vec![ch]} + AsciiString { vec: vec![ch] } } } +// FIXME? Turn this into a `From` impl +#[allow(clippy::from_over_into)] impl Into> for AsciiString { fn into(mut self) -> Vec { // SAFETY: All ascii bytes are valid `u8`, as we are `repr(u8)`. // Note: We forget `self` to avoid `self.vec` from being deallocated. - let ptr = self.vec.as_mut_ptr() as *mut u8; + let ptr = self.vec.as_mut_ptr().cast::(); let length = self.vec.len(); let capacity = self.vec.capacity(); mem::forget(self); @@ -501,10 +504,12 @@ impl<'a> From<&'a AsciiStr> for AsciiString { impl<'a> From<&'a [AsciiChar]> for AsciiString { #[inline] fn from(s: &'a [AsciiChar]) -> AsciiString { - s.iter().cloned().collect() + s.iter().copied().collect() } } +// FIXME? Turn this into a `From` impl +#[allow(clippy::from_over_into)] impl Into for AsciiString { #[inline] fn into(self) -> String { @@ -647,7 +652,7 @@ impl> Extend for AsciiString { let (lower_bound, _) = iterator.size_hint(); self.reserve(lower_bound); for item in iterator { - self.push_str(item.as_ref()) + self.push_str(item.as_ref()); } } } @@ -880,7 +885,7 @@ impl<'a> IntoAsciiString for &'a CStr { .map_err(|FromAsciiError { error, owner }| FromAsciiError { // SAFETY: We don't discard the NULL byte from the original // string, so we ensure that it's null terminated - owner: unsafe { CStr::from_ptr(owner.as_ptr() as *const _) }, + owner: unsafe { CStr::from_ptr(owner.as_ptr().cast()) }, error, }) .map(|mut s| { @@ -935,7 +940,7 @@ mod tests { use alloc::vec::Vec; #[cfg(feature = "std")] use std::ffi::CString; - use ::{AsciiChar, AsciiStr}; + use {AsciiChar, AsciiStr}; #[test] fn into_string() { @@ -946,7 +951,7 @@ mod tests { #[test] fn into_bytes() { let v = AsciiString::from_ascii(&[40_u8, 32, 59][..]).unwrap(); - assert_eq!(Into::>::into(v), vec![40_u8, 32, 59]) + assert_eq!(Into::>::into(v), vec![40_u8, 32, 59]); } #[test] diff --git a/src/free_functions.rs b/src/free_functions.rs index 42f355f..55d9732 100644 --- a/src/free_functions.rs +++ b/src/free_functions.rs @@ -17,7 +17,7 @@ use ascii_char::{AsciiChar, ToAsciiChar}; pub fn caret_encode>(c: C) -> Option { // The formula is explained in the Wikipedia article. let c = c.into() ^ 0b0100_0000; - if c >= b'?' && c <= b'_' { + if (b'?'..=b'_').contains(&c) { // SAFETY: All bytes between '?' (0x3F) and '_' (0x5f) are valid ascii characters. Some(unsafe { c.to_ascii_char_unchecked() }) } else { diff --git a/src/serialization/ascii_char.rs b/src/serialization/ascii_char.rs index 8f92248..9fd843a 100644 --- a/src/serialization/ascii_char.rs +++ b/src/serialization/ascii_char.rs @@ -60,8 +60,8 @@ mod tests { #[test] fn basic() { fn assert_serialize() {} - assert_serialize::(); fn assert_deserialize<'de, T: Deserialize<'de>>() {} + assert_serialize::(); assert_deserialize::(); } diff --git a/src/serialization/ascii_str.rs b/src/serialization/ascii_str.rs index edb3c7a..9aa9353 100644 --- a/src/serialization/ascii_str.rs +++ b/src/serialization/ascii_str.rs @@ -52,8 +52,8 @@ mod tests { #[test] fn basic() { fn assert_serialize() {} - assert_serialize::<&AsciiStr>(); fn assert_deserialize<'de, T: Deserialize<'de>>() {} + assert_serialize::<&AsciiStr>(); assert_deserialize::<&AsciiStr>(); } diff --git a/src/serialization/ascii_string.rs b/src/serialization/ascii_string.rs index b7267fd..1547655 100644 --- a/src/serialization/ascii_string.rs +++ b/src/serialization/ascii_string.rs @@ -32,7 +32,7 @@ impl<'de> Visitor<'de> for AsciiStringVisitor { } fn visit_bytes(self, v: &[u8]) -> Result { - AsciiString::from_ascii(v).map_err(|_| Error::invalid_value(Unexpected::Bytes(&v), &self)) + AsciiString::from_ascii(v).map_err(|_| Error::invalid_value(Unexpected::Bytes(v), &self)) } fn visit_byte_buf(self, v: Vec) -> Result { @@ -117,8 +117,8 @@ mod tests { #[test] fn basic() { fn assert_serialize() {} - assert_serialize::(); fn assert_deserialize<'de, T: Deserialize<'de>>() {} + assert_serialize::(); assert_deserialize::(); } diff --git a/tests.rs b/tests.rs index 3454830..017a7be 100644 --- a/tests.rs +++ b/tests.rs @@ -94,6 +94,7 @@ fn compare_ascii_str_str() { } #[test] +#[allow(clippy::redundant_slicing)] fn compare_ascii_str_slice() { let b = b"abc".as_ascii_str().unwrap(); let c = b"ab".as_ascii_str().unwrap();