From 95a73b9471abbbd31a0f7188372e476faa8e9b7b Mon Sep 17 00:00:00 2001 From: Magnus Hovland Hoff Date: Sun, 3 Apr 2022 14:29:35 +0200 Subject: [PATCH] cargo clippy --fix --- build.rs | 6 ++--- src/build_config.rs | 4 ++-- src/db.rs | 6 ++--- src/merge/chunk.rs | 1 - src/merge/chunk_iterator.rs | 4 +--- src/merge/mod.rs | 3 --- src/merge/output.rs | 6 ++--- src/models.rs | 2 -- src/rendering.rs | 2 +- src/resources/about_resource.rs | 4 ++-- src/resources/article_resource.rs | 14 +++++------- src/resources/article_revision_resource.rs | 6 ++--- src/resources/changes_resource.rs | 14 +++++------- src/resources/diff_resource.rs | 6 ++--- src/resources/html_resource.rs | 2 +- src/resources/new_article_resource.rs | 8 +++---- src/resources/pagination.rs | 4 ++-- src/resources/search_resource.rs | 16 +++++--------- src/resources/sitemap_resource.rs | 2 +- src/resources/temporary_redirect_resource.rs | 2 +- src/site.rs | 22 +++++-------------- src/state.rs | 23 +++++++++----------- src/theme.rs | 5 ----- src/web/lookup.rs | 2 -- src/web/resource.rs | 2 -- src/wiki_lookup.rs | 6 ++--- 26 files changed, 62 insertions(+), 110 deletions(-) diff --git a/build.rs b/build.rs index 9c85c2e..7ddd939 100644 --- a/build.rs +++ b/build.rs @@ -34,10 +34,8 @@ fn main() { let _ignore_failure = std::fs::remove_file(db_path); - let connection = SqliteConnection::establish(db_path).expect(&format!( - "Error esablishing a database connection to {}", - db_path - )); + let connection = SqliteConnection::establish(db_path) + .unwrap_or_else(|_| panic!("Error esablishing a database connection to {}", db_path)); // Integer is a dummy placeholder. Compiling fails when passing (). diesel::expression::sql_literal::sql::("PRAGMA foreign_keys = ON") diff --git a/src/build_config.rs b/src/build_config.rs index cdbeabe..6f32195 100644 --- a/src/build_config.rs +++ b/src/build_config.rs @@ -23,7 +23,7 @@ lazy_static! { #[cfg(feature = "dynamic-assets")] components.push("dynamic-assets".into()); - if let None = option_env!("CONTINUOUS_INTEGRATION") { + if option_env!("CONTINUOUS_INTEGRATION").is_none() { components.push("local-build".into()); } @@ -43,7 +43,7 @@ lazy_static! { )); } - if components.len() > 0 { + if !components.is_empty() { format!("{} ({})", env!("CARGO_PKG_VERSION"), components.join(" ")) } else { env!("CARGO_PKG_VERSION").to_string() diff --git a/src/db.rs b/src/db.rs index f81d2e3..d69ce50 100644 --- a/src/db.rs +++ b/src/db.rs @@ -23,17 +23,17 @@ impl CustomizeConnection for SqliteInitial fn on_acquire(&self, conn: &mut SqliteConnection) -> Result<(), r2d2_diesel::Error> { sql::("PRAGMA foreign_keys = ON") .execute(conn) - .map_err(|x| r2d2_diesel::Error::QueryError(x))?; + .map_err(r2d2_diesel::Error::QueryError)?; sqlfunc::markdown_to_fts::register_impl(conn, |text: String| { rendering::render_markdown_for_fts(&text) }) - .map_err(|x| r2d2_diesel::Error::QueryError(x))?; + .map_err(r2d2_diesel::Error::QueryError)?; sqlfunc::theme_from_str_hash::register_impl(conn, |title: String| { theme::theme_from_str_hash(&title) }) - .map_err(|x| r2d2_diesel::Error::QueryError(x))?; + .map_err(r2d2_diesel::Error::QueryError)?; Ok(()) } diff --git a/src/merge/chunk.rs b/src/merge/chunk.rs index 0a87703..0933e69 100644 --- a/src/merge/chunk.rs +++ b/src/merge/chunk.rs @@ -1,4 +1,3 @@ -use diff; use std::fmt::Debug; #[derive(Debug, PartialEq)] diff --git a/src/merge/chunk_iterator.rs b/src/merge/chunk_iterator.rs index 6f603aa..394e44d 100644 --- a/src/merge/chunk_iterator.rs +++ b/src/merge/chunk_iterator.rs @@ -1,6 +1,5 @@ use std::fmt::Debug; -use diff; use diff::Result::*; use super::chunk::Chunk; @@ -68,7 +67,7 @@ where return Some(chunk); } _ => { - if self.left.len() > 0 || self.right.len() > 0 { + if !self.left.is_empty() || !self.right.is_empty() { let chunk = Chunk(self.left, self.right); self.left = &self.left[self.left.len()..]; self.right = &self.right[self.right.len()..]; @@ -84,7 +83,6 @@ where #[cfg(test)] mod test { use super::*; - use diff; #[test] fn simple_case() { diff --git a/src/merge/mod.rs b/src/merge/mod.rs index ebadd3e..835d1da 100644 --- a/src/merge/mod.rs +++ b/src/merge/mod.rs @@ -4,8 +4,6 @@ mod output; use std::fmt::Debug; -use diff; - use self::chunk_iterator::ChunkIterator; use self::output::Output::Resolved; use self::output::*; @@ -132,7 +130,6 @@ pub fn merge_chars<'a>(a: &'a str, o: &'a str, b: &'a str) -> MergeResult #[cfg(test)] mod test { - use diff; use super::output::Output::*; use super::output::*; diff --git a/src/merge/output.rs b/src/merge/output.rs index bb697aa..6009f56 100644 --- a/src/merge/output.rs +++ b/src/merge/output.rs @@ -1,6 +1,5 @@ use std::fmt::Debug; -use diff; use diff::Result::*; use super::chunk::Chunk; @@ -67,17 +66,16 @@ pub fn resolve<'a, Item: 'a + Debug + PartialEq + Copy>(chunk: Chunk<'a, Item>) return Output::Resolved(choose_right(chunk.0)); } - return Output::Conflict( + Output::Conflict( choose_right(chunk.0), choose_left(chunk.0), choose_right(chunk.1), - ); + ) } #[cfg(test)] mod test { use super::*; - use diff::Result::*; #[test] fn empty() { diff --git a/src/models.rs b/src/models.rs index f5f70d6..9c81c3e 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,5 +1,3 @@ -use chrono; - use crate::theme::Theme; fn slug_link(slug: &str) -> &str { diff --git a/src/rendering.rs b/src/rendering.rs index 5fbb2b9..1a78eec 100644 --- a/src/rendering.rs +++ b/src/rendering.rs @@ -34,7 +34,7 @@ pub fn render_markdown_for_fts(src: &str) -> String { buf.push_str(&uri.replace(is_html_special, " ")); buf.push_str(") "); } - _ => buf.push_str(" "), + _ => buf.push(' '), } } diff --git a/src/resources/about_resource.rs b/src/resources/about_resource.rs index 63122ec..7b44afa 100644 --- a/src/resources/about_resource.rs +++ b/src/resources/about_resource.rs @@ -1,5 +1,5 @@ use futures::{self, Future}; -use hyper; + use hyper::header::ContentType; use hyper::server::*; @@ -88,7 +88,7 @@ impl Resource for AboutResource { None, // Hmm, should perhaps accept `base` as argument "About Sausagewiki", Template { - deps: &*LICENSE_INFOS, + deps: *LICENSE_INFOS, }, ) .to_string(), diff --git a/src/resources/article_resource.rs b/src/resources/article_resource.rs index 90f43d4..b75a194 100644 --- a/src/resources/article_resource.rs +++ b/src/resources/article_resource.rs @@ -1,10 +1,8 @@ use chrono::{DateTime, Local, TimeZone}; use futures::{self, Future}; -use hyper; + use hyper::header::{ContentType, Location}; use hyper::server::*; -use serde_json; -use serde_urlencoded; use crate::assets::ScriptJs; use crate::mimes::*; @@ -90,7 +88,7 @@ pub fn last_updated(article_id: i32, created: &DateTime, author: Option<& .into_link() ), author: author.map(|author| Author { - author: &author, + author, history: format!( "_changes{}", QueryParameters::default() @@ -134,7 +132,7 @@ impl Resource for ArticleResource { last_updated: Some(&last_updated( data.article_id, &Local.from_utc_datetime(&data.created), - data.author.as_ref().map(|x| &**x), + data.author.as_deref(), )), edit: self.edit, cancel_url: Some(data.link()), @@ -213,7 +211,7 @@ impl Resource for ArticleResource { last_updated: &last_updated( updated.article_id, &Local.from_utc_datetime(&updated.created), - updated.author.as_ref().map(|x| &**x), + updated.author.as_deref(), ), }) .expect("Should never fail"), @@ -245,7 +243,7 @@ impl Resource for ArticleResource { last_updated: &last_updated( base_article.article_id, &Local.from_utc_datetime(&base_article.created), - base_article.author.as_ref().map(|x| &**x), + base_article.author.as_deref(), ), }) .expect("Should never fail"), @@ -301,7 +299,7 @@ impl Resource for ArticleResource { last_updated: Some(&last_updated( base_article.article_id, &Local.from_utc_datetime(&base_article.created), - base_article.author.as_ref().map(|x| &**x), + base_article.author.as_deref(), )), edit: true, cancel_url: Some(base_article.link()), diff --git a/src/resources/article_revision_resource.rs b/src/resources/article_revision_resource.rs index 43dcdc1..7674dce 100644 --- a/src/resources/article_revision_resource.rs +++ b/src/resources/article_revision_resource.rs @@ -1,6 +1,6 @@ use chrono::{DateTime, Local, TimeZone}; use futures::{self, Future}; -use hyper; + use hyper::header::ContentType; use hyper::server::*; @@ -55,7 +55,7 @@ pub fn timestamp_and_author( .into_link() ), author: author.map(|author| Author { - author: &author, + author, history: format!( "_changes{}", QueryParameters::default() @@ -107,7 +107,7 @@ impl Resource for ArticleRevisionResource { data.sequence_number, data.article_id, &Local.from_utc_datetime(&data.created), - data.author.as_ref().map(|x| &**x), + data.author.as_deref(), ), diff_link: if data.revision > 1 { Some(format!( diff --git a/src/resources/changes_resource.rs b/src/resources/changes_resource.rs index cc7dd94..daf2b7c 100644 --- a/src/resources/changes_resource.rs +++ b/src/resources/changes_resource.rs @@ -1,10 +1,8 @@ -use diesel; use futures::future::{done, finished}; use futures::{self, Future}; -use hyper; + use hyper::header::ContentType; use hyper::server::*; -use serde_urlencoded; use crate::mimes::*; use crate::schema::article_revisions; @@ -75,7 +73,7 @@ impl QueryParameters { pub fn into_link(self) -> String { let args = serde_urlencoded::to_string(self).expect("Serializing to String cannot fail"); - if args.len() > 0 { + if !args.is_empty() { format!("?{}", args) } else { "_changes".to_owned() @@ -127,7 +125,7 @@ impl ChangesLookup { let limit = match params.limit { None => Ok(DEFAULT_LIMIT), - Some(x) if 1 <= x && x <= 100 => Ok(x), + Some(x) if (1..=100).contains(&x) => Ok(x), _ => Err("`limit` argument must be in range [1, 100]"), }?; @@ -309,7 +307,7 @@ impl Resource for ChangesResource { fn subject_clause(&self) -> String { match self.resource.article_id { Some(x) => format!(" this article", x), - None => format!(" the wiki"), + None => " the wiki".to_string(), } } @@ -332,8 +330,8 @@ impl Resource for ChangesResource { } let (before, article_id, author, limit) = ( - self.before.clone(), - self.article_id.clone(), + self.before, + self.article_id, self.author.clone(), self.limit, ); diff --git a/src/resources/diff_resource.rs b/src/resources/diff_resource.rs index 90a99e9..f0761dc 100644 --- a/src/resources/diff_resource.rs +++ b/src/resources/diff_resource.rs @@ -1,12 +1,10 @@ use std::fmt; -use diff; use futures::future::done; use futures::{self, Future}; -use hyper; + use hyper::header::ContentType; use hyper::server::*; -use serde_urlencoded; use crate::mimes::*; use crate::models::ArticleRevision; @@ -127,7 +125,7 @@ impl Resource for DiffResource { let consecutive = self.to.revision - self.from.revision == 1; let author = match consecutive { - true => self.to.author.as_ref().map(|x| &**x), + true => self.to.author.as_deref(), false => None, }; diff --git a/src/resources/html_resource.rs b/src/resources/html_resource.rs index 35452ac..0c393d3 100644 --- a/src/resources/html_resource.rs +++ b/src/resources/html_resource.rs @@ -1,5 +1,5 @@ use futures::{self, Future}; -use hyper; + use hyper::header::ContentType; use hyper::server::*; diff --git a/src/resources/new_article_resource.rs b/src/resources/new_article_resource.rs index e891e10..6e99f6d 100644 --- a/src/resources/new_article_resource.rs +++ b/src/resources/new_article_resource.rs @@ -1,9 +1,7 @@ use futures::{self, Future}; -use hyper; + use hyper::header::{ContentType, Location}; use hyper::server::*; -use serde_json; -use serde_urlencoded; use crate::assets::ScriptJs; use crate::mimes::*; @@ -100,7 +98,7 @@ impl Resource for NewArticleResource { revision: NEW, last_updated: None, edit: self.edit, - cancel_url: self.slug.as_ref().map(|x| &**x), + cancel_url: self.slug.as_deref(), title: &title, raw: "", rendered: EMPTY_ARTICLE_MESSAGE, @@ -182,7 +180,7 @@ impl Resource for NewArticleResource { last_updated: &super::article_resource::last_updated( updated.article_id, &Local.from_utc_datetime(&updated.created), - updated.author.as_ref().map(|x| &**x), + updated.author.as_deref(), ), }) .expect("Should never fail"), diff --git a/src/resources/pagination.rs b/src/resources/pagination.rs index 26235ca..fbaf7b5 100644 --- a/src/resources/pagination.rs +++ b/src/resources/pagination.rs @@ -40,9 +40,9 @@ impl PaginationStruct { pub fn _from_str<'a, T: serde::Deserialize<'a>>(s: &'a str) -> Result, Error> { let pagination: PaginationStruct = serde_urlencoded::from_str(s).map_err(|_| Error)?; // TODO Proper error reporting - Ok(pagination.into_enum()?) + pagination.into_enum() } pub fn from_fields(after: Option, before: Option) -> Result, Error> { - Ok(PaginationStruct { after, before }.into_enum()?) + PaginationStruct { after, before }.into_enum() } diff --git a/src/resources/search_resource.rs b/src/resources/search_resource.rs index e481897..d478dea 100644 --- a/src/resources/search_resource.rs +++ b/src/resources/search_resource.rs @@ -1,9 +1,7 @@ use futures::{self, Future}; -use hyper; + use hyper::header::{Accept, ContentType}; use hyper::server::*; -use serde_json; -use serde_urlencoded; use crate::mimes::*; use crate::models::SearchResult; @@ -56,7 +54,7 @@ impl QueryParameters { pub fn into_link(self) -> String { let args = serde_urlencoded::to_string(self).expect("Serializing to String cannot fail"); - if args.len() > 0 { + if !args.is_empty() { format!("_search?{}", args) } else { "_search".to_owned() @@ -185,11 +183,7 @@ impl Resource for SearchResource { } // TODO: Show a search "front page" when no query is given: - let query = self - .query - .as_ref() - .map(|x| x.clone()) - .unwrap_or("".to_owned()); + let query = self.query.as_ref().cloned().unwrap_or("".to_owned()); let data = self.state.search_query( query, @@ -224,7 +218,7 @@ impl Resource for SearchResource { match &self.response_type { &ResponseType::Json => Ok(head.with_body( serde_json::to_string(&JsonResponse { - query: self.query.as_ref().map(|x| &**x).unwrap_or(""), + query: self.query.as_deref().unwrap_or(""), hits: &data, prev, next, @@ -236,7 +230,7 @@ impl Resource for SearchResource { None, // Hmm, should perhaps accept `base` as argument "Search", &Template { - query: self.query.as_ref().map(|x| &**x).unwrap_or(""), + query: self.query.as_deref().unwrap_or(""), hits: &data.iter().enumerate().collect::>(), prev, next, diff --git a/src/resources/sitemap_resource.rs b/src/resources/sitemap_resource.rs index 3e860d7..a8ff3d7 100644 --- a/src/resources/sitemap_resource.rs +++ b/src/resources/sitemap_resource.rs @@ -1,5 +1,5 @@ use futures::{self, Future}; -use hyper; + use hyper::header::ContentType; use hyper::server::*; diff --git a/src/resources/temporary_redirect_resource.rs b/src/resources/temporary_redirect_resource.rs index 2952f86..e214ec8 100644 --- a/src/resources/temporary_redirect_resource.rs +++ b/src/resources/temporary_redirect_resource.rs @@ -1,5 +1,5 @@ use futures::{self, Future}; -use hyper; + use hyper::header::Location; use hyper::server::*; diff --git a/src/site.rs b/src/site.rs index 2dc77d2..c33e0fa 100644 --- a/src/site.rs +++ b/src/site.rs @@ -4,7 +4,7 @@ use std::fmt; use futures::{self, Future}; -use hyper; + use hyper::header::{Accept, ContentType, Server}; use hyper::mime; use hyper::server::*; @@ -118,12 +118,12 @@ impl Site { } fn root_base_from_request_uri(path: &str) -> Option { - assert!(path.starts_with("/")); + assert!(path.starts_with('/')); let slashes = path[1..].matches('/').count(); match slashes { 0 => None, - n => Some(::std::iter::repeat("../").take(n).collect()), + n => Some("../".repeat(n)), } } @@ -143,10 +143,7 @@ impl Service for Site { false => None, }; - let accept_header = headers - .get() - .map(|x: &Accept| x.clone()) - .unwrap_or(Accept(vec![])); + let accept_header = headers.get().cloned().unwrap_or(Accept(vec![])); let base = root_base_from_request_uri(uri.path()); let base2 = base.clone(); // Bah, stupid clone @@ -167,16 +164,9 @@ impl Service for Site { _ => Box::new(futures::finished(resource.method_not_allowed())), } } - None => Box::new(futures::finished(Self::not_found( - base.as_ref().map(|x| &**x), - ))), - }) - .or_else(move |err| { - Ok(Self::internal_server_error( - base2.as_ref().map(|x| &**x), - err, - )) + None => Box::new(futures::finished(Self::not_found(base.as_deref()))), }) + .or_else(move |err| Ok(Self::internal_server_error(base2.as_deref(), err))) .map(|response| response.with_header(SERVER.clone())), ) } diff --git a/src/state.rs b/src/state.rs index 9608890..41ff97e 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,6 +1,3 @@ -use std; - -use diesel; use diesel::prelude::*; use diesel::sqlite::SqliteConnection; use futures_cpupool::{self, CpuFuture}; @@ -72,7 +69,7 @@ fn decide_slug( let base_slug = ::slug::slugify(title); if let Some(prev_slug) = prev_slug { - if prev_slug == "" { + if prev_slug.is_empty() { // Never give a non-empty slug to the front page return Ok(String::new()); } @@ -313,7 +310,7 @@ impl<'a> SyncState<'a> { theme: Option, ) -> Result { if title.is_empty() { - Err("title cannot be empty")?; + return Err("title cannot be empty".into()); } self.db_connection.transaction(|| { @@ -332,7 +329,7 @@ impl<'a> SyncState<'a> { // This scheme would make POST idempotent. if base_revision > latest_revision { - Err("This edit is based on a future version of the article")?; + return Err("This edit is based on a future version of the article".into()); } let theme = theme.unwrap_or(prev_theme); @@ -375,7 +372,7 @@ impl<'a> SyncState<'a> { slug: &slug, title: &title, body: &body, - author: author.as_ref().map(|x| &**x), + author: author.as_deref(), latest: true, theme, }) @@ -399,7 +396,7 @@ impl<'a> SyncState<'a> { theme: Theme, ) -> Result { if title.is_empty() { - Err("title cannot be empty")?; + return Err("title cannot be empty".into()); } self.db_connection.transaction(|| { @@ -425,7 +422,7 @@ impl<'a> SyncState<'a> { article_id, "", &title, - target_slug.as_ref().map(|x| &**x), + target_slug.as_deref(), )?; let new_revision = 1; @@ -437,7 +434,7 @@ impl<'a> SyncState<'a> { slug: &slug, title: &title, body: &body, - author: author.as_ref().map(|x| &**x), + author: author.as_deref(), latest: true, theme, }) @@ -705,7 +702,7 @@ mod test { .update_article( article.article_id, first_edit.revision, - article.title.clone(), + article.title, "Newer body".into(), None, Some(Theme::Amber), @@ -891,7 +888,7 @@ mod test { theme, }) => { assert_eq!(first_edit.revision, base_article.revision); - assert_eq!(title, merge::MergeResult::Clean(article.title.clone())); + assert_eq!(title, merge::MergeResult::Clean(article.title)); assert_eq!( body, merge::MergeResult::Conflicted(vec![merge::Output::Conflict( @@ -929,7 +926,7 @@ mod test { .update_article( article.article_id, article.revision, - article.title.clone(), + article.title, "a\nb\ny\nc\n".into(), None, Some(Theme::Cyan), diff --git a/src/theme.rs b/src/theme.rs index 4871e70..8a147fe 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -5,8 +5,6 @@ use diesel::deserialize::{self, FromSql}; use diesel::serialize::{self, Output, ToSql}; use diesel::sql_types::Text; use diesel::sqlite::Sqlite; -use rand; -use seahash; #[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] // Serde #[serde(rename_all = "kebab-case")] @@ -97,9 +95,6 @@ mod test { use diesel::prelude::*; use diesel::sql_query; use diesel::sql_types::Text; - use serde_json; - use serde_plain; - use serde_urlencoded; use super::*; diff --git a/src/web/lookup.rs b/src/web/lookup.rs index c874fc7..858a48d 100644 --- a/src/web/lookup.rs +++ b/src/web/lookup.rs @@ -1,5 +1,3 @@ -use futures; - pub trait Lookup { type Resource; type Error; diff --git a/src/web/resource.rs b/src/web/resource.rs index ec707ca..9bbb5f8 100644 --- a/src/web/resource.rs +++ b/src/web/resource.rs @@ -1,8 +1,6 @@ -use futures; use futures::{Future, Stream}; use hyper::server::Response; use hyper::{self, header, mime, server}; -use std; lazy_static! { static ref TEXT_PLAIN: mime::Mime = "text/plain;charset=utf-8".parse().unwrap(); diff --git a/src/wiki_lookup.rs b/src/wiki_lookup.rs index 93893c0..8926dd2 100644 --- a/src/wiki_lookup.rs +++ b/src/wiki_lookup.rs @@ -81,7 +81,7 @@ fn fs_lookup( use std::fs::File; use std::io::prelude::*; - let extension = path.rsplitn(2, ".").next(); + let extension = path.rsplit_once('.').map(|x| x.1); let content_type = match extension { Some("html") => "text/html", @@ -263,10 +263,10 @@ impl Lookup for WikiLookup { type Future = Box, Error = Self::Error>>; fn lookup(&self, path: &str, query: Option<&str>) -> Self::Future { - assert!(path.starts_with("/")); + assert!(path.starts_with('/')); let path = &path[1..]; - if path.starts_with("_") { + if path.starts_with('_') { self.reserved_lookup(path, query) } else { self.article_lookup(path, query)