Skip to content

set must_use attributes only to methods returning Self #7

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 2 commits into from
Apr 5, 2025
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ expect_used = "deny"
future_not_send = "allow"
if_not_else = "allow"
missing_const_for_fn = "allow"
#module_name_repetitions = "allow"
must_use_candidate = "allow"
option_if_let_else = "allow"
print_stderr = "deny"
print_stdout = "deny"
Expand Down
59 changes: 42 additions & 17 deletions src/expectations.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
//! Definitions of the expectations that are provided by this crate.

#![allow(missing_docs)]
#![warn(clippy::return_self_not_must_use)]

use crate::std::ops::RangeInclusive;
use crate::std::{string::String, vec::Vec};
use hashbrown::HashSet;

#[must_use]
pub struct IsTrue;

#[must_use]
pub struct IsFalse;

#[must_use]
pub struct IsEqualTo<E> {
pub expected: E,
}

#[must_use]
pub struct IsNotEqualTo<E> {
pub expected: E,
}

#[must_use]
pub struct IsCloseTo<E, M> {
pub expected: E,
pub margin: M,
Expand All @@ -27,7 +33,6 @@ impl<E, M> IsCloseTo<E, M>
where
M: Default,
{
#[must_use]
pub fn new(expected: E) -> Self {
Self {
expected,
Expand All @@ -37,13 +42,13 @@ where
}

impl<E, M> IsCloseTo<E, M> {
#[must_use]
pub fn within_margin(mut self, margin: impl Into<M>) -> Self {
self.margin = margin.into();
self
}
}

#[must_use]
pub struct IsNotCloseTo<E, M> {
pub expected: E,
pub margin: M,
Expand All @@ -53,7 +58,6 @@ impl<E, M> IsNotCloseTo<E, M>
where
M: Default,
{
#[must_use]
pub fn new(expected: E) -> Self {
Self {
expected,
Expand All @@ -63,93 +67,113 @@ where
}

impl<E, M> IsNotCloseTo<E, M> {
#[must_use]
pub fn within_margin(mut self, margin: impl Into<M>) -> Self {
self.margin = margin.into();
self
}
}

#[must_use]
pub struct IsLessThan<E> {
pub expected: E,
}

#[must_use]
pub struct IsAtMost<E> {
pub expected: E,
}

#[must_use]
pub struct IsGreaterThan<E> {
pub expected: E,
}

#[must_use]
pub struct IsAtLeast<E> {
pub expected: E,
}

#[must_use]
pub struct IsInRange<E> {
pub expected_range: RangeInclusive<E>,
}

#[must_use]
pub struct IsNotInRange<E> {
pub expected_range: RangeInclusive<E>,
}

#[must_use]
pub struct IsSome;

#[must_use]
pub struct IsNone;

#[must_use]
pub struct HasValue<E> {
pub expected: E,
}

#[must_use]
pub struct IsOk;

#[must_use]
pub struct IsErr;

#[must_use]
pub struct HasError<E> {
pub expected: E,
}

#[must_use]
pub struct IsEmpty;

#[must_use]
pub struct IsNotEmpty;

#[must_use]
pub struct HasLength<E> {
pub expected_length: E,
}

#[must_use]
pub struct HasLengthInRange<E> {
pub expected_range: RangeInclusive<E>,
}

#[must_use]
pub struct StringContains<E> {
pub expected: E,
}

#[must_use]
pub struct StringContainsAnyOf<E> {
pub expected: E,
}

#[must_use]
pub struct StringStartWith<E> {
pub expected: E,
}

#[must_use]
pub struct StringEndsWith<E> {
pub expected: E,
}

#[must_use]
pub struct IterContains<E> {
pub expected: E,
}

#[must_use]
pub struct IterContainsExactlyInAnyOrder<E> {
pub expected: Vec<E>,
pub missing: HashSet<usize>,
pub extra: HashSet<usize>,
}

impl<E> IterContainsExactlyInAnyOrder<E> {
#[must_use]
pub fn new(expected: Vec<E>) -> Self {
Self {
expected,
Expand All @@ -159,17 +183,18 @@ impl<E> IterContainsExactlyInAnyOrder<E> {
}
}

#[must_use]
pub struct IterContainsAnyOf<E> {
pub expected: Vec<E>,
}

#[must_use]
pub struct IterContainsAllOf<E> {
pub expected: Vec<E>,
pub missing: HashSet<usize>,
}

impl<E> IterContainsAllOf<E> {
#[must_use]
pub fn new(expected: Vec<E>) -> Self {
Self {
expected,
Expand All @@ -178,13 +203,13 @@ impl<E> IterContainsAllOf<E> {
}
}

#[must_use]
pub struct IterContainsOnly<E> {
pub expected: Vec<E>,
pub extra: HashSet<usize>,
}

impl<E> IterContainsOnly<E> {
#[must_use]
pub fn new(expected: Vec<E>) -> Self {
Self {
expected,
Expand All @@ -193,14 +218,14 @@ impl<E> IterContainsOnly<E> {
}
}

#[must_use]
pub struct IterContainsOnlyOnce<E> {
pub expected: Vec<E>,
pub extra: HashSet<usize>,
pub duplicates: HashSet<usize>,
}

impl<E> IterContainsOnlyOnce<E> {
#[must_use]
pub fn new(expected: Vec<E>) -> Self {
Self {
expected,
Expand All @@ -210,6 +235,7 @@ impl<E> IterContainsOnlyOnce<E> {
}
}

#[must_use]
pub struct IterContainsExactly<E> {
pub expected: Vec<E>,
pub missing: HashSet<usize>,
Expand All @@ -218,7 +244,6 @@ pub struct IterContainsExactly<E> {
}

impl<E> IterContainsExactly<E> {
#[must_use]
pub fn new(expected: Vec<E>) -> Self {
Self {
expected,
Expand All @@ -229,14 +254,14 @@ impl<E> IterContainsExactly<E> {
}
}

#[must_use]
pub struct IterContainsSequence<E> {
pub expected: Vec<E>,
pub missing: HashSet<usize>,
pub extra: HashSet<usize>,
}

impl<E> IterContainsSequence<E> {
#[must_use]
pub fn new(expected: Vec<E>) -> Self {
Self {
expected,
Expand All @@ -246,13 +271,13 @@ impl<E> IterContainsSequence<E> {
}
}

#[must_use]
pub struct IterContainsAllInOrder<E> {
pub expected: Vec<E>,
pub missing: HashSet<usize>,
}

impl<E> IterContainsAllInOrder<E> {
#[must_use]
pub fn new(expected: Vec<E>) -> Self {
Self {
expected,
Expand All @@ -261,14 +286,14 @@ impl<E> IterContainsAllInOrder<E> {
}
}

#[must_use]
pub struct IterStartsWith<E> {
pub expected: Vec<E>,
pub missing: HashSet<usize>,
pub extra: HashSet<usize>,
}

impl<E> IterStartsWith<E> {
#[must_use]
pub fn new(expected: Vec<E>) -> Self {
Self {
expected,
Expand All @@ -278,14 +303,14 @@ impl<E> IterStartsWith<E> {
}
}

#[must_use]
pub struct IterEndsWith<E> {
pub expected: Vec<E>,
pub missing: HashSet<usize>,
pub extra: HashSet<usize>,
}

impl<E> IterEndsWith<E> {
#[must_use]
pub fn new(expected: Vec<E>) -> Self {
Self {
expected,
Expand All @@ -295,28 +320,27 @@ impl<E> IterEndsWith<E> {
}
}

#[must_use]
pub struct Predicate<F> {
pub predicate: F,
pub message: Option<String>,
}

#[cfg(feature = "panic")]
pub use panic::DoesNotPanic;
#[cfg(feature = "panic")]
pub use panic::DoesPanic;
pub use panic::{DoesNotPanic, DoesPanic};

#[cfg(feature = "panic")]
mod panic {
use std::any::Any;

#[must_use]
#[derive(Default)]
pub struct DoesPanic {
pub expected_message: Option<String>,
pub actual_message: Option<String>,
}

impl DoesPanic {
#[must_use]
pub fn with_any_message() -> Self {
Self::default()
}
Expand All @@ -329,6 +353,7 @@ mod panic {
}
}

#[must_use]
#[derive(Default)]
pub struct DoesNotPanic {
pub actual_message: Option<Box<dyn Any + Send>>,
Expand Down
Loading
Loading