From 1691980cbbd942bead39ae6373aed2fbed8bb960 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Fri, 15 Oct 2021 22:17:23 +0100 Subject: [PATCH] clippy --- chartered-db/src/crates.rs | 6 +++--- chartered-db/src/lib.rs | 3 ++- chartered-db/src/users.rs | 3 ++- chartered-db/src/uuid.rs | 2 +- chartered-fs/src/lib.rs | 10 +++++----- chartered-git/src/generators.rs | 6 +++--- chartered-git/src/main.rs | 20 +++++++++++--------- chartered-git/src/tree.rs | 8 +++++--- chartered-types/src/cargo.rs | 10 +++++++++- chartered-types/src/lib.rs | 3 +++ chartered-web/src/main.rs | 1 + chartered-git/src/command_handlers/fetch.rs | 2 +- chartered-git/src/command_handlers/ls_refs.rs | 2 +- chartered-git/src/command_handlers/mod.rs | 2 ++ chartered-git/src/git/packfile/high_level.rs | 32 +++++++++++++++++++++----------- chartered-web/src/endpoints/cargo_api/publish.rs | 5 +++-- chartered-web/src/endpoints/web_api/auth/openid.rs | 11 +++++++---- chartered-web/src/endpoints/web_api/crates/search.rs | 7 +++---- 18 files changed, 78 insertions(+), 55 deletions(-) diff --git a/chartered-db/src/crates.rs b/chartered-db/src/crates.rs index 76767f0..61179c7 100644 --- a/chartered-db/src/crates.rs +++ a/chartered-db/src/crates.rs @@ -349,7 +349,7 @@ Ok(CrateVersion::belonging_to(&self.crate_) .order_by(crate_versions::id.desc()) .limit(1) - .get_result::(&conn) + .get_result::>(&conn) .optional()?) }) .await? @@ -367,7 +367,7 @@ Ok(CrateVersion::belonging_to(&self.crate_) .filter(version.eq(crate_version)) - .get_result::(&conn) + .get_result::>(&conn) .optional()?) }) .await? @@ -382,7 +382,7 @@ Ok(CrateVersion::belonging_to(&self.crate_) .inner_join(users::table) - .load::<(CrateVersion, User)>(&conn)?) + .load::<(CrateVersion<'_>, User)>(&conn)?) }) .await? } diff --git a/chartered-db/src/lib.rs b/chartered-db/src/lib.rs index b3f9549..b506c72 100644 --- a/chartered-db/src/lib.rs +++ a/chartered-db/src/lib.rs @@ -1,4 +1,5 @@ #![deny(clippy::pedantic)] +#![deny(rust_2018_idioms)] #![allow(clippy::missing_errors_doc)] #![allow(clippy::module_name_repetitions)] #![allow(clippy::doc_markdown)] // `sql_function` fails this check @@ -21,7 +22,7 @@ { fn to_sql( &self, - out: &mut diesel::serialize::Output, + out: &mut diesel::serialize::Output<'_, W, B>, ) -> diesel::serialize::Result { serde_json::to_writer(out, self) .map(|_| diesel::serialize::IsNull::No) diff --git a/chartered-db/src/users.rs b/chartered-db/src/users.rs index 945b5b5..75236c4 100644 --- a/chartered-db/src/users.rs +++ a/chartered-db/src/users.rs @@ -276,10 +276,11 @@ .permissions) } + #[must_use] pub fn display_name(&self) -> &str { self.nick .as_ref() - .or(self.name.as_ref()) + .or_else(|| self.name.as_ref()) .unwrap_or(&self.username) } } diff --git a/chartered-db/src/uuid.rs b/chartered-db/src/uuid.rs index 19d8c2e..1cd01a6 100644 --- a/chartered-db/src/uuid.rs +++ a/chartered-db/src/uuid.rs @@ -45,7 +45,7 @@ { fn to_sql( &self, - out: &mut diesel::serialize::Output, + out: &mut diesel::serialize::Output<'_, W, B>, ) -> diesel::serialize::Result { out.write_all(self.0.as_bytes()) .map(|_| diesel::serialize::IsNull::No) diff --git a/chartered-fs/src/lib.rs b/chartered-fs/src/lib.rs index cc6d99a..77f6c4c 100644 --- a/chartered-fs/src/lib.rs +++ a/chartered-fs/src/lib.rs @@ -1,4 +1,5 @@ #![deny(clippy::pedantic)] +#![deny(rust_2018_idioms)] use std::path::PathBuf; @@ -35,7 +36,7 @@ Self::S3 { host: uri.host().unwrap().to_string(), bucket: path.next().unwrap().to_string(), - path: path.intersperse("/").collect(), + path: Itertools::intersperse(path, "/").collect(), } } "file" => { @@ -53,7 +54,7 @@ } impl std::fmt::Display for FileSystemKind { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Local => f.write_str("local"), Self::S3 => f.write_str("s3"), @@ -82,7 +83,7 @@ } impl std::fmt::Display for FileReference { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}:{}", self.file_system, self.reference) } } @@ -145,8 +146,7 @@ #[cfg(test)] mod tests { - use super::{FileSystem, FS}; - use std::str::FromStr; + use super::FileSystem; #[tokio::test] #[allow(clippy::pedantic)] diff --git a/chartered-git/src/generators.rs b/chartered-git/src/generators.rs index 32bc87c..34b9007 100644 --- a/chartered-git/src/generators.rs +++ a/chartered-git/src/generators.rs @@ -7,7 +7,7 @@ } impl CargoConfig { - pub fn new(base: url::Url, api_key: &str, organisation: &str) -> Self { + pub fn new(base: &url::Url, api_key: &str, organisation: &str) -> Self { let base = format!("{}a/{}/o/{}", base, api_key, organisation); Self { @@ -29,11 +29,11 @@ "my-organisation", ); assert_eq!( - conf.dl.to_string(), + conf.dl, "https://127.0.0.1:1234/a/my-api-key/o/my-organisation/api/v1/crates" ); assert_eq!( - conf.api.to_string(), + conf.api, "https://127.0.0.1:1234/a/my-api-key/o/my-organisation" ); } diff --git a/chartered-git/src/main.rs b/chartered-git/src/main.rs index 62afdf1..a6a1607 100644 --- a/chartered-git/src/main.rs +++ a/chartered-git/src/main.rs @@ -1,4 +1,5 @@ #![deny(clippy::pedantic)] +#![deny(rust_2018_idioms)] mod command_handlers; mod generators; mod tree; @@ -26,7 +27,7 @@ }; use thrussh_keys::{key, PublicKeyBase64}; use tokio_util::codec::{Decoder, Encoder as TokioEncoder}; -use tracing::{debug, error, event, info, warn, Instrument}; +use tracing::{debug, error, info, warn, Instrument}; use url::Url; #[tokio::main] @@ -151,6 +152,7 @@ ) -> Self::FutureUnit { self.span.in_scope(|| debug!("env set {}={}", name, value)); + #[allow(clippy::single_match)] match (name, value) { ("GIT_PROTOCOL", "version=2") => self.is_git_protocol_v2 = true, _ => {} @@ -274,7 +276,7 @@ self, _user: &str, _submethods: &str, - _response: Option, + _response: Option>, ) -> Self::FutureAuth { self.finished_auth(server::Auth::UnsupportedMethod) } @@ -313,20 +315,20 @@ let mut packfile = GitRepository::default(); let config = CargoConfig::new( - Url::parse("http://127.0.0.1:8888/")?, + &Url::parse("http://127.0.0.1:8888/")?, &authed.auth_key, org_name, ); let config = serde_json::to_vec(&config)?; - packfile.insert(ArrayVec::<_, 0>::new(), "config.json", &config); + packfile.insert(ArrayVec::<_, 0>::new(), "config.json", &config)?; // todo: the whole tree needs caching and then we can filter in code rather than at // the database let tree = Tree::build(self.db.clone(), authed.user.id, org_name.to_string()).await; - tree.write_to_packfile(&mut packfile); + tree.write_to_packfile(&mut packfile)?; let (commit_hash, packfile_entries) = - packfile.commit("computer", "john@computer.no", "Update crates"); + packfile.commit("computer", "john@computer.no", "Update crates")?; match frame.command.as_ref() { b"command=ls-refs" => { @@ -336,8 +338,7 @@ channel, frame.metadata, &commit_hash, - ) - .await? + )?; } b"command=fetch" => { command_handlers::fetch::handle( @@ -346,8 +347,7 @@ channel, frame.metadata, packfile_entries, - ) - .await? + )?; } v => { error!( diff --git a/chartered-git/src/tree.rs b/chartered-git/src/tree.rs index b6e4975..f5f4a14 100644 --- a/chartered-git/src/tree.rs +++ a/chartered-git/src/tree.rs @@ -49,11 +49,13 @@ Self { crates } } - pub fn write_to_packfile<'a>(&'a self, repo: &mut GitRepository<'a>) { + pub fn write_to_packfile<'a>(&'a self, repo: &mut GitRepository<'a>) -> Result<(), anyhow::Error> { for (name, content) in &self.crates { - let crate_folder = get_crate_folder(&name); - repo.insert(crate_folder, &name, content.as_bytes()); + let crate_folder = get_crate_folder(name); + repo.insert(crate_folder, name, content.as_bytes())?; } + + Ok(()) } } diff --git a/chartered-types/src/cargo.rs b/chartered-types/src/cargo.rs index 23c9d61..9150371 100644 --- a/chartered-types/src/cargo.rs +++ a/chartered-types/src/cargo.rs @@ -1,3 +1,5 @@ +#![deny(clippy::pedantic)] + //! 'Raw' types that are passed by `cargo publish` and also consumed via //! cargo when pulling. These are just inserted into the database as-is. @@ -17,11 +19,16 @@ } impl CrateVersion<'_> { + #[must_use] pub fn into_owned(self) -> CrateVersion<'static> { CrateVersion { name: Cow::Owned(self.name.into_owned()), vers: Cow::Owned(self.vers.into_owned()), - deps: self.deps.into_iter().map(|v| v.into_owned()).collect(), + deps: self + .deps + .into_iter() + .map(CrateDependency::into_owned) + .collect(), features: self.features, links: self.links.map(|v| Cow::Owned(v.into_owned())), } @@ -53,6 +60,7 @@ } impl CrateDependency<'_> { + #[must_use] pub fn into_owned(self) -> CrateDependency<'static> { CrateDependency { name: Cow::Owned(self.name.into_owned()), diff --git a/chartered-types/src/lib.rs b/chartered-types/src/lib.rs index fbe9fcc..2ac6f1d 100644 --- a/chartered-types/src/lib.rs +++ a/chartered-types/src/lib.rs @@ -1,1 +1,4 @@ +#![deny(clippy::pedantic)] +#![deny(rust_2018_idioms)] + pub mod cargo; diff --git a/chartered-web/src/main.rs b/chartered-web/src/main.rs index 836095e..7480aa4 100644 --- a/chartered-web/src/main.rs +++ a/chartered-web/src/main.rs @@ -1,4 +1,5 @@ #![deny(clippy::pedantic)] +#![deny(rust_2018_idioms)] #![allow(clippy::module_name_repetitions)] mod config; diff --git a/chartered-git/src/command_handlers/fetch.rs b/chartered-git/src/command_handlers/fetch.rs index 3fd0668..aa5cfec 100644 --- a/chartered-git/src/command_handlers/fetch.rs +++ a/chartered-git/src/command_handlers/fetch.rs @@ -9,7 +9,7 @@ Handler, }; -pub(crate) async fn handle( +pub(crate) fn handle( handle: &mut Handler, session: &mut Session, channel: ChannelId, diff --git a/chartered-git/src/command_handlers/ls_refs.rs b/chartered-git/src/command_handlers/ls_refs.rs index 78da003..d4dee55 100644 --- a/chartered-git/src/command_handlers/ls_refs.rs +++ a/chartered-git/src/command_handlers/ls_refs.rs @@ -12,7 +12,7 @@ Handler, }; -pub(crate) async fn handle( +pub(crate) fn handle( handle: &mut Handler, session: &mut Session, channel: ChannelId, diff --git a/chartered-git/src/command_handlers/mod.rs b/chartered-git/src/command_handlers/mod.rs index 8406cdb..ac33b69 100644 --- a/chartered-git/src/command_handlers/mod.rs +++ a/chartered-git/src/command_handlers/mod.rs @@ -1,2 +1,4 @@ +#![allow(clippy::needless_pass_by_value)] + pub mod fetch; pub mod ls_refs; diff --git a/chartered-git/src/git/packfile/high_level.rs b/chartered-git/src/git/packfile/high_level.rs index 2e4deba..62dd4f6 100644 --- a/chartered-git/src/git/packfile/high_level.rs +++ a/chartered-git/src/git/packfile/high_level.rs @@ -9,32 +9,32 @@ pub struct Directory<'a>(IndexMap<&'a str, Box>>); impl<'a> Directory<'a> { - fn into_packfile_entries( + fn to_packfile_entries( &self, pack_file: &mut IndexMap>, - ) -> HashOutput { + ) -> Result { let mut tree = Vec::with_capacity(self.0.len()); for (name, item) in &self.0 { tree.push(match item.as_ref() { TreeItem::Blob(hash) => LowLevelTreeItem { kind: TreeItemKind::File, - name: &name, + name, hash: *hash, }, TreeItem::Directory(dir) => LowLevelTreeItem { kind: TreeItemKind::Directory, - name: &name, - hash: dir.into_packfile_entries(pack_file), + name, + hash: dir.to_packfile_entries(pack_file)?, }, - }) + }); } let tree = PackFileEntry::Tree(tree); - let hash = tree.hash().unwrap(); + let hash = tree.hash()?; pack_file.insert(hash, tree); - hash + Ok(hash) } } @@ -56,7 +56,7 @@ path: ArrayVec<&'a str, N>, file: &'a str, content: &'a [u8], - ) { + ) -> Result<(), anyhow::Error> { let mut directory = &mut self.tree; for part in path { @@ -68,18 +68,20 @@ if let TreeItem::Directory(d) = tree_item.as_mut() { directory = d; } else { - panic!("one of the path items was a blob"); + anyhow::bail!("one of the path items was a blob"); } } let entry = PackFileEntry::Blob(content); // todo: handle overwriting error - let file_hash = entry.hash().unwrap(); + let file_hash = entry.hash()?; directory .0 .insert(file, Box::new(TreeItem::Blob(file_hash))); self.file_entries.insert(file_hash, entry); + + Ok(()) } pub fn commit( @@ -87,8 +89,8 @@ name: &'static str, email: &'static str, message: &'static str, - ) -> (HashOutput, Vec>) { - let tree_hash = self.tree.into_packfile_entries(&mut self.file_entries); + ) -> Result<(HashOutput, Vec>), anyhow::Error> { + let tree_hash = self.tree.to_packfile_entries(&mut self.file_entries)?; let commit_user = CommitUserInfo { name, @@ -103,10 +105,10 @@ message, }); - let commit_hash = commit.hash().unwrap(); + let commit_hash = commit.hash()?; self.file_entries.insert(commit_hash, commit); // TODO: make PackFileEntry copy and remove this clone - (commit_hash, self.file_entries.values().cloned().collect()) + Ok((commit_hash, self.file_entries.values().cloned().collect())) } } diff --git a/chartered-web/src/endpoints/cargo_api/publish.rs b/chartered-web/src/endpoints/cargo_api/publish.rs index 07f2a81..c2b5e82 100644 --- a/chartered-web/src/endpoints/cargo_api/publish.rs +++ a/chartered-web/src/endpoints/cargo_api/publish.rs @@ -71,7 +71,7 @@ ) -> Result, Error> { let (_, (metadata_bytes, crate_bytes)) = parse(body.as_ref()).map_err(|_| Error::MetadataParse)?; - let metadata: Metadata = serde_json::from_slice(metadata_bytes)?; + let metadata: Metadata<'_> = serde_json::from_slice(metadata_bytes)?; if !validate_crate_name(&metadata.inner.name) { return Err(Error::InvalidCrateName); @@ -177,7 +177,7 @@ } } -/// We've redefined MetadataCrateDependency for deserialisation because `cargo publish` passes +/// We've redefined `MetadataCrateDependency` for deserialisation because `cargo publish` passes /// a `version_req`, whereas when downloading it expects `req` - and `package` isn't returned. #[derive(Deserialize, Debug)] pub struct MetadataCrateDependency<'a> { @@ -194,6 +194,7 @@ impl From> for CrateDependency<'static> { fn from(us: MetadataCrateDependency<'_>) -> CrateDependency<'static> { + #[allow(clippy::option_if_let_else)] // us.name can't be moved into both closures let (name, package) = if let Some(explicit_name_in_toml) = us.explicit_name_in_toml { ( explicit_name_in_toml.into_owned(), diff --git a/chartered-web/src/endpoints/web_api/auth/openid.rs b/chartered-web/src/endpoints/web_api/auth/openid.rs index 46abb30..beba150 100644 --- a/chartered-web/src/endpoints/web_api/auth/openid.rs +++ a/chartered-web/src/endpoints/web_api/auth/openid.rs @@ -17,6 +17,7 @@ providers: Vec, } +#[allow(clippy::unused_async)] pub async fn list_providers( extract::Extension(oidc_clients): extract::Extension>, ) -> Json { @@ -24,7 +25,7 @@ providers: oidc_clients .keys() .into_iter() - .map(|v| v.to_string()) + .map(std::string::ToString::to_string) .collect(), }) } @@ -40,6 +41,7 @@ redirect_url: String, } +#[allow(clippy::unused_async)] pub async fn begin_oidc( extract::Path(provider): extract::Path, extract::Extension(config): extract::Extension>, @@ -56,7 +58,7 @@ scope: Some("openid email profile".into()), nonce: Some(base64::encode_config(&nonce, base64::URL_SAFE_NO_PAD)), state: Some(encrypt_url_safe(&state, &config)?), - ..Default::default() + ..Options::default() }); Ok(Json(BeginResponse { @@ -64,6 +66,7 @@ })) } +#[allow(dead_code)] #[derive(Deserialize)] pub struct CompleteOidcParams { state: String, @@ -92,7 +95,7 @@ client.decode_token(&mut id_token)?; let nonce = base64::encode_config(state.nonce, base64::URL_SAFE_NO_PAD); - client.validate_token(&id_token, Some(nonce.as_str()), None)?; + client.validate_token(id_token, Some(nonce.as_str()), None)?; } else { return Err(Error::MissingToken); } @@ -122,7 +125,7 @@ let nonce = ChaCha20Poly1305Nonce::from_slice(&nonce); let mut ciphertext = cipher.encrypt(nonce, input)?; - ciphertext.extend_from_slice(&nonce); + ciphertext.extend_from_slice(nonce); Ok(base64::encode_config(&ciphertext, base64::URL_SAFE_NO_PAD)) } diff --git a/chartered-web/src/endpoints/web_api/crates/search.rs b/chartered-web/src/endpoints/web_api/crates/search.rs index ab27411..9abb3d1 100644 --- a/chartered-web/src/endpoints/web_api/crates/search.rs +++ a/chartered-web/src/endpoints/web_api/crates/search.rs @@ -34,7 +34,7 @@ Crate::search(db.clone(), user.id, req.q, 5) .await? .into_iter() - .map(move |(org, crates_with_permissions)| { + .flat_map(move |(org, crates_with_permissions)| { let db = db.clone(); crates_with_permissions @@ -54,12 +54,11 @@ version: version.map(|v| v.version).unwrap_or_default(), homepage: v.crate_.homepage.clone(), repository: v.crate_.repository.clone(), - permissions: v.permissions.clone(), + permissions: v.permissions, }) } }) - }) - .flatten(), + }), ) .await?; -- rgit 0.1.3