From 9baade2a5d9fec54fe4d88216fbdb91c154ad348 Mon Sep 17 00:00:00 2001 From: Jokler Date: Sat, 16 Jun 2018 15:35:49 +0200 Subject: Fix clippy warnings --- src/error.rs | 2 +- src/lib.rs | 8 +++---- src/main.rs | 6 +++-- src/plugin.rs | 2 +- src/plugins/currency.rs | 24 +++++++++++-------- src/plugins/emoji.rs | 6 +++-- src/plugins/factoids/database.rs | 2 +- src/plugins/factoids/mod.rs | 41 +++++++++++++++++--------------- src/plugins/factoids/utils.rs | 2 +- src/plugins/help.rs | 6 +++-- src/plugins/keepnick.rs | 6 +++-- src/plugins/remind/database.rs | 14 +++++------ src/plugins/remind/mod.rs | 21 +++++++++-------- src/plugins/remind/parser.rs | 50 ++++++++++++++++------------------------ src/plugins/sed.rs | 10 ++++---- src/plugins/tell/database.rs | 2 +- src/plugins/tell/mod.rs | 40 +++++++++++++++++--------------- src/plugins/url.rs | 31 ++++++++++++------------- src/utils.rs | 5 +--- 19 files changed, 142 insertions(+), 136 deletions(-) (limited to 'src') diff --git a/src/error.rs b/src/error.rs index dc34eb7..251e812 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,7 +2,7 @@ use failure::Fail; -pub fn log_error(e: FrippyError) { +pub fn log_error(e: &FrippyError) { let text = e.causes() .skip(1) .fold(format!("{}", e), |acc, err| format!("{}: {}", acc, err)); diff --git a/src/lib.rs b/src/lib.rs index 0e14e42..8655161 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -239,7 +239,7 @@ impl ThreadedPlugins { // Send the message to the plugin if the plugin needs it match plugin.execute(client, &message) { ExecutionStatus::Done => (), - ExecutionStatus::Err(e) => log_error(e), + ExecutionStatus::Err(e) => log_error(&e), ExecutionStatus::RequiresThread => { debug!( "Spawning thread to execute {} with {}", @@ -257,14 +257,14 @@ impl ThreadedPlugins { .name(name) .spawn(move || { if let Err(e) = plugin.execute_threaded(&client, &message) { - log_error(e); + log_error(&e); } else { debug!("{} sent response from thread", plugin.name()); } }) .context(ErrorKind::ThreadSpawn) { - log_error(e.into()); + log_error(&e.into()); } } } @@ -290,7 +290,7 @@ impl ThreadedPlugins { .name(name) .spawn(move || { if let Err(e) = plugin.command(&client, command) { - log_error(e); + log_error(&e); }; }) .context(ErrorKind::ThreadSpawn)?; diff --git a/src/main.rs b/src/main.rs index 61cc839..a21ff39 100644 --- a/src/main.rs +++ b/src/main.rs @@ -102,7 +102,7 @@ fn run() -> Result<(), Error> { mysql_url = options.get("mysql_url"); } - let prefix = prefix.map(|&ref s| s.clone()).unwrap_or(String::from(".")); + let prefix = prefix.cloned().unwrap_or_else(|| String::from(".")); let mut bot = frippy::Bot::new(&prefix); bot.add_plugin(Help::new()); @@ -166,5 +166,7 @@ fn run() -> Result<(), Error> { } // Run the bots until they throw an error - an error could be loss of connection - Ok(reactor.run()?) + reactor.run()?; + + Ok(()) } diff --git a/src/plugin.rs b/src/plugin.rs index 653ec02..5538313 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -82,7 +82,7 @@ impl PluginCommand { Some(PluginCommand { source: message.source_nickname().unwrap().to_string(), target: message.response_target().unwrap().to_string(), - tokens: tokens, + tokens, }) } else { None diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs index 091c238..5790c70 100644 --- a/src/plugins/currency.rs +++ b/src/plugins/currency.rs @@ -7,8 +7,8 @@ use std::num::ParseFloatError; use irc::client::prelude::*; -use self::reqwest::Client; use self::reqwest::header::Connection; +use self::reqwest::Client; use self::serde_json::Value; use plugin::*; @@ -88,7 +88,7 @@ impl Currency { "{} {} => {:.4} {}", request.value, request.source.to_lowercase(), - response / 1.00000000, + response, request.target.to_lowercase() ); @@ -125,24 +125,28 @@ impl Plugin for Currency { fn command(&self, client: &IrcClient, mut command: PluginCommand) -> Result<(), FrippyError> { if command.tokens.is_empty() { - return Ok(client + client .send_notice(&command.source, &self.invalid_command()) - .context(FrippyErrorKind::Connection)?); + .context(FrippyErrorKind::Connection)?; + + return Ok(()); } match command.tokens[0].as_ref() { - "help" => Ok(client + "help" => client .send_notice(&command.source, self.help()) - .context(FrippyErrorKind::Connection)?), + .context(FrippyErrorKind::Connection)?, _ => match self.convert(&mut command) { - Ok(msg) => Ok(client + Ok(msg) => client .send_privmsg(&command.target, &msg) - .context(FrippyErrorKind::Connection)?), - Err(msg) => Ok(client + .context(FrippyErrorKind::Connection)?, + Err(msg) => client .send_notice(&command.source, &msg) - .context(FrippyErrorKind::Connection)?), + .context(FrippyErrorKind::Connection)?, }, } + + Ok(()) } fn evaluate(&self, _: &IrcClient, mut command: PluginCommand) -> Result { diff --git a/src/plugins/emoji.rs b/src/plugins/emoji.rs index a2337ee..e4dffdb 100644 --- a/src/plugins/emoji.rs +++ b/src/plugins/emoji.rs @@ -126,12 +126,14 @@ impl Plugin for Emoji { } fn command(&self, client: &IrcClient, command: PluginCommand) -> Result<(), FrippyError> { - Ok(client + client .send_notice( &command.source, "This Plugin does not implement any commands.", ) - .context(FrippyErrorKind::Connection)?) + .context(FrippyErrorKind::Connection)?; + + Ok(()) } fn evaluate(&self, _: &IrcClient, command: PluginCommand) -> Result { diff --git a/src/plugins/factoids/database.rs b/src/plugins/factoids/database.rs index 6cd979e..ec8ed3e 100644 --- a/src/plugins/factoids/database.rs +++ b/src/plugins/factoids/database.rs @@ -45,7 +45,7 @@ pub trait Database: Send + Sync { } // HashMap -impl Database for HashMap<(String, i32), Factoid> { +impl Database for HashMap<(String, i32), Factoid, S> { fn insert_factoid(&mut self, factoid: &NewFactoid) -> Result<(), FactoidsError> { let factoid = Factoid { name: factoid.name.to_owned(), diff --git a/src/plugins/factoids/mod.rs b/src/plugins/factoids/mod.rs index e3b28fc..eb9c10d 100644 --- a/src/plugins/factoids/mod.rs +++ b/src/plugins/factoids/mod.rs @@ -51,10 +51,10 @@ impl Factoids { let tm = time::now().to_timespec(); let factoid = database::NewFactoid { - name: name, + name, idx: count, - content: content, - author: author, + content, + author, created: NaiveDateTime::from_timestamp(tm.sec, 0u32), }; @@ -91,7 +91,7 @@ impl Factoids { } fn remove(&self, command: &mut PluginCommand) -> Result<&str, FactoidsError> { - if command.tokens.len() < 1 { + if command.tokens.is_empty() { Err(ErrorKind::InvalidCommand)?; } @@ -165,7 +165,7 @@ impl Factoids { } fn exec(&self, mut command: PluginCommand) -> Result { - if command.tokens.len() < 1 { + if command.tokens.is_empty() { Err(ErrorKind::InvalidIndex)? } else { let name = command.tokens.remove(0); @@ -251,24 +251,25 @@ impl Plugin for Factoids { tokens: t, }; - Ok(match self.exec(c) { - Ok(f) => client + if let Ok(f) = self.exec(c) { + client .send_privmsg(&message.response_target().unwrap(), &f) - .context(FrippyErrorKind::Connection)?, - Err(_) => (), - }) - } else { - Ok(()) + .context(FrippyErrorKind::Connection)?; + } } + + Ok(()) } fn command(&self, client: &IrcClient, mut command: PluginCommand) -> Result<(), FrippyError> { use self::FactoidResponse::{Private, Public}; if command.tokens.is_empty() { - return Ok(client + client .send_notice(&command.source, "Invalid command") - .context(FrippyErrorKind::Connection)?); + .context(FrippyErrorKind::Connection)?; + + return Ok(()); } let target = command.target.clone(); @@ -280,14 +281,14 @@ impl Plugin for Factoids { "fromurl" => self.add_from_url(&mut command) .map(|s| Private(s.to_owned())), "remove" => self.remove(&mut command).map(|s| Private(s.to_owned())), - "get" => self.get(&command).map(|s| Public(s)), - "info" => self.info(&command).map(|s| Public(s)), - "exec" => self.exec(command).map(|s| Public(s)), + "get" => self.get(&command).map(Public), + "info" => self.info(&command).map(Public), + "exec" => self.exec(command).map(Public), "help" => Ok(Private(self.help().to_owned())), _ => Err(ErrorKind::InvalidCommand.into()), }; - Ok(match result { + match result { Ok(v) => match v { Public(m) => client .send_privmsg(&target, &m) @@ -303,7 +304,9 @@ impl Plugin for Factoids { .context(FrippyErrorKind::Connection)?; Err(e).context(FrippyErrorKind::Factoids)? } - }) + } + + Ok(()) } fn evaluate(&self, _: &IrcClient, _: PluginCommand) -> Result { diff --git a/src/plugins/factoids/utils.rs b/src/plugins/factoids/utils.rs index 89f5d6c..48ec385 100644 --- a/src/plugins/factoids/utils.rs +++ b/src/plugins/factoids/utils.rs @@ -5,8 +5,8 @@ use std::time::Duration; use super::rlua::Error as LuaError; use super::rlua::Lua; -use utils::Url; use utils::error::ErrorKind::Connection; +use utils::Url; use failure::Fail; diff --git a/src/plugins/help.rs b/src/plugins/help.rs index b75086f..f048a8e 100644 --- a/src/plugins/help.rs +++ b/src/plugins/help.rs @@ -25,14 +25,16 @@ impl Plugin for Help { } fn command(&self, client: &IrcClient, command: PluginCommand) -> Result<(), FrippyError> { - Ok(client + client .send_notice( &command.source, "Available commands: help, currency, tell, factoids, remind\r\n\ For more detailed help call help on the specific command.\r\n\ Example: 'currency help'", ) - .context(FrippyErrorKind::Connection)?) + .context(FrippyErrorKind::Connection)?; + + Ok(()) } fn evaluate(&self, _: &IrcClient, _: PluginCommand) -> Result { diff --git a/src/plugins/keepnick.rs b/src/plugins/keepnick.rs index aa2e485..a728c63 100644 --- a/src/plugins/keepnick.rs +++ b/src/plugins/keepnick.rs @@ -56,12 +56,14 @@ impl Plugin for KeepNick { } fn command(&self, client: &IrcClient, command: PluginCommand) -> Result<(), FrippyError> { - Ok(client + client .send_notice( &command.source, "This Plugin does not implement any commands.", ) - .context(FrippyErrorKind::Connection)?) + .context(FrippyErrorKind::Connection)?; + + Ok(()) } fn evaluate(&self, _: &IrcClient, _: PluginCommand) -> Result { diff --git a/src/plugins/remind/database.rs b/src/plugins/remind/database.rs index e434ec0..97d93e8 100644 --- a/src/plugins/remind/database.rs +++ b/src/plugins/remind/database.rs @@ -1,5 +1,5 @@ -use std::collections::HashMap; use std::collections::hash_map::Entry; +use std::collections::HashMap; use std::fmt; #[cfg(feature = "mysql")] @@ -66,7 +66,7 @@ pub trait Database: Send + Sync { } // HashMap -impl Database for HashMap { +impl Database for HashMap { fn insert_event(&mut self, event: &NewEvent) -> Result { let mut id = 0; while self.contains_key(&id) { @@ -74,11 +74,11 @@ impl Database for HashMap { } let event = Event { - id: id, + id, receiver: event.receiver.to_owned(), content: event.content.to_owned(), author: event.author.to_owned(), - time: event.time.clone(), + time: *event.time, repeat: event.repeat, }; @@ -103,7 +103,7 @@ impl Database for HashMap { let mut events = Vec::new(); for (_, event) in self.iter() { - if &event.time < time { + if event.time < *time { events.push(event.clone()) } } @@ -132,9 +132,7 @@ impl Database for HashMap { } fn get_event(&self, id: i64) -> Result { - Ok(self.get(&id) - .map(|ev| ev.clone()) - .ok_or(ErrorKind::NotFound)?) + Ok(self.get(&id).cloned().ok_or(ErrorKind::NotFound)?) } fn delete_event(&mut self, id: i64) -> Result<(), RemindError> { diff --git a/src/plugins/remind/mod.rs b/src/plugins/remind/mod.rs index fada1fb..fa7cf70 100644 --- a/src/plugins/remind/mod.rs +++ b/src/plugins/remind/mod.rs @@ -98,7 +98,7 @@ impl Remind { let events = Arc::new(RwLock::new(db)); Remind { - events: events, + events, has_reminder: RwLock::new(false), } } @@ -106,17 +106,17 @@ impl Remind { fn user_cmd(&self, command: PluginCommand) -> Result { let parser = CommandParser::parse_target(command.tokens)?; - self.set(parser, &command.source) + self.set(&parser, &command.source) } fn me_cmd(&self, command: PluginCommand) -> Result { let source = command.source.clone(); let parser = CommandParser::with_target(command.tokens, command.source)?; - self.set(parser, &source) + self.set(&parser, &source) } - fn set(&self, parser: CommandParser, author: &str) -> Result { + fn set(&self, parser: &CommandParser, author: &str) -> Result { debug!("parser: {:?}", parser); let target = parser.get_target(); @@ -125,7 +125,7 @@ impl Remind { let event = database::NewEvent { receiver: target, content: &parser.get_message(), - author: author, + author, time: &time, repeat: parser .get_repeat(Duration::from_secs(600))? @@ -212,9 +212,10 @@ impl Plugin for Remind { fn command(&self, client: &IrcClient, mut command: PluginCommand) -> Result<(), FrippyError> { if command.tokens.is_empty() { - return Ok(client + client .send_notice(&command.source, &ErrorKind::InvalidCommand.to_string()) - .context(FrippyErrorKind::Connection)?); + .context(FrippyErrorKind::Connection)?; + return Ok(()); } let source = command.source.clone(); @@ -229,7 +230,7 @@ impl Plugin for Remind { _ => Err(ErrorKind::InvalidCommand.into()), }; - let result = match response { + match response { Ok(msg) => client .send_notice(&source, &msg) .context(FrippyErrorKind::Connection)?, @@ -242,9 +243,9 @@ impl Plugin for Remind { Err(e).context(FrippyErrorKind::Remind)? } - }; + } - Ok(result) + Ok(()) } fn evaluate(&self, _: &IrcClient, _: PluginCommand) -> Result { diff --git a/src/plugins/remind/parser.rs b/src/plugins/remind/parser.rs index eb39d04..d6e8574 100644 --- a/src/plugins/remind/parser.rs +++ b/src/plugins/remind/parser.rs @@ -99,36 +99,26 @@ impl CommandParser { use self::ParseState::*; let string = Some(string); match state { - &On if self.on_date.is_none() => { - return Ok(CommandParser { - on_date: string, - ..self - }) - } - &At if self.at_time.is_none() => { - return Ok(CommandParser { - at_time: string, - ..self - }) - } - &In if self.in_duration.is_none() => { - return Ok(CommandParser { - in_duration: string, - ..self - }) - } - &Msg if self.message.is_none() => { - return Ok(CommandParser { - message: string, - ..self - }) - } - &Every if self.every_time.is_none() => { - return Ok(CommandParser { - every_time: string, - ..self - }) - } + On if self.on_date.is_none() => Ok(CommandParser { + on_date: string, + ..self + }), + At if self.at_time.is_none() => Ok(CommandParser { + at_time: string, + ..self + }), + In if self.in_duration.is_none() => Ok(CommandParser { + in_duration: string, + ..self + }), + Msg if self.message.is_none() => Ok(CommandParser { + message: string, + ..self + }), + Every if self.every_time.is_none() => Ok(CommandParser { + every_time: string, + ..self + }), _ => Err(ErrorKind::MissingMessage.into()), } } diff --git a/src/plugins/sed.rs b/src/plugins/sed.rs index f766809..368f5d4 100644 --- a/src/plugins/sed.rs +++ b/src/plugins/sed.rs @@ -26,7 +26,7 @@ pub struct Sed { impl Sed { pub fn new(per_channel: usize) -> Sed { Sed { - per_channel: per_channel, + per_channel, channel_messages: RwLock::new(HashMap::new()), } } @@ -35,7 +35,7 @@ impl Sed { let mut channel_messages = self.channel_messages.write(); let messages = channel_messages .entry(channel) - .or_insert(CircularQueue::with_capacity(self.per_channel)); + .or_insert_with(|| CircularQueue::with_capacity(self.per_channel)); messages.push(message); } @@ -150,12 +150,14 @@ impl Plugin for Sed { } fn command(&self, client: &IrcClient, command: PluginCommand) -> Result<(), FrippyError> { - Ok(client + client .send_notice( &command.source, "Currently this Plugin does not implement any commands.", ) - .context(FrippyErrorKind::Connection)?) + .context(FrippyErrorKind::Connection)?; + + Ok(()) } fn evaluate(&self, _: &IrcClient, _: PluginCommand) -> Result { diff --git a/src/plugins/tell/database.rs b/src/plugins/tell/database.rs index 75789e4..cbcb93d 100644 --- a/src/plugins/tell/database.rs +++ b/src/plugins/tell/database.rs @@ -45,7 +45,7 @@ pub trait Database: Send + Sync { } // HashMap -impl Database for HashMap> { +impl Database for HashMap, S> { fn insert_tell(&mut self, tell: &NewTellMessage) -> Result<(), TellError> { let tell = TellMessage { id: 0, diff --git a/src/plugins/tell/mod.rs b/src/plugins/tell/mod.rs index c681d43..225373e 100644 --- a/src/plugins/tell/mod.rs +++ b/src/plugins/tell/mod.rs @@ -1,4 +1,5 @@ use antidote::RwLock; +use irc::client::data::User; use irc::client::prelude::*; use chrono::NaiveDateTime; @@ -56,17 +57,19 @@ impl Tell { .list_channels() .expect("The irc crate should not be compiled with the \"nochanlists\" feature"); - if let Some(_) = channels + let find_receiver = |option: Option>| { + option.and_then(|users| { + users + .into_iter() + .find(|user| user.get_nickname().eq_ignore_ascii_case(&receiver)) + }) + }; + + if channels .iter() .map(|channel| client.list_users(&channel)) - .map(|option| { - option.and_then(|users| { - users - .into_iter() - .find(|user| user.get_nickname().eq_ignore_ascii_case(&receiver)) - }) - }) - .find(|option| option.is_some()) + .map(find_receiver) + .any(|option| option.is_some()) { online.push(receiver); continue; @@ -87,7 +90,7 @@ impl Tell { } Ok(if no_receiver && online.is_empty() { - format!("Invalid receiver.") + String::from("Invalid receiver.") } else { match online.len() { 0 => format!("Got it!"), @@ -207,28 +210,29 @@ impl Plugin for Tell { fn command(&self, client: &IrcClient, command: PluginCommand) -> Result<(), FrippyError> { if command.tokens.is_empty() { - return Ok(client + client .send_notice(&command.source, &self.invalid_command()) - .context(FrippyErrorKind::Connection)?); + .context(FrippyErrorKind::Connection)?; + return Ok(()); } let sender = command.source.to_owned(); - Ok(match command.tokens[0].as_ref() { + match command.tokens[0].as_ref() { "help" => client .send_notice(&command.source, &self.help()) - .context(FrippyErrorKind::Connection) - .into(), + .context(FrippyErrorKind::Connection), _ => match self.tell_command(client, command) { Ok(msg) => client .send_notice(&sender, &msg) .context(FrippyErrorKind::Connection), Err(e) => client .send_notice(&sender, &e.to_string()) - .context(FrippyErrorKind::Connection) - .into(), + .context(FrippyErrorKind::Connection), }, - }?) + }?; + + Ok(()) } fn evaluate(&self, _: &IrcClient, _: PluginCommand) -> Result { diff --git a/src/plugins/url.rs b/src/plugins/url.rs index 9724d70..971597d 100644 --- a/src/plugins/url.rs +++ b/src/plugins/url.rs @@ -64,11 +64,11 @@ impl Title { .map_err(|_| ErrorKind::HtmlDecoding.into()) } - fn find_ogtitle<'a>(body: &str) -> Result { + fn find_ogtitle(body: &str) -> Result { Self::find_by_delimiters(body, ["property=\"og:title\"", "content=\"", "\""]) } - fn find_title<'a>(body: &str) -> Result { + fn find_title(body: &str) -> Result { Self::find_by_delimiters(body, ["", ""]) } @@ -93,12 +93,12 @@ impl Title { Title(self.0.trim().replace('\n', "|").replace('\r', "|"), self.1) } - pub fn find_clean_ogtitle<'a>(body: &str, url: &str) -> Result { + pub fn find_clean_ogtitle(body: &str, url: &str) -> Result { let title = Self::find_ogtitle(body)?; Ok(title.get_usefulness(url).clean_up()) } - pub fn find_clean_title<'a>(body: &str, url: &str) -> Result { + pub fn find_clean_title(body: &str, url: &str) -> Result { let title = Self::find_title(body)?; Ok(title.get_usefulness(url).clean_up()) } @@ -107,7 +107,7 @@ impl Title { impl UrlTitles { /// If a file is larger than `max_kib` KiB the download is stopped pub fn new(max_kib: usize) -> Self { - UrlTitles { max_kib: max_kib } + UrlTitles { max_kib } } fn grep_url<'a>(&self, msg: &'a str) -> Option> { @@ -161,28 +161,27 @@ impl Plugin for UrlTitles { } fn execute_threaded(&self, client: &IrcClient, message: &Message) -> Result<(), FrippyError> { - match message.command { - Command::PRIVMSG(_, ref content) => { - let title = self.url(content).context(FrippyErrorKind::Url)?; - let response = format!("[URL] {}", title); + if let Command::PRIVMSG(_, ref content) = message.command { + let title = self.url(content).context(FrippyErrorKind::Url)?; + let response = format!("[URL] {}", title); - client - .send_privmsg(message.response_target().unwrap(), &response) - .context(FrippyErrorKind::Connection)?; - } - _ => (), + client + .send_privmsg(message.response_target().unwrap(), &response) + .context(FrippyErrorKind::Connection)?; } Ok(()) } fn command(&self, client: &IrcClient, command: PluginCommand) -> Result<(), FrippyError> { - Ok(client + client .send_notice( &command.source, "This Plugin does not implement any commands.", ) - .context(FrippyErrorKind::Connection)?) + .context(FrippyErrorKind::Connection)?; + + Ok(()) } fn evaluate(&self, _: &IrcClient, command: PluginCommand) -> Result { diff --git a/src/utils.rs b/src/utils.rs index e64b329..3275e62 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -53,10 +53,7 @@ impl<'a> Url<'a> { /// that the limit set by max_kib() was reached. pub fn request(&self) -> Result { let client = if let Some(timeout) = self.timeout { - ClientBuilder::new() - .timeout(timeout) - .build() - .unwrap() + ClientBuilder::new().timeout(timeout).build().unwrap() } else { Client::new() }; -- cgit v1.2.3-70-g09d2