Skip to content

Commit fe19bc5

Browse files
committed
Add must_use annotations
Add annotations to warn about unused return values at call-sites. Annotate `OperationResult`, similar to `std::result::Result`, constructors, and functions with no side-effects.
1 parent 81cea04 commit fe19bc5

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

src/delay/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub struct Exponential {
1818
impl Exponential {
1919
/// Create a new [`Exponential`] using the given millisecond duration as the initial delay and
2020
/// an exponential backoff factor of `2.0`.
21+
#[must_use]
2122
pub fn from_millis(base: u64) -> Self {
2223
Exponential {
2324
current: base,
@@ -28,6 +29,7 @@ impl Exponential {
2829
/// Create a new [`Exponential`] using the given millisecond duration as the initial delay and
2930
/// the same duration as the exponential backoff factor. This was the behavior of
3031
/// [`Exponential::from_millis`] prior to version 2.0.
32+
#[must_use]
3133
pub fn from_millis_with_base_factor(base: u64) -> Self {
3234
Exponential {
3335
current: base,
@@ -37,6 +39,7 @@ impl Exponential {
3739

3840
/// Create a new [`Exponential`] using the given millisecond duration as the initial delay and
3941
/// the given exponential backoff factor.
42+
#[must_use]
4043
pub fn from_millis_with_factor(base: u64, factor: f64) -> Self {
4144
Exponential {
4245
current: base,
@@ -102,6 +105,7 @@ pub struct Fibonacci {
102105

103106
impl Fibonacci {
104107
/// Create a new [`Fibonacci`] using the given duration in milliseconds.
108+
#[must_use]
105109
pub fn from_millis(millis: u64) -> Fibonacci {
106110
Fibonacci {
107111
curr: millis,
@@ -160,6 +164,7 @@ pub struct Fixed {
160164

161165
impl Fixed {
162166
/// Create a new [`Fixed`] using the given duration in milliseconds.
167+
#[must_use]
163168
pub fn from_millis(millis: u64) -> Self {
164169
Fixed {
165170
duration: Duration::from_millis(millis),

src/delay/random.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ impl Range {
2323
/// # Panics
2424
///
2525
/// Panics if the minimum is greater than or equal to the maximum.
26+
#[must_use]
2627
pub fn from_millis_exclusive(minimum: u64, maximum: u64) -> Self {
2728
Range {
2829
distribution: Uniform::new(minimum, maximum)
@@ -49,6 +50,7 @@ impl Range {
4950
/// # Panics
5051
///
5152
/// Panics if the minimum is greater than or equal to the maximum.
53+
#[must_use]
5254
pub fn from_millis_inclusive(minimum: u64, maximum: u64) -> Self {
5355
Range {
5456
distribution: Uniform::new_inclusive(minimum, maximum)
@@ -97,6 +99,7 @@ impl From<RangeInclusive<Duration>> for Range {
9799
}
98100

99101
/// Apply full random jitter to a duration. (When the `random` Cargo feature is enabled.)
102+
#[must_use]
100103
pub fn jitter(duration: Duration) -> Duration {
101104
let jitter = random::<f64>();
102105
let secs = ((duration.as_secs() as f64) * jitter).ceil() as u64;
@@ -115,13 +118,13 @@ fn range_uniform() {
115118
#[test]
116119
#[should_panic]
117120
fn range_exclusive_uniform_wrong_input() {
118-
Range::from_millis_exclusive(1, 0);
121+
let _panic = Range::from_millis_exclusive(1, 0);
119122
}
120123

121124
#[test]
122125
#[should_panic]
123126
fn range_inclusive_uniform_wrong_input() {
124-
Range::from_millis_inclusive(1, 0);
127+
let _panic = Range::from_millis_inclusive(1, 0);
125128
}
126129

127130
#[test]

src/opresult.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
//! ```
2020
2121
/// A result that represents either success, retryable failure, or immediately-returning failure.
22+
#[must_use]
2223
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
2324
pub enum OperationResult<T, E> {
2425
/// Contains the success value.
@@ -57,6 +58,7 @@ impl<T, E> OperationResult<T, E> {
5758
/// let x: OperationResult<i32, &str> = OperationResult::Err("Some other error message");
5859
/// assert_eq!(x.is_ok(), false);
5960
/// ```
61+
#[must_use]
6062
pub fn is_ok(&self) -> bool {
6163
matches!(self, Self::Ok(_))
6264
}
@@ -79,6 +81,7 @@ impl<T, E> OperationResult<T, E> {
7981
/// let x: OperationResult<i32, &str> = OperationResult::Err("Some other error message");
8082
/// assert_eq!(x.is_retry(), false);
8183
/// ```
84+
#[must_use]
8285
pub fn is_retry(&self) -> bool {
8386
matches!(self, Self::Retry(_))
8487
}
@@ -101,6 +104,7 @@ impl<T, E> OperationResult<T, E> {
101104
/// let x: OperationResult<i32, &str> = OperationResult::Err("Some other error message");
102105
/// assert_eq!(x.is_err(), true);
103106
/// ```
107+
#[must_use]
104108
pub fn is_err(&self) -> bool {
105109
matches!(self, Self::Err(_))
106110
}

0 commit comments

Comments
 (0)