From 437e8b284811de35284b2b1d906982a1802f97a4 Mon Sep 17 00:00:00 2001 From: Miguel Mano Date: Mon, 7 Nov 2022 21:56:47 +0000 Subject: [PATCH] ci: apply clippy suggestions for rust 1.65.0 (#214) * ci: apply clippy suggestions for rust 1.65.0 * refactor: switch to let-else statements * ci: move from rust 1.59 to 1.65 * refactor: revert connection.rs --- .github/workflows/main.yml | 2 +- src/commands/queue.rs | 2 +- src/handlers/idle.rs | 45 ++++++++++++++++++---------------- src/handlers/serenity.rs | 28 ++++++++++++--------- src/sources/ffmpeg.rs | 2 +- src/sources/youtube.rs | 50 +++++++++++++++++++------------------- 6 files changed, 68 insertions(+), 61 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 79aa144d..1593ea8a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -7,7 +7,7 @@ jobs: name: Build strategy: matrix: - rust-version: ["1.59", "stable"] + rust-version: ["1.65", "stable"] runs-on: ubuntu-latest steps: - name: Repository Checkout diff --git a/src/commands/queue.rs b/src/commands/queue.rs index a7f78a73..bfde9cc7 100644 --- a/src/commands/queue.rs +++ b/src/commands/queue.rs @@ -87,7 +87,7 @@ pub async fn queue( drop(handler); let mut cib = message - .await_component_interactions(&ctx) + .await_component_interactions(ctx) .timeout(Duration::from_secs(EMBED_TIMEOUT)) .build(); diff --git a/src/handlers/idle.rs b/src/handlers/idle.rs index cdfaf433..2e51a9fe 100644 --- a/src/handlers/idle.rs +++ b/src/handlers/idle.rs @@ -21,31 +21,34 @@ pub struct IdleHandler { #[async_trait] impl EventHandler for IdleHandler { async fn act(&self, ctx: &EventContext<'_>) -> Option { - if let EventContext::Track(track_list) = ctx { - // looks like the track list isn't ordered here, so the first track in the list isn't - // guaranteed to be the first track in the actual queue, so search the entire list - let bot_is_playing = track_list - .iter() - .any(|track| matches!(track.0.playing, PlayMode::Play)); - - // if there's a track playing, then reset the counter - if bot_is_playing { - self.count.store(0, Ordering::Relaxed); - return None; - } + let EventContext::Track(track_list) = ctx else { + return None; + }; + + // looks like the track list isn't ordered here, so the first track in the list isn't + // guaranteed to be the first track in the actual queue, so search the entire list + let bot_is_playing = track_list + .iter() + .any(|track| matches!(track.0.playing, PlayMode::Play)); - if self.count.fetch_add(1, Ordering::Relaxed) >= self.limit { - let guild_id = self.interaction.guild_id?; + // if there's a track playing, then reset the counter + if bot_is_playing { + self.count.store(0, Ordering::Relaxed); + return None; + } - if self.manager.remove(guild_id).await.is_ok() { - self.interaction - .channel_id - .say(&self.http, IDLE_ALERT) - .await - .unwrap(); - } + if self.count.fetch_add(1, Ordering::Relaxed) >= self.limit { + let guild_id = self.interaction.guild_id?; + + if self.manager.remove(guild_id).await.is_ok() { + self.interaction + .channel_id + .say(&self.http, IDLE_ALERT) + .await + .unwrap(); } } + None } } diff --git a/src/handlers/serenity.rs b/src/handlers/serenity.rs index c724448e..51ef9496 100644 --- a/src/handlers/serenity.rs +++ b/src/handlers/serenity.rs @@ -44,10 +44,12 @@ impl EventHandler for SerenityHandler { } async fn interaction_create(&self, ctx: Context, interaction: Interaction) { - if let Interaction::ApplicationCommand(mut command) = interaction { - if let Err(err) = self.run_command(&ctx, &mut command).await { - self.handle_error(&ctx, &mut command, err).await - } + let Interaction::ApplicationCommand(mut command) = interaction else { + return; + }; + + if let Err(err) = self.run_command(&ctx, &mut command).await { + self.handle_error(&ctx, &mut command, err).await } } @@ -351,14 +353,16 @@ impl SerenityHandler { } async fn self_deafen(&self, ctx: &Context, guild: Option, new: VoiceState) { - if let Ok(user) = ctx.http.get_current_user().await { - if user.id == new.user_id && !new.deaf { - guild - .unwrap() - .edit_member(&ctx.http, new.user_id, |n| n.deafen(true)) - .await - .unwrap(); - } + let Ok(user) = ctx.http.get_current_user().await else { + return; + }; + + if user.id == new.user_id && !new.deaf { + guild + .unwrap() + .edit_member(&ctx.http, new.user_id, |n| n.deafen(true)) + .await + .unwrap(); } } diff --git a/src/sources/ffmpeg.rs b/src/sources/ffmpeg.rs index 7a882a81..654d9247 100644 --- a/src/sources/ffmpeg.rs +++ b/src/sources/ffmpeg.rs @@ -23,7 +23,7 @@ pub async fn ffmpeg(mut source: Child, metadata: Metadata, pre_args: &[&str]) -> let ffmpeg = Command::new("ffmpeg") .args(pre_args) - .args(&ffmpeg_args) + .args(ffmpeg_args) .stdin(taken_stdout) .stderr(Stdio::null()) .stdout(Stdio::piped()) diff --git a/src/sources/youtube.rs b/src/sources/youtube.rs index 507277f0..5cabf0c8 100644 --- a/src/sources/youtube.rs +++ b/src/sources/youtube.rs @@ -63,23 +63,23 @@ impl YouTubeRestartable { .spawn() .unwrap(); - if let Some(stdout) = &mut child.stdout { - let reader = BufReader::new(stdout); - - let lines = reader.lines().flatten().map(|line| { - let entry: Value = serde_json::from_str(&line).unwrap(); - entry - .get("webpage_url") - .unwrap() - .as_str() - .unwrap() - .to_string() - }); - - Some(lines.collect()) - } else { - None - } + let Some(stdout) = &mut child.stdout else { + return None; + }; + + let reader = BufReader::new(stdout); + + let lines = reader.lines().flatten().map(|line| { + let entry: Value = serde_json::from_str(&line).unwrap(); + entry + .get("webpage_url") + .unwrap() + .as_str() + .unwrap() + .to_string() + }); + + Some(lines.collect()) } } @@ -98,12 +98,12 @@ where async fn call_restart(&mut self, time: Option) -> SongbirdResult { let (yt, metadata) = ytdl(self.uri.as_ref()).await?; - if let Some(time) = time { - let ts = format!("{:.3}", time.as_secs_f64()); - ffmpeg(yt, metadata, &["-ss", &ts]).await - } else { - ffmpeg(yt, metadata, &[]).await - } + let Some(time) = time else { + return ffmpeg(yt, metadata, &[]).await; + }; + + let ts = format!("{:.3}", time.as_secs_f64()); + ffmpeg(yt, metadata, &["-ss", &ts]).await } async fn lazy_init(&mut self) -> SongbirdResult<(Option, Codec, Container)> { @@ -130,7 +130,7 @@ async fn ytdl(uri: &str) -> Result<(Child, Metadata), SongbirdError> { ]; let mut yt = Command::new("yt-dlp") - .args(&ytdl_args) + .args(ytdl_args) .stdin(Stdio::null()) .stderr(Stdio::piped()) .stdout(Stdio::piped()) @@ -179,7 +179,7 @@ async fn _ytdl_metadata(uri: &str) -> SongbirdResult { ]; let youtube_dl_output = TokioCommand::new("yt-dlp") - .args(&ytdl_args) + .args(ytdl_args) .stdin(Stdio::null()) .output() .await?;