From f232b6c684bebbba7eb139a242cc1ae91312d4df Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 5 Sep 2019 00:04:57 +0900 Subject: [PATCH] Fix clippy warnings (#168) --- async_ex2/src/appconfig.rs | 2 +- async_ex2/src/handlers/parts.rs | 8 ++------ async_ex2/src/handlers/products.rs | 8 ++------ basics/src/main.rs | 2 +- cookie-auth/src/main.rs | 5 ++++- jsonrpc/src/main.rs | 16 +++++++++++----- middleware/src/simple.rs | 2 +- state/src/main.rs | 2 +- template_askama/src/main.rs | 2 +- websocket-chat-broker/src/main.rs | 4 ++-- websocket-chat-broker/src/server.rs | 4 ++-- websocket-chat/src/main.rs | 2 +- websocket-chat/src/server.rs | 2 +- websocket-tcp-chat/src/client.rs | 2 +- websocket-tcp-chat/src/main.rs | 4 ++-- websocket-tcp-chat/src/server.rs | 2 +- websocket-tcp-chat/src/session.rs | 10 ++++------ websocket/src/client.rs | 10 +++------- 18 files changed, 41 insertions(+), 46 deletions(-) diff --git a/async_ex2/src/appconfig.rs b/async_ex2/src/appconfig.rs index 9393e129..f007003a 100644 --- a/async_ex2/src/appconfig.rs +++ b/async_ex2/src/appconfig.rs @@ -1,4 +1,4 @@ -use actix_web::{error, web}; +use actix_web::web; use crate::handlers::{parts, products}; diff --git a/async_ex2/src/handlers/parts.rs b/async_ex2/src/handlers/parts.rs index 0dba7b0f..3e3874f4 100644 --- a/async_ex2/src/handlers/parts.rs +++ b/async_ex2/src/handlers/parts.rs @@ -1,9 +1,5 @@ -use actix_multipart::{Field, Multipart, MultipartError}; -use actix_web::{error, web, Error, HttpResponse}; -use futures::{ - future::{err as fut_err, ok as fut_ok, Either}, - Future, Stream, -}; +use actix_web::{web, Error, HttpResponse}; +use futures::{future::ok as fut_ok, Future}; use crate::common::{Part, Product}; diff --git a/async_ex2/src/handlers/products.rs b/async_ex2/src/handlers/products.rs index 1f3d68fd..36b774a4 100644 --- a/async_ex2/src/handlers/products.rs +++ b/async_ex2/src/handlers/products.rs @@ -1,9 +1,5 @@ -use actix_multipart::{Field, Multipart, MultipartError}; -use actix_web::{error, web, Error, HttpResponse}; -use futures::{ - future::{err as fut_err, ok as fut_ok, Either}, - Future, Stream, -}; +use actix_web::{web, Error, HttpResponse}; +use futures::{future::ok as fut_ok, Future}; use crate::common::{Part, Product}; diff --git a/basics/src/main.rs b/basics/src/main.rs index cace43ae..ab98ecba 100644 --- a/basics/src/main.rs +++ b/basics/src/main.rs @@ -132,7 +132,7 @@ fn main() -> io::Result<()> { .route( web::route() .guard(guard::Not(guard::Get())) - .to(|| HttpResponse::MethodNotAllowed()), + .to(HttpResponse::MethodNotAllowed), ), ) }) diff --git a/cookie-auth/src/main.rs b/cookie-auth/src/main.rs index 59634c53..9da2da57 100644 --- a/cookie-auth/src/main.rs +++ b/cookie-auth/src/main.rs @@ -3,7 +3,10 @@ use actix_identity::{CookieIdentityPolicy, IdentityService}; use actix_web::{middleware, web, App, HttpResponse, HttpServer}; fn index(id: Identity) -> String { - format!("Hello {}", id.identity().unwrap_or("Anonymous".to_owned())) + format!( + "Hello {}", + id.identity().unwrap_or_else(|| "Anonymous".to_owned()) + ) } fn login(id: Identity) -> HttpResponse { diff --git a/jsonrpc/src/main.rs b/jsonrpc/src/main.rs index 18ba5545..fd4f0951 100644 --- a/jsonrpc/src/main.rs +++ b/jsonrpc/src/main.rs @@ -86,7 +86,10 @@ fn rpc_select( pub trait ImplNetwork { fn ping(&self) -> String; - fn wait(&self, d: u64) -> Box>>; + fn wait( + &self, + d: u64, + ) -> Box>>; fn get(&self) -> u32; fn inc(&mut self); @@ -107,9 +110,12 @@ impl ImplNetwork for ObjNetwork { String::from("pong") } - fn wait(&self, d: u64) -> Box>> { + fn wait( + &self, + d: u64, + ) -> Box>> { if let Err(e) = Delay::new(Duration::from_secs(d)).wait() { - let e: Box = Box::new(e); + let e: Box = Box::new(e); return Box::new(future::err(e)); }; Box::new(future::ok(String::from("pong"))) @@ -126,11 +132,11 @@ impl ImplNetwork for ObjNetwork { #[derive(Clone)] pub struct AppState { - network: Arc>, + network: Arc>, } impl AppState { - pub fn new(network: Arc>) -> Self { + pub fn new(network: Arc>) -> Self { Self { network } } } diff --git a/middleware/src/simple.rs b/middleware/src/simple.rs index 789eedab..a96e156b 100644 --- a/middleware/src/simple.rs +++ b/middleware/src/simple.rs @@ -43,7 +43,7 @@ where type Request = ServiceRequest; type Response = ServiceResponse; type Error = Error; - type Future = Box>; + type Future = Box>; fn poll_ready(&mut self) -> Poll<(), Self::Error> { self.service.poll_ready() diff --git a/state/src/main.rs b/state/src/main.rs index 3f5d721a..c7f6f947 100644 --- a/state/src/main.rs +++ b/state/src/main.rs @@ -1,4 +1,4 @@ -#![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))] +#![allow(clippy::needless_pass_by_value)] //! Application may have multiple data objects that are shared across //! all handlers within same Application. Data could be added //! with `App::data()` method, multiple different data objects could be added. diff --git a/template_askama/src/main.rs b/template_askama/src/main.rs index f1060f8c..f117ab36 100644 --- a/template_askama/src/main.rs +++ b/template_askama/src/main.rs @@ -17,7 +17,7 @@ struct Index; fn index(query: web::Query>) -> Result { let s = if let Some(name) = query.get("name") { UserTemplate { - name: name, + name, text: "Welcome!", } .render() diff --git a/websocket-chat-broker/src/main.rs b/websocket-chat-broker/src/main.rs index 00c6ad2d..4509badf 100644 --- a/websocket-chat-broker/src/main.rs +++ b/websocket-chat-broker/src/main.rs @@ -68,7 +68,7 @@ impl WsChatSession { fn send_msg(&self, msg: &str) { let content = format!( "{}: {}", - self.name.clone().unwrap_or("anon".to_string()), + self.name.clone().unwrap_or_else(|| "anon".to_string()), msg ); let msg = SendMessage(self.room.clone(), self.id, content); @@ -87,7 +87,7 @@ impl Actor for WsChatSession { fn stopped(&mut self, _ctx: &mut Self::Context) { info!( "WsChatSession closed for {}({}) in room {}", - self.name.clone().unwrap_or("anon".to_string()), + self.name.clone().unwrap_or_else(|| "anon".to_string()), self.id, self.room ); diff --git a/websocket-chat-broker/src/server.rs b/websocket-chat-broker/src/server.rs index 198b6c85..946f670a 100644 --- a/websocket-chat-broker/src/server.rs +++ b/websocket-chat-broker/src/server.rs @@ -42,7 +42,7 @@ impl WsChatServer { id: Option, client: Client, ) -> usize { - let mut id = id.unwrap_or_else(|| rand::random::()); + let mut id = id.unwrap_or_else(rand::random::); if let Some(room) = self.rooms.get_mut(room_name) { loop { if room.contains_key(&id) { @@ -94,7 +94,7 @@ impl Handler for WsChatServer { let id = self.add_client_to_room(&room_name, None, client); let join_msg = format!( "{} joined {}", - client_name.unwrap_or("anon".to_string()), + client_name.unwrap_or_else(|| "anon".to_string()), room_name ); self.send_chat_message(&room_name, &join_msg, id); diff --git a/websocket-chat/src/main.rs b/websocket-chat/src/main.rs index ec7da927..f8eee4c3 100644 --- a/websocket-chat/src/main.rs +++ b/websocket-chat/src/main.rs @@ -164,7 +164,7 @@ impl StreamHandler for WsChatSession { // send message to chat server self.addr.do_send(server::ClientMessage { id: self.id, - msg: msg, + msg, room: self.room.clone(), }) } diff --git a/websocket-chat/src/server.rs b/websocket-chat/src/server.rs index 80d62b6d..f8acc451 100644 --- a/websocket-chat/src/server.rs +++ b/websocket-chat/src/server.rs @@ -68,7 +68,7 @@ impl Default for ChatServer { ChatServer { sessions: HashMap::new(), - rooms: rooms, + rooms, rng: rand::thread_rng(), } } diff --git a/websocket-tcp-chat/src/client.rs b/websocket-tcp-chat/src/client.rs index 28313e29..1694c870 100644 --- a/websocket-tcp-chat/src/client.rs +++ b/websocket-tcp-chat/src/client.rs @@ -154,7 +154,7 @@ impl StreamHandler for ChatClient { for room in rooms { println!("{}", room); } - println!(""); + println!(); } _ => (), } diff --git a/websocket-tcp-chat/src/main.rs b/websocket-tcp-chat/src/main.rs index fe73dc8e..7aeaec8e 100644 --- a/websocket-tcp-chat/src/main.rs +++ b/websocket-tcp-chat/src/main.rs @@ -171,7 +171,7 @@ impl StreamHandler for WsChatSession { // send message to chat server self.addr.do_send(server::Message { id: self.id, - msg: msg, + msg, room: self.room.clone(), }) } @@ -212,7 +212,7 @@ impl WsChatSession { } fn main() -> std::io::Result<()> { - let _ = env_logger::init(); + env_logger::init(); let sys = actix::System::new("websocket-example"); // Start chat server actor diff --git a/websocket-tcp-chat/src/server.rs b/websocket-tcp-chat/src/server.rs index c30b928b..b7a1a3db 100644 --- a/websocket-tcp-chat/src/server.rs +++ b/websocket-tcp-chat/src/server.rs @@ -66,7 +66,7 @@ impl Default for ChatServer { ChatServer { sessions: HashMap::new(), - rooms: rooms, + rooms, rng: rand::thread_rng(), } } diff --git a/websocket-tcp-chat/src/session.rs b/websocket-tcp-chat/src/session.rs index 27478794..711b3287 100644 --- a/websocket-tcp-chat/src/session.rs +++ b/websocket-tcp-chat/src/session.rs @@ -138,10 +138,10 @@ impl ChatSession { ) -> ChatSession { ChatSession { id: 0, - addr: addr, + addr, hb: Instant::now(), room: "Main".to_owned(), - framed: framed, + framed, } } @@ -187,10 +187,8 @@ impl TcpServer { // implement stream handler `StreamHandler<(TcpStream, // net::SocketAddr), io::Error>` TcpServer::create(|ctx| { - ctx.add_message_stream( - listener.incoming().map_err(|_| ()).map(|s| TcpConnect(s)), - ); - TcpServer { chat: chat } + ctx.add_message_stream(listener.incoming().map_err(|_| ()).map(TcpConnect)); + TcpServer { chat } }); } } diff --git a/websocket/src/client.rs b/websocket/src/client.rs index 04a683ad..0a9eee35 100644 --- a/websocket/src/client.rs +++ b/websocket/src/client.rs @@ -18,7 +18,7 @@ use futures::{ fn main() { ::std::env::set_var("RUST_LOG", "actix_web=info"); - let _ = env_logger::init(); + env_logger::init(); let sys = actix::System::new("ws-example"); Arbiter::spawn(lazy(|| { @@ -27,7 +27,6 @@ fn main() { .connect() .map_err(|e| { println!("Error: {}", e); - () }) .map(|(response, framed)| { println!("{:?}", response); @@ -46,8 +45,6 @@ fn main() { } addr.do_send(ClientCommand(cmd)); }); - - () }) })); @@ -113,9 +110,8 @@ where T: AsyncRead + AsyncWrite, { fn handle(&mut self, msg: Frame, _ctx: &mut Context) { - match msg { - Frame::Text(txt) => println!("Server: {:?}", txt), - _ => (), + if let Frame::Text(txt) = msg { + println!("Server: {:?}", txt) } }