Skip to content

Remove #[allow(clippy::result_unit_err)] #539

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

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Changed `stable_deref_trait` to a platform-dependent dependency.
- Changed `SortedLinkedList::pop` return type from `Result<T, ()>` to `Option<T>` to match `std::vec::pop`.
- `Vec::capacity` is no longer a `const` function.
- Changed `String::push_str` return type to `Option<()>`.
- Changed `String::push` return type to `Option<()>`.
- Changed `Vec::from_slice` return type to `Option<Self>`.
- Changed `Vec::extend_from_slice` return type to `Option<()>`.
- Changed `Vec::resize` return type to `Option<()>`.
- Changed `Vec::resize_defualt` return type to `Option<()>`.
- Changed `SortedLinkedList::rpop` return type to `Option<T>`.

### Fixed

Expand Down
4 changes: 2 additions & 2 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ impl<'de, const N: usize> Deserialize<'de> for String<N> {
{
let mut s = String::new();
s.push_str(v)
.map_err(|_| E::invalid_length(v.len(), &self))?;
.ok_or_else(|| E::invalid_length(v.len(), &self))?;
Ok(s)
}

Expand All @@ -332,7 +332,7 @@ impl<'de, const N: usize> Deserialize<'de> for String<N> {
core::str::from_utf8(v)
.map_err(|_| E::invalid_value(de::Unexpected::Bytes(v), &self))?,
)
.map_err(|_| E::invalid_length(v.len(), &self))?;
.ok_or_else(|| E::invalid_length(v.len(), &self))?;

Ok(s)
}
Expand Down
34 changes: 16 additions & 18 deletions src/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl<const N: usize> String<N> {
for c in char::decode_utf16(v.iter().cloned()) {
match c {
Ok(c) => {
s.push(c).map_err(|_| FromUtf16Error::Capacity)?;
s.push(c).ok_or(FromUtf16Error::Capacity)?;
}
Err(err) => {
return Err(FromUtf16Error::DecodeUtf16Error(err));
Expand Down Expand Up @@ -428,16 +428,15 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
///
/// let mut s: String<8> = String::try_from("foo")?;
///
/// assert!(s.push_str("bar").is_ok());
/// assert!(s.push_str("bar").is_some());
///
/// assert_eq!("foobar", s);
///
/// assert!(s.push_str("tender").is_err());
/// assert!(s.push_str("tender").is_none());
/// # Ok::<(), ()>(())
/// ```
#[inline]
#[allow(clippy::result_unit_err)]
pub fn push_str(&mut self, string: &str) -> Result<(), ()> {
pub fn push_str(&mut self, string: &str) -> Option<()> {
self.vec.extend_from_slice(string.as_bytes())
}

Expand Down Expand Up @@ -479,10 +478,9 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
/// # Ok::<(), ()>(())
/// ```
#[inline]
#[allow(clippy::result_unit_err)]
pub fn push(&mut self, c: char) -> Result<(), ()> {
pub fn push(&mut self, c: char) -> Option<()> {
match c.len_utf8() {
1 => self.vec.push(c as u8).map_err(|_| {}),
1 => self.vec.push(c as u8).ok(),
_ => self
.vec
.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes()),
Expand Down Expand Up @@ -633,7 +631,7 @@ impl<'a, const N: usize> TryFrom<&'a str> for String<N> {
type Error = ();
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
let mut new = Self::new();
new.push_str(s)?;
new.push_str(s).ok_or(())?;
Ok(new)
}
}
Expand All @@ -643,7 +641,7 @@ impl<const N: usize> str::FromStr for String<N> {

fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut new = Self::new();
new.push_str(s)?;
new.push_str(s).ok_or(())?;
Ok(new)
}
}
Expand Down Expand Up @@ -707,11 +705,11 @@ impl<S: VecStorage<u8> + ?Sized> hash::Hash for StringInner<S> {

impl<S: VecStorage<u8> + ?Sized> fmt::Write for StringInner<S> {
fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
self.push_str(s).map_err(|_| fmt::Error)
self.push_str(s).ok_or(fmt::Error)
}

fn write_char(&mut self, c: char) -> Result<(), fmt::Error> {
self.push(c).map_err(|_| fmt::Error)
self.push(c).ok_or(fmt::Error)
}
}

Expand Down Expand Up @@ -1051,21 +1049,21 @@ mod tests {
#[test]
fn push_str() {
let mut s: String<8> = String::try_from("foo").unwrap();
assert!(s.push_str("bar").is_ok());
assert!(s.push_str("bar").is_some());
assert_eq!("foobar", s);
assert_eq!(s, "foobar");
assert!(s.push_str("tender").is_err());
assert!(s.push_str("tender").is_none());
assert_eq!("foobar", s);
assert_eq!(s, "foobar");
}

#[test]
fn push() {
let mut s: String<6> = String::try_from("abc").unwrap();
assert!(s.push('1').is_ok());
assert!(s.push('2').is_ok());
assert!(s.push('3').is_ok());
assert!(s.push('4').is_err());
assert!(s.push('1').is_some());
assert!(s.push('2').is_some());
assert!(s.push('3').is_some());
assert!(s.push('4').is_none());
assert!("abc123" == s.as_str());
}

Expand Down
4 changes: 2 additions & 2 deletions src/ufmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use ufmt_write::uWrite;
impl<S: VecStorage<u8> + ?Sized> uWrite for StringInner<S> {
type Error = ();
fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
self.push_str(s)
self.push_str(s).ok_or_else(|| ())
}
}

impl<S: VecStorage<u8> + ?Sized> uWrite for VecInner<u8, S> {
type Error = ();
fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
self.extend_from_slice(s.as_bytes())
self.extend_from_slice(s.as_bytes()).ok_or_else(|| ())
}
}

Expand Down
39 changes: 16 additions & 23 deletions src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,13 @@ impl<T, const N: usize> Vec<T, N> {
/// let mut v: Vec<u8, 16> = Vec::new();
/// v.extend_from_slice(&[1, 2, 3]).unwrap();
/// ```
#[allow(clippy::result_unit_err)]
pub fn from_slice(other: &[T]) -> Result<Self, ()>
pub fn from_slice(other: &[T]) -> Option<Self>
where
T: Clone,
{
let mut v = Self::new();
v.extend_from_slice(other)?;
Ok(v)
Some(v)
}

/// Constructs a new vector with a fixed capacity of `N`, initializing
Expand Down Expand Up @@ -517,28 +516,27 @@ impl<T, S: VecStorage<T> + ?Sized> VecInner<T, S> {
/// vec.extend_from_slice(&[2, 3, 4]).unwrap();
/// assert_eq!(*vec, [1, 2, 3, 4]);
/// ```
#[allow(clippy::result_unit_err)]
pub fn extend_from_slice(&mut self, other: &[T]) -> Result<(), ()>
pub fn extend_from_slice(&mut self, other: &[T]) -> Option<()>
where
T: Clone,
{
pub fn extend_from_slice_inner<T>(
len: &mut usize,
buf: &mut [MaybeUninit<T>],
other: &[T],
) -> Result<(), ()>
) -> Option<()>
where
T: Clone,
{
if *len + other.len() > buf.len() {
// won't fit in the `Vec`; don't modify anything and return an error
Err(())
None
} else {
for elem in other {
unsafe { *buf.get_unchecked_mut(*len) = MaybeUninit::new(elem.clone()) }
*len += 1;
}
Ok(())
Some(())
}
}

Expand Down Expand Up @@ -627,13 +625,12 @@ impl<T, S: VecStorage<T> + ?Sized> VecInner<T, S> {
/// new_len is less than len, the Vec is simply truncated.
///
/// See also [`resize_default`](Self::resize_default).
#[allow(clippy::result_unit_err)]
pub fn resize(&mut self, new_len: usize, value: T) -> Result<(), ()>
pub fn resize(&mut self, new_len: usize, value: T) -> Option<()>
where
T: Clone,
{
if new_len > self.capacity() {
return Err(());
return None;
}

if new_len > self.len {
Expand All @@ -644,7 +641,7 @@ impl<T, S: VecStorage<T> + ?Sized> VecInner<T, S> {
self.truncate(new_len);
}

Ok(())
Some(())
}

/// Resizes the `Vec` in-place so that `len` is equal to `new_len`.
Expand All @@ -654,8 +651,7 @@ impl<T, S: VecStorage<T> + ?Sized> VecInner<T, S> {
/// If `new_len` is less than `len`, the `Vec` is simply truncated.
///
/// See also [`resize`](Self::resize).
#[allow(clippy::result_unit_err)]
pub fn resize_default(&mut self, new_len: usize) -> Result<(), ()>
pub fn resize_default(&mut self, new_len: usize) -> Option<()>
where
T: Clone + Default,
{
Expand Down Expand Up @@ -1189,10 +1185,7 @@ where

impl<S: VecStorage<u8> + ?Sized> fmt::Write for VecInner<u8, S> {
fn write_str(&mut self, s: &str) -> fmt::Result {
match self.extend_from_slice(s.as_bytes()) {
Ok(()) => Ok(()),
Err(_) => Err(fmt::Error),
}
self.extend_from_slice(s.as_bytes()).ok_or(fmt::Error)
}
}

Expand All @@ -1215,7 +1208,7 @@ impl<'a, T: Clone, const N: usize> TryFrom<&'a [T]> for Vec<T, N> {
type Error = ();

fn try_from(slice: &'a [T]) -> Result<Self, Self::Error> {
Self::from_slice(slice)
Self::from_slice(slice).ok_or(())
}
}

Expand Down Expand Up @@ -1322,7 +1315,7 @@ where
self.vec.len() - self.next,
)
};
vec.extend_from_slice(s).ok();
vec.extend_from_slice(s);
}

Self { vec, next: 0 }
Expand Down Expand Up @@ -1836,7 +1829,7 @@ mod tests {

v.resize(0, 0).unwrap();
v.resize(4, 0).unwrap();
v.resize(5, 0).expect_err("full");
assert!(v.resize(5, 0).is_none(), "full");
}

#[test]
Expand Down Expand Up @@ -1916,7 +1909,7 @@ mod tests {
v.extend_from_slice(&[3]).unwrap();
assert_eq!(v.len(), 3);
assert_eq!(v.as_slice(), &[1, 2, 3]);
assert!(v.extend_from_slice(&[4, 5]).is_err());
assert!(v.extend_from_slice(&[4, 5]).is_none());
assert_eq!(v.len(), 3);
assert_eq!(v.as_slice(), &[1, 2, 3]);
}
Expand All @@ -1929,7 +1922,7 @@ mod tests {
assert_eq!(v.as_slice(), &[1, 2, 3]);

// Slice too large
assert!(Vec::<u8, 2>::from_slice(&[1, 2, 3]).is_err());
assert!(Vec::<u8, 2>::from_slice(&[1, 2, 3]).is_none());
}

#[test]
Expand Down