-
-
Notifications
You must be signed in to change notification settings - Fork 61
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
Address clippy lints #160
Address clippy lints #160
Changes from all commits
a3e76e6
8e629d5
ab08a45
584674d
f46caa1
9843ef7
6a367f6
d9d7b88
5627aeb
948142f
14db391
9b7e161
906eb4b
5b379ed
8bd170b
76f590d
246b823
7b16270
39a749e
af7dba9
5c7c495
319b31a
9800151
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
use core::mem; | ||
|
||
#[cfg(feature = "alloc")] | ||
use alloc::boxed::Box; | ||
|
||
|
@@ -29,7 +27,6 @@ use alloc::boxed::Box; | |
/// The `Display` implementation behaves as if `BStr` were first lossily | ||
/// converted to a `str`. Invalid UTF-8 bytes are substituted with the Unicode | ||
/// replacement codepoint, which looks like this: �. | ||
#[derive(Hash)] | ||
#[repr(transparent)] | ||
pub struct BStr { | ||
pub(crate) bytes: [u8], | ||
|
@@ -60,7 +57,7 @@ impl BStr { | |
/// assert_eq!(a, c); | ||
/// ``` | ||
#[inline] | ||
pub fn new<'a, B: ?Sized + AsRef<[u8]>>(bytes: &'a B) -> &'a BStr { | ||
pub fn new<B: ?Sized + AsRef<[u8]>>(bytes: &B) -> &BStr { | ||
BStr::from_bytes(bytes.as_ref()) | ||
} | ||
|
||
|
@@ -73,12 +70,12 @@ impl BStr { | |
|
||
#[inline] | ||
pub(crate) fn from_bytes(slice: &[u8]) -> &BStr { | ||
unsafe { mem::transmute(slice) } | ||
unsafe { &*(slice as *const [u8] as *const BStr) } | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn from_bytes_mut(slice: &mut [u8]) -> &mut BStr { | ||
unsafe { mem::transmute(slice) } | ||
unsafe { &mut *(slice as *mut [u8] as *mut BStr) } | ||
Comment on lines
-76
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getting rid of the transmute seems like a win here. This cast soup could probably use a SAFETY comment that refers to the fact that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is a clippy restriction lint that can be used to enforce that all |
||
} | ||
|
||
#[inline] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,7 +72,7 @@ use crate::{ | |
/// string literals. This can be quite convenient! | ||
#[allow(non_snake_case)] | ||
#[inline] | ||
pub fn B<'a, B: ?Sized + AsRef<[u8]>>(bytes: &'a B) -> &'a [u8] { | ||
pub fn B<B: ?Sized + AsRef<[u8]>>(bytes: &B) -> &[u8] { | ||
bytes.as_ref() | ||
} | ||
|
||
|
@@ -3045,7 +3045,7 @@ pub trait ByteSlice: private::Sealed { | |
#[inline] | ||
fn last_byte(&self) -> Option<u8> { | ||
let bytes = self.as_bytes(); | ||
bytes.get(bytes.len().saturating_sub(1)).map(|&b| b) | ||
bytes.last().copied() | ||
} | ||
|
||
/// Returns the index of the first non-ASCII byte in this byte string (if | ||
|
@@ -3331,7 +3331,7 @@ impl<'a> Iterator for Bytes<'a> { | |
|
||
#[inline] | ||
fn next(&mut self) -> Option<u8> { | ||
self.it.next().map(|&b| b) | ||
self.it.next().copied() | ||
} | ||
|
||
#[inline] | ||
|
@@ -3343,7 +3343,7 @@ impl<'a> Iterator for Bytes<'a> { | |
impl<'a> DoubleEndedIterator for Bytes<'a> { | ||
#[inline] | ||
fn next_back(&mut self) -> Option<u8> { | ||
self.it.next_back().map(|&b| b) | ||
self.it.next_back().copied() | ||
} | ||
} | ||
|
||
|
@@ -3374,7 +3374,7 @@ pub struct Fields<'a> { | |
#[cfg(feature = "unicode")] | ||
impl<'a> Fields<'a> { | ||
fn new(bytes: &'a [u8]) -> Fields<'a> { | ||
Fields { it: bytes.fields_with(|ch| ch.is_whitespace()) } | ||
Fields { it: bytes.fields_with(char::is_whitespace) } | ||
} | ||
} | ||
|
||
|
@@ -3428,7 +3428,7 @@ impl<'a, F: FnMut(char) -> bool> Iterator for FieldsWith<'a, F> { | |
} | ||
} | ||
} | ||
while let Some((_, e, ch)) = self.chars.next() { | ||
for (_, e, ch) in self.chars.by_ref() { | ||
if (self.f)(ch) { | ||
break; | ||
} | ||
|
@@ -3744,7 +3744,7 @@ impl<'a> Iterator for LinesWithTerminator<'a> { | |
Some(line) | ||
} | ||
Some(end) => { | ||
let line = &self.bytes[..end + 1]; | ||
let line = &self.bytes[..=end]; | ||
self.bytes = &self.bytes[end + 1..]; | ||
Comment on lines
-3747
to
3748
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would probably replace this suggestion and the code with a call to |
||
Some(line) | ||
} | ||
|
@@ -3764,7 +3764,7 @@ impl<'a> DoubleEndedIterator for LinesWithTerminator<'a> { | |
} | ||
Some(end) => { | ||
let line = &self.bytes[end + 1..]; | ||
self.bytes = &self.bytes[..end + 1]; | ||
self.bytes = &self.bytes[..=end]; | ||
Comment on lines
3766
to
+3767
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
Some(line) | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -188,7 +188,7 @@ pub trait ByteVec: private::Sealed { | |
fn imp(os_str: OsString) -> Result<Vec<u8>, OsString> { | ||
use std::os::unix::ffi::OsStringExt; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this code could be made to support |
||
|
||
Ok(Vec::from(os_str.into_vec())) | ||
Ok(os_str.into_vec()) | ||
} | ||
|
||
#[cfg(not(unix))] | ||
|
@@ -223,18 +223,18 @@ pub trait ByteVec: private::Sealed { | |
/// ``` | ||
#[inline] | ||
#[cfg(feature = "std")] | ||
fn from_os_str_lossy<'a>(os_str: &'a OsStr) -> Cow<'a, [u8]> { | ||
fn from_os_str_lossy(os_str: &OsStr) -> Cow<'_, [u8]> { | ||
#[cfg(unix)] | ||
#[inline] | ||
fn imp<'a>(os_str: &'a OsStr) -> Cow<'a, [u8]> { | ||
fn imp(os_str: &OsStr) -> Cow<'_, [u8]> { | ||
use std::os::unix::ffi::OsStrExt; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto, |
||
|
||
Cow::Borrowed(os_str.as_bytes()) | ||
} | ||
|
||
#[cfg(not(unix))] | ||
#[inline] | ||
fn imp<'a>(os_str: &'a OsStr) -> Cow<'a, [u8]> { | ||
fn imp(os_str: &OsStr) -> Cow<'_, [u8]> { | ||
match os_str.to_string_lossy() { | ||
Cow::Borrowed(x) => Cow::Borrowed(x.as_bytes()), | ||
Cow::Owned(x) => Cow::Owned(Vec::from(x)), | ||
|
@@ -292,7 +292,7 @@ pub trait ByteVec: private::Sealed { | |
/// ``` | ||
#[inline] | ||
#[cfg(feature = "std")] | ||
fn from_path_lossy<'a>(path: &'a Path) -> Cow<'a, [u8]> { | ||
fn from_path_lossy(path: &Path) -> Cow<'_, [u8]> { | ||
Vec::from_os_str_lossy(path.as_os_str()) | ||
} | ||
|
||
|
@@ -961,7 +961,7 @@ pub trait ByteVec: private::Sealed { | |
R: ops::RangeBounds<usize>, | ||
B: AsRef<[u8]>, | ||
{ | ||
self.as_vec_mut().splice(range, replace_with.as_ref().iter().cloned()); | ||
self.as_vec_mut().splice(range, replace_with.as_ref().iter().copied()); | ||
} | ||
|
||
/// Creates a draining iterator that removes the specified range in this | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,7 +62,7 @@ macro_rules! impl_partial_ord { | |
#[cfg(feature = "alloc")] | ||
mod bstring { | ||
use core::{ | ||
cmp::Ordering, convert::TryFrom, fmt, iter::FromIterator, ops, | ||
cmp::Ordering, convert::TryFrom, fmt, hash, iter::FromIterator, ops, | ||
}; | ||
|
||
use alloc::{ | ||
|
@@ -342,7 +342,7 @@ mod bstring { | |
impl PartialEq for BString { | ||
#[inline] | ||
fn eq(&self, other: &BString) -> bool { | ||
&self[..] == &other[..] | ||
self[..] == other[..] | ||
} | ||
} | ||
|
||
|
@@ -355,6 +355,13 @@ mod bstring { | |
impl_partial_eq!(BString, BStr); | ||
impl_partial_eq!(BString, &'a BStr); | ||
|
||
impl hash::Hash for BString { | ||
#[inline] | ||
fn hash<H: hash::Hasher>(&self, state: &mut H) { | ||
self.as_bytes().hash(state); | ||
} | ||
} | ||
|
||
impl PartialOrd for BString { | ||
#[inline] | ||
fn partial_cmp(&self, other: &BString) -> Option<Ordering> { | ||
|
@@ -384,7 +391,7 @@ mod bstr { | |
borrow::{Borrow, BorrowMut}, | ||
cmp::Ordering, | ||
convert::TryFrom, | ||
fmt, ops, | ||
fmt, hash, ops, | ||
}; | ||
|
||
#[cfg(feature = "alloc")] | ||
|
@@ -479,7 +486,10 @@ mod bstr { | |
| '\x7f' => { | ||
write!(f, "\\x{:02x}", ch as u32)?; | ||
} | ||
'\n' | '\r' | '\t' | _ => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with this change, I'd probably want to add a comment on why newline, carriage return, and tab are called out specially here to get their own match arm. |
||
'\n' | '\r' | '\t' => { | ||
write!(f, "{}", ch.escape_debug())?; | ||
} | ||
_ => { | ||
write!(f, "{}", ch.escape_debug())?; | ||
} | ||
} | ||
|
@@ -814,6 +824,13 @@ mod bstr { | |
#[cfg(feature = "alloc")] | ||
impl_partial_eq_cow!(&'a BStr, Cow<'a, [u8]>); | ||
|
||
impl hash::Hash for BStr { | ||
#[inline] | ||
fn hash<H: hash::Hasher>(&self, state: &mut H) { | ||
self.as_bytes().hash(state); | ||
} | ||
} | ||
|
||
impl PartialOrd for BStr { | ||
#[inline] | ||
fn partial_cmp(&self, other: &BStr) -> Option<Ordering> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#![allow(clippy::single_char_add_str)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there are several places in the crate where the replacement character is used in a single character string. I prefer suppressing this because it means you don't pay the cost for a UTF-8 encode at runtime. |
||
|
||
/*! | ||
A byte string library. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#![allow(clippy::all)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suppressed all lints because these modules appear to be generated code. clippy didn't like the use of the |
||
|
||
pub mod grapheme_break_fwd; | ||
pub mod grapheme_break_rev; | ||
pub mod regional_indicator_rev; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these two functions can probably be nuked and the routines from
core
slotted into place where they are used.I believe these two functions on the pointer primitive were added during the great pointer reform of
std
.