From 4967e9aa374b89e3f09f391ea621714aa64029a7 Mon Sep 17 00:00:00 2001 From: Alexander Medvedev <71594357+Snowiiii@users.noreply.github.com> Date: Tue, 8 Oct 2024 20:02:56 +0100 Subject: [PATCH] New clipps lints --- pumpkin/src/client/container.rs | 25 +++++++++++-------------- pumpkin/src/client/mod.rs | 2 +- pumpkin/src/client/player_packet.rs | 12 ++++++------ pumpkin/src/commands/cmd_echest.rs | 2 +- pumpkin/src/commands/cmd_gamemode.rs | 2 +- pumpkin/src/commands/cmd_help.rs | 2 +- pumpkin/src/commands/cmd_kill.rs | 2 +- pumpkin/src/commands/cmd_pumpkin.rs | 2 +- pumpkin/src/commands/cmd_stop.rs | 2 +- pumpkin/src/commands/mod.rs | 6 +++--- pumpkin/src/commands/tree.rs | 14 +++++++------- pumpkin/src/commands/tree_builder.rs | 4 ++-- pumpkin/src/entity/mod.rs | 2 +- pumpkin/src/entity/player.rs | 12 ++++++------ pumpkin/src/main.rs | 13 +++++++++++++ pumpkin/src/rcon/mod.rs | 4 ++-- pumpkin/src/rcon/packet.rs | 14 +++++++------- pumpkin/src/server/connection_cache.rs | 6 +++--- pumpkin/src/server/key_store.rs | 2 +- 19 files changed, 69 insertions(+), 59 deletions(-) diff --git a/pumpkin/src/client/container.rs b/pumpkin/src/client/container.rs index dc15a21cd..bb3a01d1a 100644 --- a/pumpkin/src/client/container.rs +++ b/pumpkin/src/client/container.rs @@ -40,7 +40,7 @@ impl Player { let window_title = container .as_ref() .map(|container| container.window_name()) - .unwrap_or(inventory.window_name()); + .unwrap_or_else(|| inventory.window_name()); let title = TextComponent::text(window_title); self.client.send_packet(&COpenScreen::new( @@ -64,13 +64,12 @@ impl Player { .map(Slot::from) .collect_vec(); - let carried_item = { - if let Some(item) = self.carried_item.load().as_ref() { - item.into() - } else { - Slot::empty() - } - }; + let carried_item = self + .carried_item + .load() + .as_ref() + .map_or_else(Slot::empty, |item| item.into()); + // Gets the previous value let i = inventory .state_id @@ -378,7 +377,7 @@ impl Player { } } - async fn get_current_players_in_container(&self, server: &Server) -> Vec> { + async fn get_current_players_in_container(&self, server: &Server) -> Vec> { let player_ids = { let open_containers = server.open_containers.read(); open_containers @@ -453,10 +452,8 @@ impl Player { } pub fn get_open_container(&self, server: &Server) -> Option>>> { - if let Some(id) = self.open_container.load() { - server.try_get_container(self.entity_id(), id) - } else { - None - } + self.open_container + .load() + .map_or_else(|| None, |id| server.try_get_container(self.entity_id(), id)) } } diff --git a/pumpkin/src/client/mod.rs b/pumpkin/src/client/mod.rs index ca744d1b5..d88889070 100644 --- a/pumpkin/src/client/mod.rs +++ b/pumpkin/src/client/mod.rs @@ -371,7 +371,7 @@ impl Client { match self.connection_state.load() { ConnectionState::Login => { self.try_send_packet(&CLoginDisconnect::new( - &serde_json::to_string_pretty(&reason).unwrap_or("".into()), + &serde_json::to_string_pretty(&reason).unwrap_or_else(|_| "".into()), )) .unwrap_or_else(|_| self.close()); } diff --git a/pumpkin/src/client/player_packet.rs b/pumpkin/src/client/player_packet.rs index 5e2e48da7..a9a881c0c 100644 --- a/pumpkin/src/client/player_packet.rs +++ b/pumpkin/src/client/player_packet.rs @@ -110,9 +110,9 @@ impl Player { &[self.client.token], &CUpdateEntityPos::new( entity_id.into(), - (x * 4096.0 - lastx * 4096.0) as i16, - (y * 4096.0 - lasty * 4096.0) as i16, - (z * 4096.0 - lastz * 4096.0) as i16, + x.mul_add(4096.0, -(lastx * 4096.0)) as i16, + y.mul_add(4096.0, -(lasty * 4096.0)) as i16, + z.mul_add(4096.0, -(lastz * 4096.0)) as i16, position.ground, ), ); @@ -180,9 +180,9 @@ impl Player { &[self.client.token], &CUpdateEntityPosRot::new( entity_id.into(), - (x * 4096.0 - lastx * 4096.0) as i16, - (y * 4096.0 - lasty * 4096.0) as i16, - (z * 4096.0 - lastz * 4096.0) as i16, + x.mul_add(4096.0, -(lastx * 4096.0)) as i16, + y.mul_add(4096.0, -(lasty * 4096.0)) as i16, + z.mul_add(4096.0, -(lastz * 4096.0)) as i16, yaw as u8, pitch as u8, position_rotation.ground, diff --git a/pumpkin/src/commands/cmd_echest.rs b/pumpkin/src/commands/cmd_echest.rs index 690fff41c..e793ccb8a 100644 --- a/pumpkin/src/commands/cmd_echest.rs +++ b/pumpkin/src/commands/cmd_echest.rs @@ -7,7 +7,7 @@ const NAMES: [&str; 2] = ["echest", "enderchest"]; const DESCRIPTION: &str = "Show your personal enderchest (this command is used for testing container behaviour)"; -pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> { +pub fn init_command_tree<'a>() -> CommandTree<'a> { CommandTree::new(NAMES, DESCRIPTION).execute(&|sender, server, _| { if let Some(player) = sender.as_mut_player() { let entity_id = player.entity_id(); diff --git a/pumpkin/src/commands/cmd_gamemode.rs b/pumpkin/src/commands/cmd_gamemode.rs index c950df438..484d85c0e 100644 --- a/pumpkin/src/commands/cmd_gamemode.rs +++ b/pumpkin/src/commands/cmd_gamemode.rs @@ -56,7 +56,7 @@ pub fn parse_arg_gamemode(consumed_args: &ConsumedArgs) -> Result() -> CommandTree<'a> { +pub fn init_command_tree<'a>() -> CommandTree<'a> { CommandTree::new(NAMES, DESCRIPTION).with_child( require(&|sender| sender.permission_lvl() >= 2).with_child( argument(ARG_GAMEMODE, consume_arg_gamemode) diff --git a/pumpkin/src/commands/cmd_help.rs b/pumpkin/src/commands/cmd_help.rs index 59788b221..a955f4c8c 100644 --- a/pumpkin/src/commands/cmd_help.rs +++ b/pumpkin/src/commands/cmd_help.rs @@ -32,7 +32,7 @@ fn parse_arg_command<'a>( .map_err(|_| InvalidConsumptionError(Some(command_name.into()))) } -pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> { +pub fn init_command_tree<'a>() -> CommandTree<'a> { CommandTree::new(NAMES, DESCRIPTION) .with_child( argument(ARG_COMMAND, consume_arg_command).execute(&|sender, server, args| { diff --git a/pumpkin/src/commands/cmd_kill.rs b/pumpkin/src/commands/cmd_kill.rs index d3b8135e6..3b0bdf3fa 100644 --- a/pumpkin/src/commands/cmd_kill.rs +++ b/pumpkin/src/commands/cmd_kill.rs @@ -14,7 +14,7 @@ pub fn consume_arg_target(_src: &CommandSender, args: &mut RawArgs) -> Option() -> CommandTree<'a> { +pub fn init_command_tree<'a>() -> CommandTree<'a> { CommandTree::new(NAMES, DESCRIPTION).with_child( argument(ARG_TARGET, consume_arg_target).execute(&|sender, server, args| { let target = parse_arg_player(sender, server, ARG_TARGET, args)?; diff --git a/pumpkin/src/commands/cmd_pumpkin.rs b/pumpkin/src/commands/cmd_pumpkin.rs index ad0b3561e..0d8d7565f 100644 --- a/pumpkin/src/commands/cmd_pumpkin.rs +++ b/pumpkin/src/commands/cmd_pumpkin.rs @@ -8,7 +8,7 @@ const NAMES: [&str; 1] = ["pumpkin"]; const DESCRIPTION: &str = "Display information about Pumpkin."; -pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> { +pub fn init_command_tree<'a>() -> CommandTree<'a> { CommandTree::new(NAMES, DESCRIPTION).execute(&|sender, _, _| { let version = env!("CARGO_PKG_VERSION"); let description = env!("CARGO_PKG_DESCRIPTION"); diff --git a/pumpkin/src/commands/cmd_stop.rs b/pumpkin/src/commands/cmd_stop.rs index 5e12ccb48..2d90ae707 100644 --- a/pumpkin/src/commands/cmd_stop.rs +++ b/pumpkin/src/commands/cmd_stop.rs @@ -8,7 +8,7 @@ const NAMES: [&str; 1] = ["stop"]; const DESCRIPTION: &str = "Stop the server."; -pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> { +pub fn init_command_tree<'a>() -> CommandTree<'a> { CommandTree::new(NAMES, DESCRIPTION).with_child( require(&|sender| sender.permission_lvl() >= 4).execute(&|sender, _, _args| { sender diff --git a/pumpkin/src/commands/mod.rs b/pumpkin/src/commands/mod.rs index 912b03183..322b96202 100644 --- a/pumpkin/src/commands/mod.rs +++ b/pumpkin/src/commands/mod.rs @@ -35,7 +35,7 @@ impl<'a> CommandSender<'a> { } } - pub fn is_player(&self) -> bool { + pub const fn is_player(&self) -> bool { match self { CommandSender::Console => false, CommandSender::Player(_) => true, @@ -43,7 +43,7 @@ impl<'a> CommandSender<'a> { } } - pub fn is_console(&self) -> bool { + pub const fn is_console(&self) -> bool { match self { CommandSender::Console => true, CommandSender::Player(_) => false, @@ -59,7 +59,7 @@ impl<'a> CommandSender<'a> { } /// todo: implement - pub fn permission_lvl(&self) -> i32 { + pub const fn permission_lvl(&self) -> i32 { match self { CommandSender::Rcon(_) => 4, CommandSender::Console => 4, diff --git a/pumpkin/src/commands/tree.rs b/pumpkin/src/commands/tree.rs index 6f1d8517f..ebbe3d801 100644 --- a/pumpkin/src/commands/tree.rs +++ b/pumpkin/src/commands/tree.rs @@ -3,20 +3,20 @@ use crate::commands::CommandSender; use std::collections::{HashMap, VecDeque}; /// see [crate::commands::tree_builder::argument] -pub(crate) type RawArgs<'a> = Vec<&'a str>; +pub type RawArgs<'a> = Vec<&'a str>; /// see [crate::commands::tree_builder::argument] and [CommandTree::execute]/[crate::commands::tree_builder::NonLeafNodeBuilder::execute] -pub(crate) type ConsumedArgs<'a> = HashMap<&'a str, String>; +pub type ConsumedArgs<'a> = HashMap<&'a str, String>; /// see [crate::commands::tree_builder::argument] -pub(crate) type ArgumentConsumer<'a> = fn(&CommandSender, &mut RawArgs) -> Option; +pub type ArgumentConsumer<'a> = fn(&CommandSender, &mut RawArgs) -> Option; -pub(crate) struct Node<'a> { +pub struct Node<'a> { pub(crate) children: Vec, pub(crate) node_type: NodeType<'a>, } -pub(crate) enum NodeType<'a> { +pub enum NodeType<'a> { ExecuteLeaf { run: &'a RunFunctionType, }, @@ -32,12 +32,12 @@ pub(crate) enum NodeType<'a> { }, } -pub(crate) enum Command<'a> { +pub enum Command<'a> { Tree(CommandTree<'a>), Alias(&'a str), } -pub(crate) struct CommandTree<'a> { +pub struct CommandTree<'a> { pub(crate) nodes: Vec>, pub(crate) children: Vec, pub(crate) names: Vec<&'a str>, diff --git a/pumpkin/src/commands/tree_builder.rs b/pumpkin/src/commands/tree_builder.rs index 00a5449f1..ecb79880e 100644 --- a/pumpkin/src/commands/tree_builder.rs +++ b/pumpkin/src/commands/tree_builder.rs @@ -101,7 +101,7 @@ impl<'a> NodeBuilder<'a> for NonLeafNodeBuilder<'a> { impl<'a> NonLeafNodeBuilder<'a> { /// Add a child [Node] to this one. - pub fn with_child(mut self, child: NonLeafNodeBuilder<'a>) -> Self { + pub fn with_child(mut self, child: Self) -> Self { self.child_nodes.push(child); self } @@ -124,7 +124,7 @@ impl<'a> NonLeafNodeBuilder<'a> { /// Matches a sting literal. #[expect(dead_code)] // todo: remove (so far no commands requiring this are implemented) -pub fn literal(string: &str) -> NonLeafNodeBuilder { +pub const fn literal(string: &str) -> NonLeafNodeBuilder { NonLeafNodeBuilder { node_type: NodeType::Literal { string }, child_nodes: Vec::new(), diff --git a/pumpkin/src/entity/mod.rs b/pumpkin/src/entity/mod.rs index 68c323a6b..71204dfa7 100644 --- a/pumpkin/src/entity/mod.rs +++ b/pumpkin/src/entity/mod.rs @@ -146,7 +146,7 @@ impl Entity { // This has some vanilla magic let mut x = x; let mut z = z; - while x * x + z * z < 1.0E-5 { + while x.mul_add(x, z * z) < 1.0E-5 { x = (rand::random::() - rand::random::()) * 0.01; z = (rand::random::() - rand::random::()) * 0.01; } diff --git a/pumpkin/src/entity/player.rs b/pumpkin/src/entity/player.rs index dd9a900fe..aee382fe2 100644 --- a/pumpkin/src/entity/player.rs +++ b/pumpkin/src/entity/player.rs @@ -96,9 +96,8 @@ impl Player { entity_id: EntityId, gamemode: GameMode, ) -> Self { - let gameprofile = match client.gameprofile.lock().clone() { - Some(profile) => profile, - None => { + let gameprofile = client.gameprofile.lock().clone().map_or_else( + || { log::error!("No gameprofile?. Impossible"); GameProfile { id: uuid::Uuid::new_v4(), @@ -106,8 +105,9 @@ impl Player { properties: vec![], profile_actions: None, } - } - }; + }, + |profile| profile, + ); let config = client.config.lock().clone().unwrap_or_default(); Self { entity: Entity::new(entity_id, world, EntityType::Player, 1.62), @@ -135,7 +135,7 @@ impl Player { self.entity.world.remove_player(self); } - pub fn entity_id(&self) -> EntityId { + pub const fn entity_id(&self) -> EntityId { self.entity.entity_id } diff --git a/pumpkin/src/main.rs b/pumpkin/src/main.rs index bb709c22c..ab21ef1b3 100644 --- a/pumpkin/src/main.rs +++ b/pumpkin/src/main.rs @@ -1,3 +1,16 @@ +#![deny(clippy::all)] +// #![warn(clippy::pedantic)] +// #![warn(clippy::restriction)] +#![warn(clippy::nursery)] +#![warn(clippy::cargo)] +// expect +#![expect(clippy::cargo_common_metadata)] +#![expect(clippy::multiple_crate_versions)] +#![expect(clippy::while_float)] +#![expect(clippy::significant_drop_in_scrutinee)] +#![expect(clippy::significant_drop_tightening)] +#![expect(clippy::future_not_send)] +#![expect(clippy::single_call_fn)] #![expect(clippy::await_holding_lock)] #[cfg(target_os = "wasi")] diff --git a/pumpkin/src/rcon/mod.rs b/pumpkin/src/rcon/mod.rs index 3aad9732b..252210f8c 100644 --- a/pumpkin/src/rcon/mod.rs +++ b/pumpkin/src/rcon/mod.rs @@ -28,7 +28,7 @@ pub enum RCONError { const SERVER: Token = Token(0); -pub struct RCONServer {} +pub struct RCONServer; impl RCONServer { pub async fn new(config: &RCONConfig, server: Arc) -> Result { @@ -122,7 +122,7 @@ pub struct RCONClient { } impl RCONClient { - pub fn new(connection: TcpStream) -> Self { + pub const fn new(connection: TcpStream) -> Self { Self { connection, logged_in: false, diff --git a/pumpkin/src/rcon/packet.rs b/pumpkin/src/rcon/packet.rs index 7c3edac83..f4cdc7925 100644 --- a/pumpkin/src/rcon/packet.rs +++ b/pumpkin/src/rcon/packet.rs @@ -8,7 +8,7 @@ use thiserror::Error; use tokio::io::AsyncReadExt; /// Client -> Server -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[repr(i32)] pub enum ServerboundPacket { /// Typically, the first packet sent by the client, which is used to authenticate the connection with the server. @@ -19,7 +19,7 @@ pub enum ServerboundPacket { } impl ServerboundPacket { - pub fn from_i32(n: i32) -> Self { + pub const fn from_i32(n: i32) -> Self { match n { 3 => Self::Auth, 2 => Self::ExecCommand, @@ -28,7 +28,7 @@ impl ServerboundPacket { } } -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[repr(i32)] /// Server -> Client pub enum ClientboundPacket { @@ -76,7 +76,7 @@ pub struct Packet { } impl Packet { - pub async fn deserialize(incoming: &mut Vec) -> Result, PacketError> { + pub async fn deserialize(incoming: &mut Vec) -> Result, PacketError> { if incoming.len() < 4 { return Ok(None); } @@ -98,7 +98,7 @@ impl Packet { } incoming.drain(0..len as usize); - let packet = Packet { + let packet = Self { id, ptype: ServerboundPacket::from_i32(ty), body: String::from_utf8(payload).map_err(PacketError::InvalidBody)?, @@ -110,11 +110,11 @@ impl Packet { &self.body } - pub fn get_type(&self) -> ServerboundPacket { + pub const fn get_type(&self) -> ServerboundPacket { self.ptype } - pub fn get_id(&self) -> i32 { + pub const fn get_id(&self) -> i32 { self.id } } diff --git a/pumpkin/src/server/connection_cache.rs b/pumpkin/src/server/connection_cache.rs index 76aa3b076..c87091fc8 100644 --- a/pumpkin/src/server/connection_cache.rs +++ b/pumpkin/src/server/connection_cache.rs @@ -22,9 +22,9 @@ pub struct CachedBranding { } impl CachedBranding { - pub fn new() -> CachedBranding { + pub fn new() -> Self { let cached_server_brand = Self::build_brand(); - CachedBranding { + Self { cached_server_brand, } } @@ -46,7 +46,7 @@ impl CachedStatus { let status_response_json = serde_json::to_string(&status_response) .expect("Failed to parse Status response into JSON"); - CachedStatus { + Self { _status_response: status_response, status_response_json, } diff --git a/pumpkin/src/server/key_store.rs b/pumpkin/src/server/key_store.rs index 54393dccf..12bfb829c 100644 --- a/pumpkin/src/server/key_store.rs +++ b/pumpkin/src/server/key_store.rs @@ -22,7 +22,7 @@ impl KeyStore { &private_key.e().to_bytes_be(), ) .into_boxed_slice(); - KeyStore { + Self { _public_key: public_key, private_key, public_key_der,