From 13fcc8a263970e48999e7f9319876a6443a54ac5 Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Wed, 17 Dec 2025 19:19:59 -0500 Subject: clippy fixes --- Cargo.toml | 2 +- src/bot.rs | 46 +++++++---------- src/commands/meme/create.rs | 12 ++--- src/commands/meme/history.rs | 20 ++++---- src/commands/meme/invoke.rs | 14 +++--- src/commands/meme/mod.rs | 16 +++--- src/commands/mod.rs | 14 ++---- src/db/models.rs | 6 +-- src/lib.rs | 1 - src/util/mod.rs | 4 +- src/util/windows.rs | 115 ++++++++++++++++++++++++------------------- 11 files changed, 124 insertions(+), 126 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ebc4348..d4f83de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ members = [ [workspace.package] authors = ["Nathan Perry "] -edition = "2021" +edition = "2024" [package] name = "thulani" diff --git a/src/bot.rs b/src/bot.rs index ac283db..99acd42 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -109,7 +109,7 @@ async fn perm_check(ctx: &Context, guild: &Guild) -> anyhow::Result<()> { let me = ctx.cache.current_user().id; let member = guild.member(&ctx, me).await?; - let perms = member.permissions(&ctx)?; + let perms = member.permissions(ctx)?; let lacking_perms = util::REQUIRED_PERMS.difference(perms); @@ -331,19 +331,17 @@ fn on_err(err: FrameworkError) -> BoxFuture<()> { "BANIC".to_string() }, } + } else if let Err(e) = commands::meme::invoke::_meme( + PoiseContext::Prefix(ctx), + msg_content, + Default::default(), + ) + .await + { + tracing::error!(error = %e, "producing meme for unrecognized"); + "BANIC".to_string() } else { - if let Err(e) = commands::meme::invoke::_meme( - PoiseContext::Prefix(ctx), - msg_content, - Default::default(), - ) - .await - { - tracing::error!(error = %e, "producing meme for unrecognized"); - "BANIC".to_string() - } else { - return; - } + return; } }, _ => "BANIC".to_string(), @@ -365,7 +363,7 @@ async fn framework() -> poise::Framework { let additional_prefixes = ALL_PREFIXES.iter().skip(1).map(|x| poise::Prefix::Literal(x.to_owned())).collect(); - let framework = poise::Framework::builder() + poise::Framework::builder() .options(poise::FrameworkOptions { pre_command: before_handle, post_command: after_handle, @@ -374,7 +372,7 @@ async fn framework() -> poise::Framework { command_check: Some(check), prefix_options: poise::PrefixFrameworkOptions { - prefix: ALL_PREFIXES.get(0).map(|&x| x.to_owned()), + prefix: ALL_PREFIXES.first().map(|&x| x.to_owned()), additional_prefixes, case_insensitive_commands: true, mention_as_prefix: false, @@ -390,9 +388,7 @@ async fn framework() -> poise::Framework { ..Default::default() }) .setup(|_ctx, _ready, _framework| Box::pin(async move { Ok(()) })) - .build(); - - framework + .build() } fn check(ctx: PoiseContext) -> BoxFuture> { @@ -466,9 +462,7 @@ fn after_handle(ctx: PoiseContext) -> BoxFuture<()> { pub async fn run() -> anyhow::Result<()> { #[cfg(all(windows, feature = "windows_autostart_postgres"))] - let started_pg = - tokio::task::spawn_blocking(|| unsafe { util::windows::ensure_postgres_started() }) - .await??; + let started_pg = tokio::task::spawn_blocking(util::windows::ensure_postgres_started).await??; let token = &CONFIG.discord.auth.token; @@ -506,7 +500,7 @@ pub async fn run() -> anyhow::Result<()> { let Some(songbird) = ({ let data = client_data.read().await; - data.get::().map(|x| x.clone()) + data.get::().cloned() }) else { tracing::warn!("gc songbird data: no songbird in state"); continue; @@ -559,12 +553,10 @@ pub async fn run() -> anyhow::Result<()> { tracing::info!("discord shutdown"); #[cfg(all(windows, feature = "windows_autostart_postgres"))] - unsafe { - if started_pg { - tracing::info!("we started postgres, stopping it before shutdown"); + if started_pg { + tracing::info!("we started postgres, stopping it before shutdown"); - tokio::task::spawn_blocking(|| util::windows::shutdown_postgres()).await??; - } + tokio::task::spawn_blocking(util::windows::shutdown_postgres).await??; } Ok(()) diff --git a/src/commands/meme/create.rs b/src/commands/meme/create.rs index 2d0ee9a..80fbe44 100644 --- a/src/commands/meme/create.rs +++ b/src/commands/meme/create.rs @@ -12,16 +12,16 @@ use tokio::{ use url::Url; use crate::{ + FFMPEG_COMMAND, + PoiseContext, db::{ - connection, Audio, Image, NewMeme, + connection, }, parse_times, util, - PoiseContext, - FFMPEG_COMMAND, }; /// Add a text/image meme to the db. @@ -79,7 +79,7 @@ pub async fn addmeme( return Ok(()); } - return Err(e.into()); + return Err(e); }, } @@ -100,7 +100,7 @@ pub async fn addaudiomeme( if elems.is_empty() { util::reply(ctx, "are you stupid").await?; - return Err(anyhow!("no audio link was provided").into()); + return Err(anyhow!("no audio link was provided")); } let audio_link = Url::parse(elems[0])?; @@ -206,7 +206,7 @@ pub async fn addaudiomeme( return Ok(()); } - return Err(e.into()); + return Err(e); }, } diff --git a/src/commands/meme/history.rs b/src/commands/meme/history.rs index 236e47f..5a46758 100644 --- a/src/commands/meme/history.rs +++ b/src/commands/meme/history.rs @@ -4,8 +4,8 @@ use chrono::{ Utc, }; use diesel::{ - result::Error as DieselError, NotFound, + result::Error as DieselError, }; use grate::tracing; use itertools::Itertools; @@ -33,17 +33,17 @@ use timeago::{ }; use crate::{ + PoiseContext, commands::game::get_user_id, config::CONFIG, db::{ self, - connection, InvocationRecord, Meme, Metadata, + connection, }, util, - PoiseContext, }; lazy_static! { @@ -76,7 +76,7 @@ pub async fn wat(ctx: PoiseContext<'_>) -> anyhow::Result<()> { } util::reply(ctx, "BAD MEME BAD MEME").await?; - return Err(e.into()); + return Err(e); }, }; @@ -107,7 +107,7 @@ pub async fn wat(ctx: PoiseContext<'_>) -> anyhow::Result<()> { } util::reply(ctx, "do i look like i know what a jpeg is").await?; - return Err(e.into()); + return Err(e); }, }; @@ -151,7 +151,7 @@ pub async fn history(ctx: PoiseContext<'_>, n: Option) -> anyhow::Result< .then(|(i, rec)| async move { let mut conn = connection().await?; - let dt = chrono::Utc.from_utc_datetime(&rec.time).conv::(); + let dt = Utc.from_utc_datetime(&rec.time).conv::(); let ago = FormattedTimestamp::new(dt, Some(FormattedTimestampStyle::RelativeTime)); let rand = if rec.random { @@ -186,10 +186,10 @@ pub async fn history(ctx: PoiseContext<'_>, n: Option) -> anyhow::Result< ) }, Err(e) => { - if let Some(variant) = e.downcast_ref::() { - if *variant != NotFound { - tracing::error!(error = %e, "error encountered loading meme history"); - } + if let Some(variant) = e.downcast_ref::() + && *variant != NotFound + { + tracing::error!(error = %e, "error encountered loading meme history"); } format!( diff --git a/src/commands/meme/invoke.rs b/src/commands/meme/invoke.rs index d7c6b3f..f057db5 100644 --- a/src/commands/meme/invoke.rs +++ b/src/commands/meme/invoke.rs @@ -1,20 +1,20 @@ use diesel::{ - result::Error as DieselError, NotFound, + result::Error as DieselError, }; use grate::tracing; use crate::{ + PoiseContext, + RestVec, commands::meme::send_meme, db::{ self, + InvocationRecord, connection, find_meme, - InvocationRecord, }, util, - PoiseContext, - RestVec, }; /// Post a meme. @@ -104,7 +104,7 @@ pub(crate) async fn _meme( Ok(()) } else { util::reply(ctx, "what in ryan's name").await?; - Err(e.into()) + Err(e) }; }, }; @@ -152,7 +152,7 @@ async fn rand_meme(ctx: PoiseContext<'_>, audio_playback: AudioPlayback) -> anyh } util::reply(ctx, "HELP").await?; - Err(e.into()) + Err(e) }, } } @@ -197,7 +197,7 @@ pub async fn rare_meme(ctx: PoiseContext<'_>) -> anyhow::Result<()> { util::reply(ctx, "THE MEME MARKET IS IN FREEFALL").await?; - Err(e.into()) + Err(e) }, } } diff --git a/src/commands/meme/mod.rs b/src/commands/meme/mod.rs index 603ec28..98cabef 100644 --- a/src/commands/meme/mod.rs +++ b/src/commands/meme/mod.rs @@ -15,14 +15,14 @@ use serenity::{ }, }; use songbird::input::{ - core::{ - io::MediaSource, - probe::Hint, - }, AudioStream, AudioStreamError, Compose, Input, + core::{ + io::MediaSource, + probe::Hint, + }, }; pub use self::{ @@ -32,12 +32,13 @@ pub use self::{ invoke::*, }; use crate::{ + PoiseContext, bot::PlaybackKey, commands::{ playback, playback::{ - songbird, InvokeInfo, + songbird, }, }, db::{ @@ -45,7 +46,6 @@ use crate::{ Meme, }, util, - PoiseContext, }; mod create; @@ -78,8 +78,8 @@ async fn send_meme( t: &Meme, conn: &mut AsyncPgConnection, ) -> anyhow::Result<()> { - let should_tts = - t.content.as_ref().map(|t| !t.is_empty()).unwrap_or(false) && random::() % 25 == 0; + let should_tts = t.content.as_ref().map(|t| !t.is_empty()).unwrap_or(false) + && random::().is_multiple_of(25); tracing::debug!(should_tts, meme = ?t, "sending meme"); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index b0ef83b..a5ee1bd 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,8 +1,8 @@ use poise::builtins::PrettyHelpConfiguration; use crate::{ - commands::playback::_play, PoiseContext, + commands::playback::_play, }; #[cfg(feature = "games")] @@ -42,20 +42,16 @@ pub fn commands() -> Vec> { /// Print this help text. #[poise::command(prefix_command, aliases("halp"))] pub async fn help(ctx: PoiseContext<'_>, command: Option) -> anyhow::Result<()> { - poise::builtins::pretty_help( - ctx, - command.as_ref().map(|x| x.as_str()), - PrettyHelpConfiguration { - ..Default::default() - }, - ) + poise::builtins::pretty_help(ctx, command.as_deref(), PrettyHelpConfiguration { + ..Default::default() + }) .await?; Ok(()) } pub async fn link_unrecognized(ctx: PoiseContext<'_>, u: url::Url) -> anyhow::Result<()> { - let _ = _play(ctx, &u).await?; + _play(ctx, &u).await?; Ok(()) } diff --git a/src/db/models.rs b/src/db/models.rs index e699630..2d77979 100644 --- a/src/db/models.rs +++ b/src/db/models.rs @@ -4,10 +4,10 @@ use anyhow::{ }; use chrono::naive::NaiveDateTime; use diesel::{ - prelude::*, Identifiable, Insertable, Queryable, + prelude::*, }; use diesel_async::{ AsyncPgConnection, @@ -40,9 +40,7 @@ impl Meme { } pub async fn audio(&self, conn: &mut AsyncPgConnection) -> Option> { - let Some(x) = self.audio_id else { - return None; - }; + let x = self.audio_id?; Some(audio::table.filter(audio::id.eq(x)).first(conn).await.map_err(Error::from)) } diff --git a/src/lib.rs b/src/lib.rs index a45f9fb..90c7c14 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,4 @@ #![feature(try_blocks)] -#![feature(maybe_uninit_slice)] #[cfg(feature = "db")] pub mod db; diff --git a/src/util/mod.rs b/src/util/mod.rs index db85a9b..2035054 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -212,7 +212,7 @@ pub fn voice_states_by_channel(ctx: PoiseContext<'_>) -> HashMap) -> Option { } let voice_states = voice_states_by_channel(ctx); - let max_pop = voice_states.iter().map(|(_, states)| states.len()).max(); + let max_pop = voice_states.values().map(|states| states.len()).max(); let matching_channels = voice_states .iter() diff --git a/src/util/windows.rs b/src/util/windows.rs index 7003490..4764201 100644 --- a/src/util/windows.rs +++ b/src/util/windows.rs @@ -1,25 +1,27 @@ use grate::tracing; use windows::{ - core::{ - HSTRING, - PWSTR, - }, Win32::{ Foundation, System::Services, }, + core::{ + HSTRING, + PWSTR, + }, }; lazy_static::lazy_static! { static ref PSQL_REGEX: regex::Regex = regex::Regex::new(r#"^(?:postgresql|psql|postgres)-.*([\d\.]*)"#).unwrap(); } -pub unsafe fn ensure_postgres_started() -> windows::core::Result { - let sc_manager = Services::OpenSCManagerW( - None, - None, - (Foundation::GENERIC_READ | Foundation::GENERIC_WRITE).0, - )?; +pub fn ensure_postgres_started() -> windows::core::Result { + let sc_manager = unsafe { + Services::OpenSCManagerW( + None, + None, + (Foundation::GENERIC_READ | Foundation::GENERIC_WRITE).0, + )? + }; let Some(svc) = get_psql_service(sc_manager)? else { return Ok(false); @@ -41,25 +43,29 @@ pub unsafe fn ensure_postgres_started() -> windows::core::Result { }, } - let service = - Services::OpenServiceW(sc_manager, &HSTRING::from(&svc.name), Services::SERVICE_START)?; + unsafe { + let service = + Services::OpenServiceW(sc_manager, &HSTRING::from(&svc.name), Services::SERVICE_START)?; - Services::StartServiceW(service, None)?; + Services::StartServiceW(service, None)?; - tracing::info!("started postgres service"); + tracing::info!("started postgres service"); - Services::CloseServiceHandle(service)?; - Services::CloseServiceHandle(sc_manager)?; + Services::CloseServiceHandle(service)?; + Services::CloseServiceHandle(sc_manager)?; + } Ok(true) } -pub unsafe fn shutdown_postgres() -> windows::core::Result<()> { - let sc_manager = Services::OpenSCManagerW( - None, - None, - (Foundation::GENERIC_READ | Foundation::GENERIC_WRITE).0, - )?; +pub fn shutdown_postgres() -> windows::core::Result<()> { + let sc_manager = unsafe { + Services::OpenSCManagerW( + None, + None, + (Foundation::GENERIC_READ | Foundation::GENERIC_WRITE).0, + ) + }?; let Some(svc) = get_psql_service(sc_manager)? else { tracing::warn!("wasn't able to find postgres service"); @@ -71,8 +77,9 @@ pub unsafe fn shutdown_postgres() -> windows::core::Result<()> { return Ok(()); } - let service = - Services::OpenServiceW(sc_manager, &HSTRING::from(&svc.name), Services::SERVICE_STOP)?; + let service = unsafe { + Services::OpenServiceW(sc_manager, &HSTRING::from(&svc.name), Services::SERVICE_STOP) + }?; let mut params = Services::SERVICE_CONTROL_STATUS_REASON_PARAMSW { dwReason: Services::SERVICE_STOP_REASON_FLAG_PLANNED @@ -82,22 +89,26 @@ pub unsafe fn shutdown_postgres() -> windows::core::Result<()> { ServiceStatus: Services::SERVICE_STATUS_PROCESS::default(), }; - Services::ControlServiceExW( - service, - Services::SERVICE_CONTROL_STOP, - Services::SERVICE_CONTROL_STATUS_REASON_INFO, - &mut params as *mut _ as _, - )?; + unsafe { + Services::ControlServiceExW( + service, + Services::SERVICE_CONTROL_STOP, + Services::SERVICE_CONTROL_STATUS_REASON_INFO, + &mut params as *mut _ as _, + )?; + } tracing::info!("stopped postgres service"); - Services::CloseServiceHandle(service)?; - Services::CloseServiceHandle(sc_manager)?; + unsafe { + Services::CloseServiceHandle(service)?; + Services::CloseServiceHandle(sc_manager)?; + } Ok(()) } -pub unsafe fn get_psql_service( +pub fn get_psql_service( sc_manager: Services::SC_HANDLE, ) -> windows::core::Result> { let services = list_services(sc_manager)?; @@ -133,9 +144,7 @@ pub struct ServiceStatus { pub status: Services::SERVICE_STATUS_PROCESS, } -pub unsafe fn list_services( - sc_manager: Services::SC_HANDLE, -) -> windows::core::Result> { +pub fn list_services(sc_manager: Services::SC_HANDLE) -> windows::core::Result> { let mut bytes_needed: u32 = 0; let mut services_returned: u32 = 0; let mut resume = 0; @@ -145,17 +154,19 @@ pub unsafe fn list_services( loop { let mut data = [0u8; 256 * 1024]; - let result = Services::EnumServicesStatusExW( - sc_manager, - Services::SC_ENUM_PROCESS_INFO, - Services::SERVICE_WIN32, - Services::SERVICE_STATE_ALL, - Some(&mut data), - &mut bytes_needed as *mut _, - &mut services_returned as *mut _, - Some(&mut resume), - None, - ); + let result = unsafe { + Services::EnumServicesStatusExW( + sc_manager, + Services::SC_ENUM_PROCESS_INFO, + Services::SERVICE_WIN32, + Services::SERVICE_STATE_ALL, + Some(&mut data), + &mut bytes_needed as *mut _, + &mut services_returned as *mut _, + Some(&mut resume), + None, + ) + }; if let Err(ref e) = result && e.code() != Foundation::ERROR_MORE_DATA.to_hresult() @@ -168,11 +179,13 @@ pub unsafe fn list_services( ); let elems = data - .into_iter() - .map(|x| core::mem::transmute::<_, Services::ENUM_SERVICE_STATUS_PROCESSW>(*x)) + .iter() + .map(|x| unsafe { + core::mem::transmute::<_, Services::ENUM_SERVICE_STATUS_PROCESSW>(*x) + }) .map(|x| ServiceStatus { - display_name: x.lpDisplayName.to_string().unwrap(), - name: x.lpServiceName.to_string().unwrap(), + display_name: unsafe { x.lpDisplayName.to_string() }.unwrap(), + name: unsafe { x.lpServiceName.to_string() }.unwrap(), status: x.ServiceStatusProcess, }); -- cgit v1.3.1