From 08602f001008f78bff6e2c819081801f48c23627 Mon Sep 17 00:00:00 2001 From: Bjorn Beishline <75190918+BjornTheProgrammer@users.noreply.github.com> Date: Wed, 4 Feb 2026 00:07:45 -0700 Subject: [PATCH] Fix all clippy lints --- ARCHITECTURE.md | 2 -- rust/build/protobufs.rs | 18 ++++++++++-- rust/src/java/native_callbacks/abilities.rs | 21 +++++++------- rust/src/java/native_callbacks/events.rs | 30 +++++++------------ rust/src/java/native_callbacks/location.rs | 32 +++++++++------------ rust/src/java/native_callbacks/registry.rs | 20 ++++++------- 6 files changed, 59 insertions(+), 64 deletions(-) diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index e4dc556..fbb83ec 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -122,5 +122,3 @@ but in this project, it is very important. The build process is split into modul 1. `rust/build/main.rs` - Entry point that orchestrates the build 2. `rust/build/java.rs` - Embeds all transitive dependencies of the `paper-api` Java library and the PatchBukkit jar 3. `rust/build/protobufs.rs` - Generates Rust protobuf code and FFI entry points from `.proto` definitions - -## Adding a new FFI method diff --git a/rust/build/protobufs.rs b/rust/build/protobufs.rs index fc60f32..b45a25c 100644 --- a/rust/build/protobufs.rs +++ b/rust/build/protobufs.rs @@ -63,7 +63,7 @@ impl FfiServiceGenerator { fn package_to_module_path(&self, package: &str) -> String { package .split('.') - .map(|s| to_snake_case(s)) + .map(to_snake_case) .collect::>() .join("::") } @@ -92,7 +92,13 @@ impl ServiceGenerator for FfiServiceGenerator { }); buf.push_str(&format!( - r#" + r#"/// FFI function for {fn_name} +/// +/// # Safety +/// +/// - `input_ptr` must be a valid pointer to `input_len` bytes of memory +/// - `output_len` must be a valid pointer to write the output length +/// - The caller is responsible for freeing the returned pointer using `ffi_free_bytes` #[unsafe(no_mangle)] pub unsafe extern "C" fn {fn_name}( input_ptr: *const u8, @@ -170,7 +176,13 @@ pub fn setup_protobufs(base: PathBuf) { writeln!( file, - r#" + r#"/// Frees bytes allocated by FFI functions +/// +/// # Safety +/// +/// - `ptr` must have been returned by an FFI function in this module +/// - `len` must be the length that was written to `output_len` +/// - The pointer must not have been freed previously #[unsafe(no_mangle)] pub unsafe extern "C" fn ffi_free_bytes(ptr: *mut u8, len: usize) {{ if !ptr.is_null() && len > 0 {{ diff --git a/rust/src/java/native_callbacks/abilities.rs b/rust/src/java/native_callbacks/abilities.rs index ccb2cd4..5fe30b4 100644 --- a/rust/src/java/native_callbacks/abilities.rs +++ b/rust/src/java/native_callbacks/abilities.rs @@ -7,22 +7,21 @@ use crate::proto::patchbukkit::{ pub fn ffi_native_bridge_get_abilities_impl(request: Uuid) -> Option { let ctx = CALLBACK_CONTEXT.get()?; let player_uuid = uuid::Uuid::parse_str(&request.value).ok()?; - let mut abilities = Abilities::default(); let player = ctx.plugin_context.server.get_player_by_uuid(player_uuid)?; - let pumpkin_abilities = tokio::task::block_in_place(|| { + let abilities = tokio::task::block_in_place(|| { ctx.runtime .block_on(async { player.abilities.lock().await }) }); - abilities.invulnerable = pumpkin_abilities.invulnerable; - abilities.flying = pumpkin_abilities.flying; - abilities.allow_flying = pumpkin_abilities.allow_flying; - abilities.creative = pumpkin_abilities.creative; - abilities.allow_modify_world = pumpkin_abilities.allow_modify_world; - abilities.fly_speed = pumpkin_abilities.fly_speed; - abilities.walk_speed = pumpkin_abilities.walk_speed; - - return Some(abilities); + Some(Abilities { + invulnerable: abilities.invulnerable, + flying: abilities.flying, + allow_flying: abilities.allow_flying, + creative: abilities.creative, + allow_modify_world: abilities.allow_modify_world, + fly_speed: abilities.fly_speed, + walk_speed: abilities.walk_speed, + }) } pub fn ffi_native_bridge_set_abilities_impl(request: SetAbilitiesRequest) -> Option { diff --git a/rust/src/java/native_callbacks/events.rs b/rust/src/java/native_callbacks/events.rs index a1e0d55..4b085a4 100644 --- a/rust/src/java/native_callbacks/events.rs +++ b/rust/src/java/native_callbacks/events.rs @@ -74,27 +74,19 @@ pub fn ffi_native_bridge_call_event_impl(request: CallEventRequest) -> Option { - if let Ok(uuid) = - uuid::Uuid::parse_str(&player_join_event_data.player_uuid?.value) - { - if let Some(player) = context.server.get_player_by_uuid(uuid) { - let pumpkin_event = PlayerJoinEvent::new( - player, - TextComponent::from_legacy_string( - &player_join_event_data.join_message, - ), - ); - context.server.plugin_manager.fire(pumpkin_event).await; - return Some(true); - } - } - Some(false) + let uuid = + uuid::Uuid::parse_str(&player_join_event_data.player_uuid?.value).ok()?; + let player = context.server.get_player_by_uuid(uuid)?; + let pumpkin_event = PlayerJoinEvent::new( + player, + TextComponent::from_legacy_string(&player_join_event_data.join_message), + ); + context.server.plugin_manager.fire(pumpkin_event).await; + Some(true) } } }) - }); + })?; - let mut response = CallEventResponse::default(); - response.handled = handled?; - Some(response) + Some(CallEventResponse { handled }) } diff --git a/rust/src/java/native_callbacks/location.rs b/rust/src/java/native_callbacks/location.rs index 661ce6b..7582945 100644 --- a/rust/src/java/native_callbacks/location.rs +++ b/rust/src/java/native_callbacks/location.rs @@ -28,24 +28,20 @@ pub fn ffi_native_bridge_get_location_impl(entity_uuid: Uuid) -> Option { let sounds = pumpkin_data::sound::Sound::slice() .iter() - .map(|s| { - let mut sound_event = SoundEvent::default(); - sound_event.id = *s as u32; - sound_event.name = s.to_name().to_string(); - sound_event + .map(|s| SoundEvent { + id: *s as u32, + name: s.to_name().to_string(), }) .collect::>(); - let mut data = SoundEventRegistryData::default(); - data.sound_events = sounds; - Registry::SoundEvent(data) + Registry::SoundEvent(SoundEventRegistryData { + sound_events: sounds, + }) } _ => unreachable!(), }; - let mut response = GetRegistryDataResponse::default(); - response.registry = Some(registry); - Some(response) + Some(GetRegistryDataResponse { + registry: Some(registry), + }) }