From 9f95d727547a05ae6c1eb5f1f3138672a7e8ba57 Mon Sep 17 00:00:00 2001 From: Chance Date: Mon, 1 Jul 2024 10:17:30 -0400 Subject: [PATCH] removes all `#[must_use]`, allows `clippy::must_use_candidate` --- src/index.rs | 2 +- src/lib.rs | 17 +++++++++-------- src/pointer.rs | 40 ++++++++++++++++++++-------------------- src/resolve.rs | 10 +++++----- src/token.rs | 8 ++++---- 5 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/index.rs b/src/index.rs index a1e3559..2d770f2 100644 --- a/src/index.rs +++ b/src/index.rs @@ -128,7 +128,7 @@ impl Index { /// assert_eq!(Index::Num(42).for_len_unchecked(30), 42); /// assert_eq!(Index::Next.for_len_unchecked(30), 30); /// ```` - #[must_use] + pub fn for_len_unchecked(&self, length: usize) -> usize { match *self { Self::Num(idx) => idx, diff --git a/src/lib.rs b/src/lib.rs index a394803..bcf47bf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,13 @@ #![doc = include_str!("../README.md")] #![warn(missing_docs)] -#![warn(clippy::all, clippy::pedantic)] +#![deny(clippy::all, clippy::pedantic)] #![cfg_attr(not(feature = "std"), no_std)] #![allow( clippy::module_name_repetitions, clippy::into_iter_without_iter, clippy::needless_pass_by_value, - clippy::expect_fun_call + clippy::expect_fun_call, + clippy::must_use_candidate )] #[cfg_attr(not(feature = "std"), macro_use)] @@ -89,14 +90,14 @@ impl fmt::Display for ParseError { impl ParseError { /// Returns `true` if this error is `NoLeadingBackslash`; otherwise returns /// `false`. - #[must_use] + pub fn is_no_leading_backslash(&self) -> bool { matches!(self, Self::NoLeadingBackslash { .. }) } /// Returns `true` if this error is `InvalidEncoding`; otherwise returns /// `false`. - #[must_use] + pub fn is_invalid_encoding(&self) -> bool { matches!(self, Self::InvalidEncoding { .. }) } @@ -113,7 +114,7 @@ impl ParseError { /// let err = PointerBuf::parse("/foo/invalid~tilde/invalid").unwrap_err(); /// assert_eq!(err.pointer_offset(), 4) /// ``` - #[must_use] + pub fn pointer_offset(&self) -> usize { match *self { Self::NoLeadingBackslash { .. } => 0, @@ -133,7 +134,7 @@ impl ParseError { /// let err = PointerBuf::parse("/foo/invalid~tilde/invalid").unwrap_err(); /// assert_eq!(err.source_offset(), 8) /// ``` - #[must_use] + pub fn source_offset(&self) -> usize { match self { Self::NoLeadingBackslash { .. } => 0, @@ -152,7 +153,7 @@ impl ParseError { /// let err = PointerBuf::parse("/foo/invalid~tilde/invalid").unwrap_err(); /// assert_eq!(err.pointer_offset(), 4) /// ``` - #[must_use] + pub fn complete_offset(&self) -> usize { self.source_offset() + self.pointer_offset() } @@ -225,7 +226,7 @@ pub struct InvalidEncodingError { impl InvalidEncodingError { /// The byte offset of the first invalid `~`. - #[must_use] + pub fn offset(&self) -> usize { self.offset } diff --git a/src/pointer.rs b/src/pointer.rs index 45d0674..f42cd2f 100644 --- a/src/pointer.rs +++ b/src/pointer.rs @@ -56,7 +56,7 @@ impl Pointer { } /// Constant reference to a root pointer - #[must_use] + pub const fn root() -> &'static Self { unsafe { &*(core::ptr::from_ref::("") as *const Self) } } @@ -88,26 +88,26 @@ impl Pointer { /// let bar = data.resolve(POINTER).unwrap(); /// assert_eq!(bar, "baz"); /// ```` - #[must_use] + pub const fn from_static(s: &'static str) -> &'static Self { assert!(validate(s).is_ok(), "invalid json pointer"); unsafe { &*(core::ptr::from_ref::(s) as *const Self) } } /// The encoded string representation of this `Pointer` - #[must_use] + pub fn as_str(&self) -> &str { &self.0 } /// Converts into an owned [`PointerBuf`] - #[must_use] + pub fn to_buf(&self) -> PointerBuf { PointerBuf(self.0.to_string()) } /// Returns an iterator of `Token`s in the `Pointer`. - #[must_use] + pub fn tokens(&self) -> Tokens { let mut s = self.0.split('/'); // skipping the first '/' @@ -116,26 +116,26 @@ impl Pointer { } /// Returns the number of tokens in the `Pointer`. - #[must_use] + pub fn count(&self) -> usize { self.tokens().count() } /// Returns `true` if the JSON Pointer equals `""`. - #[must_use] + pub fn is_root(&self) -> bool { self.0.is_empty() } /// Returns a `serde_json::Value` representation of this `Pointer` - #[must_use] + #[cfg(feature = "json")] pub fn to_json_value(&self) -> serde_json::Value { serde_json::Value::String(self.0.to_string()) } /// Returns the last `Token` in the `Pointer`. - #[must_use] + pub fn back(&self) -> Option { self.0 .rsplit_once('/') @@ -145,13 +145,13 @@ impl Pointer { /// Returns the last token in the `Pointer`. /// /// alias for `back` - #[must_use] + pub fn last(&self) -> Option { self.back() } /// Returns the first `Token` in the `Pointer`. - #[must_use] + pub fn front(&self) -> Option { if self.is_root() { return None; @@ -168,13 +168,13 @@ impl Pointer { /// Returns the first `Token` in the `Pointer`. /// /// alias for `front` - #[must_use] + pub fn first(&self) -> Option { self.front() } /// Splits the `Pointer` into the first `Token` and a remainder `Pointer`. - #[must_use] + pub fn split_front(&self) -> Option<(Token, &Self)> { if self.is_root() { return None; @@ -215,7 +215,7 @@ impl Pointer { /// assert_eq!(tail, Pointer::from_static("/bar/baz")); /// assert_eq!(ptr.split_at(3), None); /// ``` - #[must_use] + pub fn split_at(&self, idx: usize) -> Option<(&Self, &Self)> { if self.0.as_bytes().get(idx).copied() != Some(b'/') { return None; @@ -225,7 +225,7 @@ impl Pointer { } /// Splits the `Pointer` into the parent path and the last `Token`. - #[must_use] + pub fn split_back(&self) -> Option<(&Self, Token)> { self.0 .rsplit_once('/') @@ -233,7 +233,7 @@ impl Pointer { } /// A pointer to the parent of the current path. - #[must_use] + pub fn parent(&self) -> Option<&Self> { self.0.rsplit_once('/').map(|(front, _)| Self::new(front)) } @@ -258,7 +258,7 @@ impl Pointer { /// let ptr = Pointer::root(); /// assert_eq!(ptr.get(0), None); /// ``` - #[must_use] + pub fn get(&self, index: usize) -> Option { self.tokens().nth(index).clone() } @@ -309,7 +309,7 @@ impl Pointer { } /// Finds the commonality between this and another `Pointer`. - #[must_use] + pub fn intersection<'a>(&'a self, other: &Self) -> &'a Self { if self.is_root() || other.is_root() { return Self::root(); @@ -587,7 +587,7 @@ pub struct PointerBuf(String); impl PointerBuf { /// Creates a new `PointerBuf` pointing to a document root. - #[must_use] + pub fn new() -> Self { Self(String::new()) } @@ -614,7 +614,7 @@ impl PointerBuf { } /// Coerces to a Pointer slice. - #[must_use] + pub fn as_ptr(&self) -> &Pointer { self } diff --git a/src/resolve.rs b/src/resolve.rs index e6c402a..486a458 100644 --- a/src/resolve.rs +++ b/src/resolve.rs @@ -156,7 +156,7 @@ pub enum ResolveError { impl ResolveError { /// Offset of the partial pointer starting with the token which caused the /// error. - #[must_use] + pub fn offset(&self) -> usize { match self { Self::FailedToParseIndex { offset, .. } @@ -168,28 +168,28 @@ impl ResolveError { /// Returns `true` if this error is `FailedToParseIndex`; otherwise returns /// `false`. - #[must_use] + pub fn is_unreachable(&self) -> bool { matches!(self, Self::Unreachable { .. }) } /// Returns `true` if this error is `FailedToParseIndex`; otherwise returns /// `false`. - #[must_use] + pub fn is_not_found(&self) -> bool { matches!(self, Self::NotFound { .. }) } /// Returns `true` if this error is `FailedToParseIndex`; otherwise returns /// `false`. - #[must_use] + pub fn is_out_of_bounds(&self) -> bool { matches!(self, Self::OutOfBounds { .. }) } /// Returns `true` if this error is `FailedToParseIndex`; otherwise returns /// `false`. - #[must_use] + pub fn is_failed_to_parse_index(&self) -> bool { matches!(self, Self::FailedToParseIndex { .. }) } diff --git a/src/token.rs b/src/token.rs index f6cf166..5c5cb72 100644 --- a/src/token.rs +++ b/src/token.rs @@ -143,7 +143,7 @@ impl<'a> Token<'a> { /// /// If the token is not already owned, this will clone the referenced string /// slice. - #[must_use] + pub fn into_owned(self) -> Token<'static> { Token { inner: Cow::Owned(self.inner.into_owned()), @@ -157,7 +157,7 @@ impl<'a> Token<'a> { /// /// This method is like [`Self::into_owned`], except it doesn't take /// ownership of the original `Token`. - #[must_use] + pub fn to_owned(&self) -> Token<'static> { Token { inner: Cow::Owned(self.inner.clone().into_owned()), @@ -172,7 +172,7 @@ impl<'a> Token<'a> { /// # use jsonptr::Token; /// assert_eq!(Token::new("~bar").encoded(), "~0bar"); /// ``` - #[must_use] + pub fn encoded(&self) -> &str { &self.inner } @@ -185,7 +185,7 @@ impl<'a> Token<'a> { /// # use jsonptr::Token; /// assert_eq!(Token::new("~bar").decoded(), "~bar"); /// ``` - #[must_use] + pub fn decoded(&self) -> Cow<'_, str> { if let Some(i) = self.inner.bytes().position(|b| b == ENC_PREFIX) { let input = self.inner.as_bytes();