Skip to content

Commit 7d0b848

Browse files
authored
Merge pull request #526 from ethanboxx/clippy
Various Clippy Fixes
2 parents 162652a + 40a353a commit 7d0b848

12 files changed

+82
-51
lines changed

src/cookies/middleware.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use std::sync::{Arc, RwLock};
2525
pub(crate) struct CookiesMiddleware;
2626

2727
impl CookiesMiddleware {
28-
/// Creates a new CookiesMiddleware.
28+
/// Creates a new `CookiesMiddleware`.
2929
pub fn new() -> Self {
3030
Self::default()
3131
}
@@ -82,15 +82,15 @@ impl CookieData {
8282
if let Some(cookie_headers) = req.header(&headers::COOKIE) {
8383
for cookie_header in cookie_headers {
8484
// spec says there should be only one, so this is permissive
85-
for pair in cookie_header.as_str().split(";") {
85+
for pair in cookie_header.as_str().split(';') {
8686
if let Ok(cookie) = Cookie::parse_encoded(String::from(pair)) {
8787
jar.add_original(cookie);
8888
}
8989
}
9090
}
9191
}
9292

93-
CookieData {
93+
Self {
9494
content: Arc::new(RwLock::new(jar)),
9595
}
9696
}

src/endpoint.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ use crate::{Middleware, Request, Response};
2727
/// Ok(String::from("hello"))
2828
/// }
2929
///
30-
/// fn main() {
31-
/// let mut app = tide::Server::new();
32-
/// app.at("/hello").get(hello);
33-
/// }
30+
/// let mut app = tide::Server::new();
31+
/// app.at("/hello").get(hello);
3432
/// ```
3533
///
3634
/// An endpoint with similar functionality that does not make use of the `async` keyword would look something like this:
@@ -41,10 +39,8 @@ use crate::{Middleware, Request, Response};
4139
/// async_std::future::ready(Ok(String::from("hello")))
4240
/// }
4341
///
44-
/// fn main() {
45-
/// let mut app = tide::Server::new();
46-
/// app.at("/hello").get(hello);
47-
/// }
42+
/// let mut app = tide::Server::new();
43+
/// app.at("/hello").get(hello);
4844
/// ```
4945
///
5046
/// Tide routes will also accept endpoints with `Fn` signatures of this form, but using the `async` keyword has better ergonomics.

src/fs/serve_dir.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,11 @@ impl<State> Endpoint<State> for ServeDir {
5757
Ok(file) => file,
5858
};
5959

60-
let len = match file.metadata().await {
61-
Ok(metadata) => metadata.len() as usize,
62-
Err(_) => {
63-
log::warn!("Could not retrieve metadata");
64-
return Ok(Response::new(StatusCode::InternalServerError));
65-
}
60+
let len = if let Ok(metadata) = file.metadata().await {
61+
metadata.len() as usize
62+
} else {
63+
log::warn!("Could not retrieve metadata");
64+
return Ok(Response::new(StatusCode::InternalServerError));
6665
};
6766

6867
let body = Body::from_reader(BufReader::new(file), Some(len));

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ pub use http_types::{self as http, Body, Error, Status, StatusCode};
227227
/// #
228228
/// # Ok(()) }) }
229229
/// ```
230+
#[must_use]
230231
pub fn new() -> server::Server<()> {
231232
Server::new()
232233
}

src/log/middleware.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ use crate::{Middleware, Next, Request};
1212
/// let mut app = tide::Server::new();
1313
/// app.middleware(tide::log::LogMiddleware::new());
1414
/// ```
15-
#[derive(Debug, Clone)]
15+
#[derive(Debug, Default, Clone)]
1616
pub struct LogMiddleware {
1717
_priv: (),
1818
}
1919

2020
impl LogMiddleware {
2121
/// Create a new instance of `LogMiddleware`.
22+
#[must_use]
2223
pub fn new() -> Self {
2324
Self { _priv: () }
2425
}

src/middleware.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub struct Next<'a, State> {
4747

4848
impl<'a, State: 'static> Next<'a, State> {
4949
/// Asynchronously execute the remaining middleware chain.
50+
#[must_use]
5051
pub fn run(mut self, req: Request<State>) -> BoxFuture<'a, crate::Result> {
5152
if let Some((current, next)) = self.next_middleware.split_first() {
5253
self.next_middleware = next;

src/request.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ impl<State> Request<State> {
3232
state: Arc<State>,
3333
request: http_types::Request,
3434
route_params: Vec<Params>,
35-
) -> Request<State> {
36-
Request {
35+
) -> Self {
36+
Self {
3737
state,
3838
request,
3939
route_params,
@@ -59,6 +59,7 @@ impl<State> Request<State> {
5959
/// #
6060
/// # Ok(()) })}
6161
/// ```
62+
#[must_use]
6263
pub fn method(&self) -> Method {
6364
self.request.method()
6465
}
@@ -82,6 +83,7 @@ impl<State> Request<State> {
8283
/// #
8384
/// # Ok(()) })}
8485
/// ```
86+
#[must_use]
8587
pub fn uri(&self) -> &Url {
8688
self.request.url()
8789
}
@@ -105,6 +107,7 @@ impl<State> Request<State> {
105107
/// #
106108
/// # Ok(()) })}
107109
/// ```
110+
#[must_use]
108111
pub fn version(&self) -> Option<Version> {
109112
self.request.version()
110113
}
@@ -128,6 +131,7 @@ impl<State> Request<State> {
128131
/// #
129132
/// # Ok(()) })}
130133
/// ```
134+
#[must_use]
131135
pub fn header(
132136
&self,
133137
key: &http_types::headers::HeaderName,
@@ -136,6 +140,7 @@ impl<State> Request<State> {
136140
}
137141

138142
/// Get a local value.
143+
#[must_use]
139144
pub fn local<T: Send + Sync + 'static>(&self) -> Option<&T> {
140145
self.request.local().get()
141146
}
@@ -146,6 +151,7 @@ impl<State> Request<State> {
146151
self
147152
}
148153

154+
#[must_use]
149155
/// Access app-global state.
150156
pub fn state(&self) -> &State {
151157
&self.state
@@ -171,8 +177,7 @@ impl<State> Request<State> {
171177
self.route_params
172178
.iter()
173179
.rev()
174-
.filter_map(|params| params.find(key))
175-
.next()
180+
.find_map(|params| params.find(key))
176181
.unwrap()
177182
.parse()
178183
}
@@ -293,6 +298,7 @@ impl<State> Request<State> {
293298
}
294299

295300
/// returns a `Cookie` by name of the cookie.
301+
#[must_use]
296302
pub fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
297303
let cookie_data = self
298304
.local::<CookieData>()
@@ -303,9 +309,15 @@ impl<State> Request<State> {
303309
}
304310

305311
/// Get the length of the body.
312+
#[must_use]
306313
pub fn len(&self) -> Option<usize> {
307314
self.request.len()
308315
}
316+
/// Checks if the body is empty.
317+
#[must_use]
318+
pub fn is_empty(&self) -> Option<bool> {
319+
Some(self.request.len()? == 0)
320+
}
309321
}
310322

311323
impl<State> AsMut<http::Request> for Request<State> {

src/response.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub struct Response {
2525

2626
impl Response {
2727
/// Create a new instance.
28+
#[must_use]
2829
pub fn new(status: StatusCode) -> Self {
2930
let res = http_types::Response::new(status);
3031
Self {
@@ -72,22 +73,32 @@ impl Response {
7273
}
7374

7475
/// Returns the statuscode.
76+
#[must_use]
7577
pub fn status(&self) -> crate::StatusCode {
7678
self.res.status()
7779
}
7880

7981
/// Set the statuscode.
82+
#[must_use]
8083
pub fn set_status(mut self, status: crate::StatusCode) -> Self {
8184
self.res.set_status(status);
8285
self
8386
}
8487

8588
/// Get the length of the body.
89+
#[must_use]
8690
pub fn len(&self) -> Option<usize> {
8791
self.res.len()
8892
}
8993

94+
/// Checks if the body is empty.
95+
#[must_use]
96+
pub fn is_empty(&self) -> Option<bool> {
97+
Some(self.res.len()? == 0)
98+
}
99+
90100
/// Get an HTTP header.
101+
#[must_use]
91102
pub fn header(&self, name: &HeaderName) -> Option<&Vec<HeaderValue>> {
92103
self.res.header(name)
93104
}
@@ -126,6 +137,7 @@ impl Response {
126137
/// Set the request MIME.
127138
///
128139
/// [Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types)
140+
#[must_use]
129141
pub fn set_mime(self, mime: Mime) -> Self {
130142
self.set_header(http_types::headers::CONTENT_TYPE, format!("{}", mime))
131143
}
@@ -135,6 +147,7 @@ impl Response {
135147
/// # Mime
136148
///
137149
/// The encoding is set to `text/plain; charset=utf-8`.
150+
#[must_use]
138151
pub fn body_string(mut self, string: String) -> Self {
139152
self.res.set_body(string);
140153
self.set_mime(mime::TEXT_PLAIN_UTF_8)
@@ -167,7 +180,7 @@ impl Response {
167180
pub async fn body_form<T: serde::Serialize>(
168181
mut self,
169182
form: T,
170-
) -> Result<Response, serde_qs::Error> {
183+
) -> Result<Self, serde_qs::Error> {
171184
// TODO: think about how to handle errors
172185
self.res.set_body(serde_qs::to_string(&form)?.into_bytes());
173186
Ok(self
@@ -221,6 +234,7 @@ impl Response {
221234
}
222235

223236
/// Get a local value.
237+
#[must_use]
224238
pub fn local<T: Send + Sync + 'static>(&self) -> Option<&T> {
225239
self.res.local().get()
226240
}

src/route.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ impl<'a, State: 'static> Route<'a, State> {
6060
}
6161

6262
/// Get the current path.
63+
#[must_use]
6364
pub fn path(&self) -> &str {
6465
&self.path
6566
}

src/router.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ pub(crate) struct Selection<'a, State> {
2222
}
2323

2424
impl<State: 'static> Router<State> {
25-
pub(crate) fn new() -> Router<State> {
26-
Router {
25+
pub(crate) fn new() -> Self {
26+
Self {
2727
method_map: HashMap::default(),
2828
all_method_router: MethodRouter::new(),
2929
}

0 commit comments

Comments
 (0)