Skip to content

Fixed failing Clippy lints #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/ascii_char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
21 changes: 12 additions & 9 deletions src/ascii_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,19 +308,20 @@ impl AsciiStr {
#[inline]
#[must_use]
pub fn first(&self) -> Option<AsciiChar> {
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<AsciiChar> {
self.slice.last().cloned()
self.slice.last().copied()
}

/// Converts a [`Box<AsciiStr>`] into a [`AsciiString`] without copying or allocating.
#[cfg(feature = "alloc")]
#[inline]
#[must_use]
pub fn into_ascii_string(self: Box<Self>) -> AsciiString {
let slice = Box::<[AsciiChar]>::from(self);
AsciiString::from(slice.into_vec())
Expand Down Expand Up @@ -581,7 +582,7 @@ impl<'a> Iterator for Chars<'a> {
type Item = AsciiChar;
#[inline]
fn next(&mut self) -> Option<AsciiChar> {
self.0.next().cloned()
self.0.next().copied()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
Expand All @@ -590,7 +591,7 @@ impl<'a> Iterator for Chars<'a> {
impl<'a> DoubleEndedIterator for Chars<'a> {
#[inline]
fn next_back(&mut self) -> Option<AsciiChar> {
self.0.next_back().cloned()
self.0.next_back().copied()
}
}
impl<'a> ExactSizeIterator for Chars<'a> {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -1051,7 +1053,7 @@ impl AsAsciiStr for [AsciiChar] {

#[inline]
fn get_ascii(&self, index: usize) -> Option<AsciiChar> {
self.get(index).cloned()
self.get(index).copied()
}
}
impl AsMutAsciiStr for [AsciiChar] {
Expand Down Expand Up @@ -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::<AsciiChar>();
let len = slice.len();

// SAFETY: The pointer is valid for `len` elements, as it came
Expand Down Expand Up @@ -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'];

Expand Down Expand Up @@ -1374,6 +1376,7 @@ mod tests {
}

#[test]
#[allow(clippy::redundant_slicing)]
fn index() {
let mut arr = [AsciiChar::A, AsciiChar::B, AsciiChar::C, AsciiChar::D];
{
Expand Down Expand Up @@ -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);
}
}
Expand Down
37 changes: 21 additions & 16 deletions src/ascii_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<AsciiChar>();
let length = bytes.len();
let capacity = bytes.capacity();
mem::forget(bytes);
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -383,14 +383,15 @@ impl AsciiString {
#[inline]

pub fn clear(&mut self) {
self.vec.clear()
self.vec.clear();
}

/// Converts this [`AsciiString`] into a [`Box`]`<`[`AsciiStr`]`>`.
///
/// This will drop any excess capacity
#[cfg(feature = "alloc")]
#[inline]
#[must_use]
pub fn into_boxed_ascii_str(self) -> Box<AsciiStr> {
let slice = self.vec.into_boxed_slice();
Box::from(slice)
Expand Down Expand Up @@ -471,15 +472,17 @@ impl From<Vec<AsciiChar>> for AsciiString {
impl From<AsciiChar> 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<Vec<u8>> for AsciiString {
fn into(mut self) -> Vec<u8> {
// 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::<u8>();
let length = self.vec.len();
let capacity = self.vec.capacity();
mem::forget(self);
Expand All @@ -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<String> for AsciiString {
#[inline]
fn into(self) -> String {
Expand Down Expand Up @@ -647,7 +652,7 @@ impl<A: AsRef<AsciiStr>> Extend<A> 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());
}
}
}
Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -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() {
Expand All @@ -946,7 +951,7 @@ mod tests {
#[test]
fn into_bytes() {
let v = AsciiString::from_ascii(&[40_u8, 32, 59][..]).unwrap();
assert_eq!(Into::<Vec<u8>>::into(v), vec![40_u8, 32, 59])
assert_eq!(Into::<Vec<u8>>::into(v), vec![40_u8, 32, 59]);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/free_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use ascii_char::{AsciiChar, ToAsciiChar};
pub fn caret_encode<C: Copy + Into<u8>>(c: C) -> Option<AsciiChar> {
// 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 {
Expand Down
2 changes: 1 addition & 1 deletion src/serialization/ascii_char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ mod tests {
#[test]
fn basic() {
fn assert_serialize<T: Serialize>() {}
assert_serialize::<AsciiChar>();
fn assert_deserialize<'de, T: Deserialize<'de>>() {}
assert_serialize::<AsciiChar>();
assert_deserialize::<AsciiChar>();
}

Expand Down
2 changes: 1 addition & 1 deletion src/serialization/ascii_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ mod tests {
#[test]
fn basic() {
fn assert_serialize<T: Serialize>() {}
assert_serialize::<&AsciiStr>();
fn assert_deserialize<'de, T: Deserialize<'de>>() {}
assert_serialize::<&AsciiStr>();
assert_deserialize::<&AsciiStr>();
}

Expand Down
4 changes: 2 additions & 2 deletions src/serialization/ascii_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<'de> Visitor<'de> for AsciiStringVisitor {
}

fn visit_bytes<E: Error>(self, v: &[u8]) -> Result<Self::Value, E> {
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<E: Error>(self, v: Vec<u8>) -> Result<Self::Value, E> {
Expand Down Expand Up @@ -117,8 +117,8 @@ mod tests {
#[test]
fn basic() {
fn assert_serialize<T: Serialize>() {}
assert_serialize::<AsciiString>();
fn assert_deserialize<'de, T: Deserialize<'de>>() {}
assert_serialize::<AsciiString>();
assert_deserialize::<AsciiString>();
}

Expand Down
1 change: 1 addition & 0 deletions tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down