Skip to content

Commit

Permalink
removes all #[must_use], allows clippy::must_use_candidate
Browse files Browse the repository at this point in the history
  • Loading branch information
chanced committed Jul 1, 2024
1 parent 8991015 commit 9f95d72
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 9 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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 { .. })
}
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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()
}
Expand Down Expand Up @@ -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
}
Expand Down
40 changes: 20 additions & 20 deletions src/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<str>("") as *const Self) }
}
Expand Down Expand Up @@ -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::<str>(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 '/'
Expand All @@ -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<Token> {
self.0
.rsplit_once('/')
Expand All @@ -145,13 +145,13 @@ impl Pointer {
/// Returns the last token in the `Pointer`.
///
/// alias for `back`
#[must_use]
pub fn last(&self) -> Option<Token> {
self.back()
}

/// Returns the first `Token` in the `Pointer`.
#[must_use]
pub fn front(&self) -> Option<Token> {
if self.is_root() {
return None;
Expand All @@ -168,13 +168,13 @@ impl Pointer {
/// Returns the first `Token` in the `Pointer`.
///
/// alias for `front`
#[must_use]
pub fn first(&self) -> Option<Token> {
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;
Expand Down Expand Up @@ -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;
Expand All @@ -225,15 +225,15 @@ 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('/')
.map(|(front, back)| (Self::new(front), Token::from_encoded_unchecked(back)))
}

/// 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))
}
Expand All @@ -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<Token> {
self.tokens().nth(index).clone()
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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())
}
Expand All @@ -614,7 +614,7 @@ impl PointerBuf {
}

/// Coerces to a Pointer slice.
#[must_use]
pub fn as_ptr(&self) -> &Pointer {
self
}
Expand Down
10 changes: 5 additions & 5 deletions src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, .. }
Expand All @@ -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 { .. })
}
Expand Down
8 changes: 4 additions & 4 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand All @@ -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()),
Expand All @@ -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
}
Expand All @@ -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();
Expand Down

0 comments on commit 9f95d72

Please sign in to comment.