From 66fd6850628073702a5b3517987902a060ece878 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Fri, 2 Oct 2020 07:37:27 +0100 Subject: [PATCH] fix clippy lints (#56) --- src/engine/fut/analyze.rs | 2 +- src/engine/fut/crawl.rs | 10 +++----- src/engine/machines/crawler.rs | 2 +- src/engine/mod.rs | 8 +++--- src/interactors/crates.rs | 21 +++++---------- src/interactors/github.rs | 8 +++--- src/interactors/mod.rs | 7 +++-- src/interactors/rustsec.rs | 10 +++----- src/models/crates.rs | 12 +++------ src/models/repo.rs | 12 ++++----- src/parsers/manifest.rs | 33 ++++++++++-------------- src/server/mod.rs | 47 ++++++++++++++++------------------ src/server/views/html/index.rs | 2 +- src/server/views/html/mod.rs | 3 +-- src/utils/cache.rs | 4 +-- 15 files changed, 78 insertions(+), 103 deletions(-) diff --git a/src/engine/fut/analyze.rs b/src/engine/fut/analyze.rs index a5d9f21..d090b59 100644 --- a/src/engine/fut/analyze.rs +++ b/src/engine/fut/analyze.rs @@ -46,7 +46,7 @@ pub async fn analyze_dependencies( let deps_iter = main_deps.chain(dev_deps).chain(build_deps); let mut releases = engine.fetch_releases(deps_iter); - for release in releases.next().await { + while let Some(release) = releases.next().await { let release = release?; analyzer.process(release) } diff --git a/src/engine/fut/crawl.rs b/src/engine/fut/crawl.rs index b851394..afbb10f 100644 --- a/src/engine/fut/crawl.rs +++ b/src/engine/fut/crawl.rs @@ -1,7 +1,7 @@ use std::{future::Future, mem, pin::Pin, task::Context, task::Poll}; use anyhow::Error; -use futures::{ready, Stream}; +use futures::{future::BoxFuture, ready, Stream}; use futures::{stream::FuturesOrdered, FutureExt}; use relative_path::RelativePathBuf; @@ -17,9 +17,7 @@ pub struct CrawlManifestFuture { engine: Engine, crawler: ManifestCrawler, #[pin] - futures: FuturesOrdered< - Pin> + Send>>, - >, + futures: FuturesOrdered>>, } impl CrawlManifestFuture { @@ -63,7 +61,7 @@ impl Future for CrawlManifestFuture { let future: Pin + Send>> = Box::pin( self.engine .retrieve_manifest_at_path(&self.repo_path, &path) - .map(move |contents| contents.map(|c| ((path, c)))), + .map(move |contents| contents.map(|c| (path, c))), ); self.futures.push(future); } @@ -71,7 +69,7 @@ impl Future for CrawlManifestFuture { self.poll(cx) } - Some(Err(err)) => Poll::Ready(Err(err.into())), + Some(Err(err)) => Poll::Ready(Err(err)), } } } diff --git a/src/engine/machines/crawler.rs b/src/engine/machines/crawler.rs index 69d4b6c..8eb7ba4 100644 --- a/src/engine/machines/crawler.rs +++ b/src/engine/machines/crawler.rs @@ -85,7 +85,7 @@ impl ManifestCrawler { .chain(deps.dev.iter()) .chain(deps.build.iter()) { - if let &CrateDep::Internal(ref path) = dep { + if let CrateDep::Internal(ref path) = dep { self.register_interest(base_path, path, output); } } diff --git a/src/engine/mod.rs b/src/engine/mod.rs index 380507a..0761f32 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -48,7 +48,7 @@ impl Service> for ServiceHttpClient { type Future = ResponseFuture; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { - self.0.poll_ready(cx).map_err(|err| err.into()) + self.0.poll_ready(cx) } fn call(&mut self, req: Request) -> Self::Future { @@ -97,7 +97,7 @@ impl Engine { ); Engine { - client: client.clone(), + client, logger, metrics, query_crate: Arc::new(Mutex::new(query_crate)), @@ -156,7 +156,7 @@ impl Engine { pub async fn get_popular_crates(&self) -> Result, Error> { let crates = self.get_popular_crates.lock().unwrap().call(()); let crates = crates.await?; - Ok(crates.clone()) + Ok(crates) } pub async fn analyze_repo_dependencies( @@ -264,7 +264,7 @@ impl Engine { .lock() .unwrap() .call(name) - .map(|resp| resp.map(|r| r.releases.clone())) + .map(|resp| resp.map(|r| r.releases)) }) .collect::>() } diff --git a/src/interactors/crates.rs b/src/interactors/crates.rs index ecd8cfc..c987f48 100644 --- a/src/interactors/crates.rs +++ b/src/interactors/crates.rs @@ -1,10 +1,8 @@ -use std::pin::Pin; -use std::{future::Future, task::Poll}; -use std::{str, task::Context}; +use std::{str, task::Context, task::Poll}; use anyhow::{anyhow, Error}; use futures::{ - future::{err, ok, ready}, + future::{err, ok, ready, BoxFuture}, TryFutureExt, }; use hyper::{ @@ -44,12 +42,7 @@ fn convert_pkgs( .map(|package| { let mut deps = CrateDeps::default(); for dep in package.deps { - match dep - .kind - .map(|k| k.clone()) - .unwrap_or_else(|| "normal".into()) - .as_ref() - { + match dep.kind.unwrap_or_else(|| "normal".into()).as_ref() { "normal" => deps .main .insert(dep.name.parse()?, CrateDep::External(dep.req)), @@ -62,13 +55,13 @@ fn convert_pkgs( Ok(CrateRelease { name: name.clone(), version: package.vers, - deps: deps, + deps, yanked: package.yanked, }) }) .collect::>()?; - Ok(QueryCrateResponse { releases: releases }) + Ok(QueryCrateResponse { releases }) } #[derive(Debug, Clone)] @@ -86,7 +79,7 @@ where { type Response = QueryCrateResponse; type Error = Error; - type Future = Pin> + Send>>; + type Future = BoxFuture<'static, Result>; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { self.0.poll_ready(cx).map_err(|err| err.into()) @@ -175,7 +168,7 @@ where { type Response = Vec; type Error = Error; - type Future = Pin> + Send>>; + type Future = BoxFuture<'static, Result>; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { self.0.poll_ready(cx).map_err(|err| err.into()) diff --git a/src/interactors/github.rs b/src/interactors/github.rs index 2c3ea12..a15bb4b 100644 --- a/src/interactors/github.rs +++ b/src/interactors/github.rs @@ -1,8 +1,8 @@ -use std::{future::Future, pin::Pin, task::Context, task::Poll}; +use std::{task::Context, task::Poll}; use anyhow::{anyhow, Error}; use futures::{ - future::{err, ok, ready}, + future::{err, ok, ready, BoxFuture}, TryFutureExt, }; use hyper::{ @@ -12,7 +12,7 @@ use serde::Deserialize; use crate::models::repo::{RepoPath, Repository}; -const GITHUB_API_BASE_URI: &'static str = "https://api.github.com"; +const GITHUB_API_BASE_URI: &str = "https://api.github.com"; #[derive(Deserialize)] struct GithubSearchResponse { @@ -41,7 +41,7 @@ where { type Response = Vec; type Error = Error; - type Future = Pin> + Send>>; + type Future = BoxFuture<'static, Result>; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { self.0.poll_ready(cx).map_err(|err| err.into()) diff --git a/src/interactors/mod.rs b/src/interactors/mod.rs index a2f8eec..9007c0b 100644 --- a/src/interactors/mod.rs +++ b/src/interactors/mod.rs @@ -1,9 +1,8 @@ -use std::{future::Future, task::Poll}; -use std::{pin::Pin, task::Context}; +use std::{task::Context, task::Poll}; use anyhow::{anyhow, Error}; use futures::{ - future::{err, ok, ready}, + future::{err, ok, ready, BoxFuture}, TryFutureExt, }; use hyper::{ @@ -27,7 +26,7 @@ where { type Response = String; type Error = Error; - type Future = Pin> + Send>>; + type Future = BoxFuture<'static, Result>; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { self.0.poll_ready(cx).map_err(|err| err.into()) diff --git a/src/interactors/rustsec.rs b/src/interactors/rustsec.rs index 7d28e63..c74f16e 100644 --- a/src/interactors/rustsec.rs +++ b/src/interactors/rustsec.rs @@ -1,7 +1,7 @@ -use std::{pin::Pin, sync::Arc, task::Context, task::Poll}; +use std::{sync::Arc, task::Context, task::Poll}; use anyhow::Error; -use futures::{future::ready, Future}; +use futures::{future::ready, future::BoxFuture}; use hyper::{service::Service, Body, Error as HyperError, Request, Response}; use rustsec::database::Database; @@ -15,7 +15,7 @@ where { type Response = Arc; type Error = Error; - type Future = Pin> + Send>>; + type Future = BoxFuture<'static, Result>; fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { // TODO: should be this when async client is used again @@ -28,9 +28,7 @@ where let _service = self.0.clone(); Box::pin(ready( - rustsec::Database::fetch() - .map(|db| Arc::new(db)) - .map_err(|err| err.into()), + rustsec::Database::fetch().map(Arc::new).map_err(Into::into), )) } } diff --git a/src/models/crates.rs b/src/models/crates.rs index c61a54f..09354bf 100644 --- a/src/models/crates.rs +++ b/src/models/crates.rs @@ -73,11 +73,7 @@ pub enum CrateDep { impl CrateDep { pub fn is_external(&self) -> bool { - if let &CrateDep::External(_) = self { - true - } else { - false - } + matches!(self, CrateDep::External(_)) } } @@ -124,7 +120,7 @@ impl AnalyzedDependencies { .main .iter() .filter_map(|(name, dep)| { - if let &CrateDep::External(ref req) = dep { + if let CrateDep::External(ref req) = dep { Some((name.clone(), AnalyzedDependency::new(req.clone()))) } else { None @@ -135,7 +131,7 @@ impl AnalyzedDependencies { .dev .iter() .filter_map(|(name, dep)| { - if let &CrateDep::External(ref req) = dep { + if let CrateDep::External(ref req) = dep { Some((name.clone(), AnalyzedDependency::new(req.clone()))) } else { None @@ -146,7 +142,7 @@ impl AnalyzedDependencies { .build .iter() .filter_map(|(name, dep)| { - if let &CrateDep::External(ref req) = dep { + if let CrateDep::External(ref req) = dep { Some((name.clone(), AnalyzedDependency::new(req.clone()))) } else { None diff --git a/src/models/repo.rs b/src/models/repo.rs index 9e980d5..c3d5d9a 100644 --- a/src/models/repo.rs +++ b/src/models/repo.rs @@ -50,9 +50,9 @@ pub enum RepoSite { impl RepoSite { pub fn to_base_uri(&self) -> &'static str { match self { - &RepoSite::Github => "https://github.com", - &RepoSite::Gitlab => "https://gitlab.com", - &RepoSite::Bitbucket => "https://bitbucket.org", + RepoSite::Github => "https://github.com", + RepoSite::Gitlab => "https://gitlab.com", + RepoSite::Bitbucket => "https://bitbucket.org", } } @@ -88,9 +88,9 @@ impl FromStr for RepoSite { impl AsRef for RepoSite { fn as_ref(&self) -> &str { match self { - &RepoSite::Github => "github", - &RepoSite::Gitlab => "gitlab", - &RepoSite::Bitbucket => "bitbucket", + RepoSite::Github => "github", + RepoSite::Gitlab => "gitlab", + RepoSite::Bitbucket => "bitbucket", } } } diff --git a/src/parsers/manifest.rs b/src/parsers/manifest.rs index 358e155..fc8e63e 100644 --- a/src/parsers/manifest.rs +++ b/src/parsers/manifest.rs @@ -51,35 +51,30 @@ fn convert_dependency( cargo_dep: (String, CargoTomlDependency), ) -> Option> { match cargo_dep { - (name, CargoTomlDependency::Simple(string)) => Some( - name.parse::() - .map_err(|err| err.into()) - .and_then(|parsed_name| { - string - .parse::() - .map_err(|err| err.into()) - .map(|version| (parsed_name, CrateDep::External(version))) - }), - ), + (name, CargoTomlDependency::Simple(string)) => { + Some(name.parse::().and_then(|parsed_name| { + string + .parse::() + .map_err(|err| err.into()) + .map(|version| (parsed_name, CrateDep::External(version))) + })) + } (name, CargoTomlDependency::Complex(cplx)) => { if cplx.git.is_some() { None } else if cplx.path.is_some() { cplx.path.map(|path| { name.parse::() - .map_err(|err| err.into()) .map(|parsed_name| (parsed_name, CrateDep::Internal(path))) }) } else { cplx.version.map(|string| { - name.parse::() - .map_err(|err| err.into()) - .and_then(|parsed_name| { - string - .parse::() - .map_err(|err| err.into()) - .map(|version| (parsed_name, CrateDep::External(version))) - }) + name.parse::().and_then(|parsed_name| { + string + .parse::() + .map_err(|err| err.into()) + .map(|version| (parsed_name, CrateDep::External(version))) + }) }) } } diff --git a/src/server/mod.rs b/src/server/mod.rs index c104f55..5d719d7 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -86,36 +86,33 @@ impl App { .new(o!("http_path" => req.uri().path().to_owned())); if let Ok(route_match) = self.router.recognize(req.uri().path()) { - match route_match.handler { - &Route::Index => { - if *req.method() == Method::GET { - return self.index(req, route_match.params, logger).await; - } + match (req.method(), route_match.handler) { + (&Method::GET, Route::Index) => { + return self.index(req, route_match.params, logger).await; } - &Route::RepoStatus(format) => { - if *req.method() == Method::GET { - return self - .repo_status(req, route_match.params, logger, format) - .await; - } + + (&Method::GET, Route::RepoStatus(format)) => { + return self + .repo_status(req, route_match.params, logger, *format) + .await; } - &Route::CrateStatus(format) => { - if *req.method() == Method::GET { - return self - .crate_status(req, route_match.params, logger, format) - .await; - } + + (&Method::GET, Route::CrateStatus(format)) => { + return self + .crate_status(req, route_match.params, logger, *format) + .await; } - &Route::CrateRedirect => { - if *req.method() == Method::GET { - return self.crate_redirect(req, route_match.params, logger).await; - } + + (&Method::GET, Route::CrateRedirect) => { + return self.crate_redirect(req, route_match.params, logger).await; } - &Route::Static(file) => { - if *req.method() == Method::GET { - return Ok(App::static_file(file)); - } + + (&Method::GET, Route::Static(file)) => { + return Ok(App::static_file(*file)); } + + // fall through to 404 + _ => {} } } diff --git a/src/server/views/html/index.rs b/src/server/views/html/index.rs index 10effc3..43211e8 100644 --- a/src/server/views/html/index.rs +++ b/src/server/views/html/index.rs @@ -48,7 +48,7 @@ fn popular_table(popular_repos: Vec, popular_crates: Vec) tr { td { a href=(format!("{}/crate/{}/{}", &super::SELF_BASE_URL as &str, crate_path.name.as_ref(), crate_path.version)) { - (format!("{}", crate_path.name.as_ref())) + (crate_path.name.as_ref().to_string()) } } td class="has-text-right" { diff --git a/src/server/views/html/mod.rs b/src/server/views/html/mod.rs index b4407c2..b1afec1 100644 --- a/src/server/views/html/mod.rs +++ b/src/server/views/html/mod.rs @@ -48,8 +48,7 @@ fn render_navbar() -> Markup { } fn render_footer(duration: Option) -> Markup { - let duration_millis = - duration.map(|d| d.as_secs() * 1000 + (d.subsec_nanos() / 1000 / 1000) as u64); + let duration_millis = duration.map(|d| d.as_secs() * 1000 + (d.subsec_millis()) as u64); html! { footer class="footer" { diff --git a/src/utils/cache.rs b/src/utils/cache.rs index bcad41a..87d0b07 100644 --- a/src/utils/cache.rs +++ b/src/utils/cache.rs @@ -43,7 +43,7 @@ where pub fn new(service: S, duration: Duration, capacity: usize) -> Cache { Cache { inner: service, - duration: duration, + duration, cache: Mutex::new(LruCache::new(capacity)), } } @@ -77,7 +77,7 @@ where // } // } - self.inner.call(req.clone()) + self.inner.call(req) // .and_then(|response| { // // cache.insert(req, (now + self.duration, response.clone())); // ok(response)