Skip to content

Commit fb64e72

Browse files
theodorebjeTheodore Bjernhed
andauthored
Add #[must_use] to types and methods (#3395)
Co-authored-by: Theodore Bjernhed <[email protected]>
1 parent d710431 commit fb64e72

File tree

22 files changed

+59
-6
lines changed

22 files changed

+59
-6
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ lossy_float_literal = "warn"
3535
macro_use_imports = "warn"
3636
match_wildcard_for_single_variants = "warn"
3737
mem_forget = "warn"
38+
must_use_candidate = "warn"
3839
needless_borrow = "warn"
3940
needless_continue = "warn"
4041
option_option = "warn"
4142
rest_pat_in_fully_bound_structs = "warn"
43+
return_self_not_must_use = "warn"
4244
str_to_string = "warn"
4345
suboptimal_flops = "warn"
4446
todo = "warn"

axum-core/src/body.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ where
3434
}
3535

3636
/// The body type used in axum requests and responses.
37+
#[must_use]
3738
#[derive(Debug)]
3839
pub struct Body(BoxBody);
3940

@@ -135,6 +136,7 @@ impl http_body::Body for Body {
135136
/// A stream of data frames.
136137
///
137138
/// Created with [`Body::into_data_stream`].
139+
#[must_use]
138140
#[derive(Debug)]
139141
pub struct BodyDataStream {
140142
inner: Body,

axum-core/src/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ impl Error {
1616
}
1717

1818
/// Convert an `Error` back into the underlying boxed trait object.
19+
#[must_use]
1920
pub fn into_inner(self) -> BoxError {
2021
self.inner
2122
}

axum-core/src/macros.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,13 @@ macro_rules! __define_rejection {
106106
}
107107

108108
/// Get the response body text used for this rejection.
109+
#[must_use]
109110
pub fn body_text(&self) -> String {
110111
self.to_string()
111112
}
112113

113114
/// Get the status code used for this rejection.
115+
#[must_use]
114116
pub fn status(&self) -> http::StatusCode {
115117
http::StatusCode::$status
116118
}
@@ -179,6 +181,7 @@ macro_rules! __composite_rejection {
179181

180182
impl $name {
181183
/// Get the response body text used for this rejection.
184+
#[must_use]
182185
pub fn body_text(&self) -> String {
183186
match self {
184187
$(
@@ -188,6 +191,7 @@ macro_rules! __composite_rejection {
188191
}
189192

190193
/// Get the status code used for this rejection.
194+
#[must_use]
191195
pub fn status(&self) -> http::StatusCode {
192196
match self {
193197
$(

axum-extra/src/extract/cookie/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ pub use cookie::Key;
8484
/// .route("/me", get(me));
8585
/// # let app: Router = app;
8686
/// ```
87+
#[must_use = "`CookieJar` should be returned as part of a `Response`, otherwise it does nothing."]
8788
#[derive(Debug, Default, Clone)]
8889
pub struct CookieJar {
8990
jar: cookie::CookieJar,
@@ -153,6 +154,7 @@ impl CookieJar {
153154
/// .map(|cookie| cookie.value().to_owned());
154155
/// }
155156
/// ```
157+
#[must_use]
156158
pub fn get(&self, name: &str) -> Option<&Cookie<'static>> {
157159
self.jar.get(name)
158160
}
@@ -169,7 +171,6 @@ impl CookieJar {
169171
/// jar.remove(Cookie::from("foo"))
170172
/// }
171173
/// ```
172-
#[must_use]
173174
pub fn remove<C: Into<Cookie<'static>>>(mut self, cookie: C) -> Self {
174175
self.jar.remove(cookie);
175176
self
@@ -189,7 +190,6 @@ impl CookieJar {
189190
/// jar.add(Cookie::new("foo", "bar"))
190191
/// }
191192
/// ```
192-
#[must_use]
193193
#[allow(clippy::should_implement_trait)]
194194
pub fn add<C: Into<Cookie<'static>>>(mut self, cookie: C) -> Self {
195195
self.jar.add(cookie);

axum-extra/src/extract/cookie/private.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ use std::{convert::Infallible, fmt, marker::PhantomData};
104104
/// }
105105
/// }
106106
/// ```
107+
#[must_use = "`PrivateCookieJar` should be returned as part of a `Response`, otherwise it does nothing."]
107108
pub struct PrivateCookieJar<K = Key> {
108109
jar: cookie::CookieJar,
109110
key: Key,
@@ -201,6 +202,7 @@ impl<K> PrivateCookieJar<K> {
201202
/// .map(|cookie| cookie.value().to_owned());
202203
/// }
203204
/// ```
205+
#[must_use]
204206
pub fn get(&self, name: &str) -> Option<Cookie<'static>> {
205207
self.private_jar().get(name)
206208
}
@@ -217,7 +219,6 @@ impl<K> PrivateCookieJar<K> {
217219
/// jar.remove(Cookie::from("foo"))
218220
/// }
219221
/// ```
220-
#[must_use]
221222
pub fn remove<C: Into<Cookie<'static>>>(mut self, cookie: C) -> Self {
222223
self.private_jar_mut().remove(cookie);
223224
self
@@ -237,7 +238,6 @@ impl<K> PrivateCookieJar<K> {
237238
/// jar.add(Cookie::new("foo", "bar"))
238239
/// }
239240
/// ```
240-
#[must_use]
241241
#[allow(clippy::should_implement_trait)]
242242
pub fn add<C: Into<Cookie<'static>>>(mut self, cookie: C) -> Self {
243243
self.private_jar_mut().add(cookie);
@@ -246,6 +246,7 @@ impl<K> PrivateCookieJar<K> {
246246

247247
/// Authenticates and decrypts `cookie`, returning the plaintext version if decryption succeeds
248248
/// or `None` otherwise.
249+
#[must_use]
249250
pub fn decrypt(&self, cookie: Cookie<'static>) -> Option<Cookie<'static>> {
250251
self.private_jar().decrypt(cookie)
251252
}
@@ -284,6 +285,7 @@ impl<K> IntoResponse for PrivateCookieJar<K> {
284285
}
285286
}
286287

288+
#[must_use = "iterators are lazy and do nothing unless consumed"]
287289
struct PrivateCookieJarIter<'a, K> {
288290
jar: &'a PrivateCookieJar<K>,
289291
iter: cookie::Iter<'a>,

axum-extra/src/extract/cookie/signed.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ use std::{convert::Infallible, fmt, marker::PhantomData};
121121
/// }
122122
/// }
123123
/// ```
124+
#[must_use = "`SignedCookieJar` should be returned as part of a `Response`, otherwise it does nothing."]
124125
pub struct SignedCookieJar<K = Key> {
125126
jar: cookie::CookieJar,
126127
key: Key,
@@ -219,6 +220,7 @@ impl<K> SignedCookieJar<K> {
219220
/// .map(|cookie| cookie.value().to_owned());
220221
/// }
221222
/// ```
223+
#[must_use]
222224
pub fn get(&self, name: &str) -> Option<Cookie<'static>> {
223225
self.signed_jar().get(name)
224226
}
@@ -235,7 +237,6 @@ impl<K> SignedCookieJar<K> {
235237
/// jar.remove(Cookie::from("foo"))
236238
/// }
237239
/// ```
238-
#[must_use]
239240
pub fn remove<C: Into<Cookie<'static>>>(mut self, cookie: C) -> Self {
240241
self.signed_jar_mut().remove(cookie);
241242
self
@@ -255,7 +256,6 @@ impl<K> SignedCookieJar<K> {
255256
/// jar.add(Cookie::new("foo", "bar"))
256257
/// }
257258
/// ```
258-
#[must_use]
259259
#[allow(clippy::should_implement_trait)]
260260
pub fn add<C: Into<Cookie<'static>>>(mut self, cookie: C) -> Self {
261261
self.signed_jar_mut().add(cookie);
@@ -264,6 +264,7 @@ impl<K> SignedCookieJar<K> {
264264

265265
/// Verifies the authenticity and integrity of `cookie`, returning the plaintext version if
266266
/// verification succeeds or `None` otherwise.
267+
#[must_use]
267268
pub fn verify(&self, cookie: Cookie<'static>) -> Option<Cookie<'static>> {
268269
self.signed_jar().verify(cookie)
269270
}
@@ -302,6 +303,7 @@ impl<K> IntoResponse for SignedCookieJar<K> {
302303
}
303304
}
304305

306+
#[must_use = "iterators are lazy and do nothing unless consumed"]
305307
struct SignedCookieJarIter<'a, K> {
306308
jar: &'a SignedCookieJar<K>,
307309
iter: cookie::Iter<'a>,

axum-extra/src/extract/multipart.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,27 @@ impl Field {
150150
/// The field name found in the
151151
/// [`Content-Disposition`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition)
152152
/// header.
153+
#[must_use]
153154
pub fn name(&self) -> Option<&str> {
154155
self.inner.name()
155156
}
156157

157158
/// The file name found in the
158159
/// [`Content-Disposition`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition)
159160
/// header.
161+
#[must_use]
160162
pub fn file_name(&self) -> Option<&str> {
161163
self.inner.file_name()
162164
}
163165

164166
/// Get the [content type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type) of the field.
167+
#[must_use]
165168
pub fn content_type(&self) -> Option<&str> {
166169
self.inner.content_type().map(|m| m.as_ref())
167170
}
168171

169172
/// Get a map of headers as [`HeaderMap`].
173+
#[must_use]
170174
pub fn headers(&self) -> &HeaderMap {
171175
self.inner.headers()
172176
}
@@ -253,6 +257,7 @@ impl MultipartError {
253257
}
254258

255259
/// Get the status code used for this rejection.
260+
#[must_use]
256261
pub fn status(&self) -> http::StatusCode {
257262
status_code_from_multer_error(&self.source)
258263
}

axum-extra/src/response/file_stream.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use tokio_util::io::ReaderStream;
4444
/// let app = Router::new().route("/file-stream", get(file_stream));
4545
/// # let _: Router = app;
4646
/// ```
47+
#[must_use]
4748
#[derive(Debug)]
4849
pub struct FileStream<S> {
4950
/// stream.

axum-extra/src/response/multiple.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use mime::Mime;
88
/// Create multipart forms to be used in API responses.
99
///
1010
/// This struct implements [`IntoResponse`], and so it can be returned from a handler.
11+
#[must_use]
1112
#[derive(Debug)]
1213
pub struct MultipartForm {
1314
parts: Vec<Part>,
@@ -103,6 +104,7 @@ impl Part {
103104
/// let parts: Vec<Part> = vec![Part::text("foo".to_string(), "abc")];
104105
/// let form = MultipartForm::from_iter(parts);
105106
/// ```
107+
#[must_use]
106108
pub fn text(name: String, contents: &str) -> Self {
107109
Self {
108110
name,
@@ -127,6 +129,7 @@ impl Part {
127129
/// let parts: Vec<Part> = vec![Part::file("foo", "foo.txt", vec![0x68, 0x68, 0x20, 0x6d, 0x6f, 0x6d])];
128130
/// let form = MultipartForm::from_iter(parts);
129131
/// ```
132+
#[must_use]
130133
pub fn file(field_name: &str, file_name: &str, contents: Vec<u8>) -> Self {
131134
Self {
132135
name: field_name.to_owned(),

0 commit comments

Comments
 (0)