From bfd146de95fa9c7e27a9d7eed2a86018d13cef45 Mon Sep 17 00:00:00 2001 From: "Brian J. Tarricone" Date: Tue, 19 Sep 2023 21:35:16 -0700 Subject: [PATCH] Fix clippy warnings --- src/event.rs | 30 +++++++++++++++--------------- src/main.rs | 23 +++++++++-------------- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/event.rs b/src/event.rs index 377a286..4f1a140 100644 --- a/src/event.rs +++ b/src/event.rs @@ -19,7 +19,7 @@ use core::fmt; use crate::config::PublishEvent; pub trait GitlabEventExt { - fn should_publish(&self, publish_events: &Vec) -> bool; + fn should_publish(&self, publish_events: &[PublishEvent]) -> bool; fn project(&self) -> &Project; fn r#ref(&self) -> Option<&str>; fn user(&self) -> &str; @@ -264,7 +264,7 @@ macro_rules! find_publish_event { } impl GitlabEventExt for GitlabEvent { - fn should_publish(&self, publish_events: &Vec) -> bool { + fn should_publish(&self, publish_events: &[PublishEvent]) -> bool { match self { GitlabEvent::Push { r#ref, .. } => { if let Some(PublishEvent::Push { branches }) = @@ -274,7 +274,7 @@ impl GitlabEventExt for GitlabEvent { None => true, Some(branches) => { let refname = parse_ref(r#ref); - branches.iter().find(|branch| branch.find(&refname).is_some()).is_some() + branches.iter().any(|branch| branch.find(&refname).is_some()) } } } else { @@ -324,19 +324,19 @@ impl GitlabEventExt for GitlabEvent { fn project(&self) -> &Project { match self { - GitlabEvent::Push { project, .. } => &project, - GitlabEvent::TagPush { project, .. } => &project, + GitlabEvent::Push { project, .. } => project, + GitlabEvent::TagPush { project, .. } => project, GitlabEvent::Issue { project, .. } => project, - GitlabEvent::MergeRequest { project, .. } => &project, - GitlabEvent::Pipeline { project, .. } => &project, + GitlabEvent::MergeRequest { project, .. } => project, + GitlabEvent::Pipeline { project, .. } => project, GitlabEvent::Other => unreachable!("Unsupported event type"), } } fn r#ref(&self) -> Option<&str> { match self { - GitlabEvent::Push { r#ref, .. } => Some(&r#ref), - GitlabEvent::TagPush { r#ref, .. } => Some(&r#ref), + GitlabEvent::Push { r#ref, .. } => Some(r#ref), + GitlabEvent::TagPush { r#ref, .. } => Some(r#ref), GitlabEvent::Issue { .. } => None, GitlabEvent::MergeRequest { object_attributes, .. } => Some(&object_attributes.target_branch), GitlabEvent::Pipeline { object_attributes, .. } => Some(&object_attributes.r#ref), @@ -346,8 +346,8 @@ impl GitlabEventExt for GitlabEvent { fn user(&self) -> &str { match self { - GitlabEvent::Push { user_name, .. } => &user_name, - GitlabEvent::TagPush { user_name, .. } => &user_name, + GitlabEvent::Push { user_name, .. } => user_name, + GitlabEvent::TagPush { user_name, .. } => user_name, GitlabEvent::Issue { user, .. } => &user.name, GitlabEvent::MergeRequest { user, .. } => &user.name, GitlabEvent::Pipeline { user, .. } => &user.name, @@ -356,7 +356,7 @@ impl GitlabEventExt for GitlabEvent { } fn titles(&self) -> Vec { - fn find_commit<'a>(commits: &'a Vec, sha: &str) -> Option<&'a Commit> { + fn find_commit<'a>(commits: &'a [Commit], sha: &str) -> Option<&'a Commit> { commits.iter().find(|commit| commit.id == sha) } @@ -395,7 +395,7 @@ impl GitlabEventExt for GitlabEvent { } => { let title = format!( "**tagged** {}", - find_commit(commits, &checkout_sha) + find_commit(commits, checkout_sha) .map(|commit| &commit.title) .unwrap_or(checkout_sha) ); @@ -433,7 +433,7 @@ impl GitlabEventExt for GitlabEvent { let title = object_attributes .name .as_ref() - .map(|n| n.clone()) + .cloned() .or(merge_request.as_ref().map(|mr| mr.title.clone())) .iter() .fold(format!("Pipeline **{}**", object_attributes.status), |accum, title| { @@ -474,7 +474,7 @@ mod test { use std::{fs::File, io::BufReader}; fn load_test_data(name: &str) -> anyhow::Result { - let f = File::open(&format!("{}/test-data/{}.json", env!("CARGO_MANIFEST_DIR"), name))?; + let f = File::open(format!("{}/test-data/{}.json", env!("CARGO_MANIFEST_DIR"), name))?; let r = BufReader::new(f); let event: GitlabEvent = serde_json::from_reader(r)?; Ok(event) diff --git a/src/main.rs b/src/main.rs index a3e19ae..aada25f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -83,37 +83,32 @@ async fn ensure_matrix_room_joined(matrix_client: &Client, room_id: &OwnedRoomOr || a_room .canonical_alias() .iter() - .find(|alias| alias.as_str() == our_room_str) - .is_some() - || a_room - .alt_aliases() - .iter() - .find(|alias| alias.as_str() == our_room_str) - .is_some() + .any(|alias| alias.as_str() == our_room_str) + || a_room.alt_aliases().iter().any(|alias| alias.as_str() == our_room_str) } let mut room = matrix_client .joined_rooms() .iter() - .find(|a_room| room_matches(*a_room, room_id)) - .map(|room| room.clone()); + .find(|a_room| room_matches(a_room, room_id)) + .cloned(); if room.is_none() { if let Some(invited) = matrix_client .invited_rooms() .iter() - .find(|a_room| room_matches(*a_room, room_id)) + .find(|a_room| room_matches(a_room, room_id)) { invited.accept_invitation().await?; } else { matrix_client.join_room_by_id_or_alias(room_id, &[]).await?; } - let settings = build_sync_settings(&matrix_client).await; + let settings = build_sync_settings(matrix_client).await; matrix_client.sync_once(settings).await?; room = matrix_client .joined_rooms() .iter() - .find(|a_room| room_matches(*a_room, room_id)) - .map(|room| room.clone()); + .find(|a_room| room_matches(a_room, room_id)) + .cloned(); } room.ok_or_else(|| anyhow!("Unable to join room {}", room_id)) @@ -162,7 +157,7 @@ async fn run() -> anyhow::Result<()> { let addr = config .bind_address .as_ref() - .map(|ba| ba.clone()) + .cloned() .unwrap_or_else(|| "127.0.0.1".to_string()) .parse::() .context("Failed to parse bind_address")?;