From 5039f9668f3512240af22ac6bb49140012885509 Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 10 Dec 2021 22:01:16 +0000 Subject: [PATCH] clippy --- minecraft-chat/src/component.rs | 20 +++++++++---------- minecraft-chat/src/style.rs | 12 ++++------- minecraft-chat/tests/integration_test.rs | 2 +- minecraft-client/src/lib.rs | 2 +- minecraft-protocol/src/connection.rs | 4 ++-- minecraft-protocol/src/mc_buf.rs | 14 +++++++------ .../handshake/client_intention_packet.rs | 2 +- minecraft-protocol/src/packets/mod.rs | 8 ++++---- .../clientbound_status_response_packet.rs | 2 +- .../serverbound_status_request_packet.rs | 3 +-- minecraft-protocol/src/resolver.rs | 2 +- .../src/server_status_pinger.rs | 2 +- 12 files changed, 35 insertions(+), 38 deletions(-) diff --git a/minecraft-chat/src/component.rs b/minecraft-chat/src/component.rs index 39687ed5..c59a5d5d 100644 --- a/minecraft-chat/src/component.rs +++ b/minecraft-chat/src/component.rs @@ -1,4 +1,4 @@ -use std::borrow::BorrowMut; + use serde_json; @@ -42,7 +42,7 @@ impl Component { // otherwise add the component to the array let c = Component::new(&with[i])?; if let Component::TextComponent(text_component) = c { - if text_component.base.siblings.len() == 0 + if text_component.base.siblings.is_empty() && text_component.base.style.is_empty() { with_array.push(StringOrComponent::String(text_component.text)); @@ -91,11 +91,11 @@ impl Component { return Err(format!("Don't know how to turn {} into a Component", json)); } // object = GsonHelper.getAsString(jsonObject, "nbt"); - let nbt = json.get("nbt").unwrap().to_string(); + let _nbt = json.get("nbt").unwrap().to_string(); // Optional optional = this.parseSeparator(type, jsonDeserializationContext, jsonObject); - let separator = Component::parse_separator(json)?; + let _separator = Component::parse_separator(json)?; - let interpret = match json.get("interpret") { + let _interpret = match json.get("interpret") { Some(v) => v.as_bool().ok_or(Some(false)).unwrap(), None => false, }; @@ -127,7 +127,7 @@ impl Component { Some(r) => r, None => return Err("Extra isn't an array".to_string()), }; - if extra.len() == 0 { + if extra.is_empty() { return Err("Unexpected empty array of components".to_string()); } for extra_component in extra { @@ -183,10 +183,10 @@ impl Component { } /// Recursively call the function for every component in this component - pub fn visit(&self, f: &mut F) -> () + pub fn visit(&self, f: &mut F) where // The closure takes an `i32` and returns an `i32`. - F: FnMut(&Component) -> (), + F: FnMut(&Component), { f(self); self.get_base() @@ -211,9 +211,9 @@ impl Component { let ansi_text = running_style.compare_ansi(component_style); built_string.push_str(&ansi_text); - built_string.push_str(&component_text); + built_string.push_str(component_text); - running_style.apply(&component_style); + running_style.apply(component_style); }); if !running_style.is_empty() { diff --git a/minecraft-chat/src/style.rs b/minecraft-chat/src/style.rs index 5d5cc8a5..ecde7c9e 100644 --- a/minecraft-chat/src/style.rs +++ b/minecraft-chat/src/style.rs @@ -10,7 +10,7 @@ pub struct TextColor { impl TextColor { pub fn parse(value: String) -> Result { - if value.starts_with("#") { + if value.starts_with('#') { let n = value.chars().skip(1).collect::(); let n = u32::from_str_radix(&n, 16).unwrap(); return Ok(TextColor::from_rgb(n)); @@ -152,7 +152,7 @@ impl<'a> ChatFormatting<'a> { color: Option, ) -> ChatFormatting { ChatFormatting { - name: name, + name, code, is_format, id, @@ -275,11 +275,7 @@ impl Style { } else if self.strikethrough.unwrap_or(false) && !after.strikethrough.unwrap_or(true) { true // if it used to be obfuscated and now it's not, reset - } else if self.obfuscated.unwrap_or(false) && !after.obfuscated.unwrap_or(true) { - true - } else { - false - } + } else { self.obfuscated.unwrap_or(false) && !after.obfuscated.unwrap_or(true) } }; let mut ansi_codes = String::new(); @@ -290,7 +286,7 @@ impl Style { // we should apply after into before and use that as after ansi_codes.push_str(Ansi::RESET); let mut updated_after = self.clone(); - updated_after.apply(&after); + updated_after.apply(after); (Style::new(), updated_after) } else { (self.clone(), after.clone()) diff --git a/minecraft-chat/tests/integration_test.rs b/minecraft-chat/tests/integration_test.rs index 0423a9d7..1a010a13 100644 --- a/minecraft-chat/tests/integration_test.rs +++ b/minecraft-chat/tests/integration_test.rs @@ -2,7 +2,7 @@ use minecraft_chat::{ component::Component, style::{Ansi, ChatFormatting, TextColor}, }; -use serde_json::{Result, Value}; +use serde_json::{Value}; #[test] fn basic_ansi_test() { diff --git a/minecraft-client/src/lib.rs b/minecraft-client/src/lib.rs index 2670101f..3491f6ad 100644 --- a/minecraft-client/src/lib.rs +++ b/minecraft-client/src/lib.rs @@ -1,4 +1,4 @@ -use minecraft_protocol; + #[cfg(test)] mod tests { diff --git a/minecraft-protocol/src/connection.rs b/minecraft-protocol/src/connection.rs index a8319fe1..cfca403c 100644 --- a/minecraft-protocol/src/connection.rs +++ b/minecraft-protocol/src/connection.rs @@ -54,10 +54,10 @@ impl Connection { // the first thing minecraft sends us is the length as a varint, which can be up to 5 bytes long let mut buf = BufReader::with_capacity(4 * 1024 * 1024, &mut self.stream); println!("reading length varint"); - let (packet_size, packet_size_varint_size) = mc_buf::read_varint(&mut buf).await?; + let (_packet_size, _packet_size_varint_size) = mc_buf::read_varint(&mut buf).await?; // then, minecraft tells us the packet id as a varint println!("reading id varint"); - let (packet_id, packet_id_size) = mc_buf::read_varint(&mut buf).await?; + let (packet_id, _packet_id_size) = mc_buf::read_varint(&mut buf).await?; // if we recognize the packet id, parse it diff --git a/minecraft-protocol/src/mc_buf.rs b/minecraft-protocol/src/mc_buf.rs index 68ce6de4..a9ad1642 100644 --- a/minecraft-protocol/src/mc_buf.rs +++ b/minecraft-protocol/src/mc_buf.rs @@ -1,8 +1,8 @@ //! Utilities for reading and writing for the Minecraft protocol -use std::io::{Cursor, Write}; +use std::io::Write; -use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; +use byteorder::{BigEndian, WriteBytesExt}; use tokio::io::{AsyncRead, AsyncReadExt, BufReader}; // const DEFAULT_NBT_QUOTA: u32 = 2097152; @@ -37,8 +37,8 @@ pub async fn read_varint( for i in 0..4 { buf.read_exact(&mut buffer) .await - .or_else(|_| Err("Invalid VarInt".to_string()))?; - ans |= ((buffer[0] & 0b0111_1111) as i32) << 7 * i; + .map_err(|_| "Invalid VarInt".to_string())?; + ans |= ((buffer[0] & 0b0111_1111) as i32) << (7 * i); if buffer[0] & 0b1000_0000 == 0 { return Ok((ans, i + 1)); } @@ -64,6 +64,8 @@ pub fn write_varint(buf: &mut Vec, mut value: i32) { #[cfg(test)] mod tests { use super::*; + use std::io::Cursor; + #[test] fn test_write_varint() { let mut buf = Vec::new(); @@ -98,7 +100,7 @@ pub async fn read_utf_with_len( buf: &mut BufReader, max_length: u32, ) -> Result { - let (length, length_varint_length) = read_varint(buf).await?; + let (length, _length_varint_length) = read_varint(buf).await?; // i don't know why it's multiplied by 4 but it's like that in mojang's code so if length < 0 { return Err( @@ -119,7 +121,7 @@ pub async fn read_utf_with_len( let mut buffer = vec![0; length as usize]; buf.read_exact(&mut buffer) .await - .or_else(|_| Err("Invalid UTF-8".to_string()))?; + .map_err(|_| "Invalid UTF-8".to_string())?; string.push_str(std::str::from_utf8(&buffer).unwrap()); if string.len() > length as usize { diff --git a/minecraft-protocol/src/packets/handshake/client_intention_packet.rs b/minecraft-protocol/src/packets/handshake/client_intention_packet.rs index c6a940e3..f3416f4c 100644 --- a/minecraft-protocol/src/packets/handshake/client_intention_packet.rs +++ b/minecraft-protocol/src/packets/handshake/client_intention_packet.rs @@ -31,7 +31,7 @@ impl PacketTrait for ClientIntentionPacket { } async fn read( - buf: &mut BufReader, + _buf: &mut BufReader, ) -> Result { Err("ClientIntentionPacket::parse not implemented".to_string()) // Ok(ClientIntentionPacket {}.get()) diff --git a/minecraft-protocol/src/packets/mod.rs b/minecraft-protocol/src/packets/mod.rs index 1fac2a3b..bacd0c27 100644 --- a/minecraft-protocol/src/packets/mod.rs +++ b/minecraft-protocol/src/packets/mod.rs @@ -46,9 +46,9 @@ impl Packet { pub fn id(&self) -> u32 { match self { - Packet::ClientIntentionPacket(packet) => 0x00, - Packet::ServerboundStatusRequestPacket(packet) => 0x00, - Packet::ClientboundStatusResponsePacket(packet) => 0x00, + Packet::ClientIntentionPacket(_packet) => 0x00, + Packet::ServerboundStatusRequestPacket(_packet) => 0x00, + Packet::ClientboundStatusResponsePacket(_packet) => 0x00, } } @@ -98,7 +98,7 @@ impl Packet { pub trait PacketTrait { /// Return a version of the packet that you can actually use for stuff fn get(self) -> Packet; - fn write(&self, buf: &mut Vec) -> (); + fn write(&self, buf: &mut Vec); async fn read( buf: &mut BufReader, ) -> Result diff --git a/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs b/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs index 5099952c..20db9fe1 100644 --- a/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs +++ b/minecraft-protocol/src/packets/status/clientbound_status_response_packet.rs @@ -52,7 +52,7 @@ impl PacketTrait for ClientboundStatusResponsePacket { // this.status = GsonHelper.fromJson(GSON, friendlyByteBuf.readUtf(32767), ServerStatus.class); Ok(ClientboundStatusResponsePacket { // version: status_json.get("version"), - description: Component::new(&description_string)?, + description: Component::new(description_string)?, } .get()) } diff --git a/minecraft-protocol/src/packets/status/serverbound_status_request_packet.rs b/minecraft-protocol/src/packets/status/serverbound_status_request_packet.rs index a08ffdff..56799677 100644 --- a/minecraft-protocol/src/packets/status/serverbound_status_request_packet.rs +++ b/minecraft-protocol/src/packets/status/serverbound_status_request_packet.rs @@ -3,7 +3,6 @@ use std::hash::Hash; use tokio::io::BufReader; use crate::{ - mc_buf, packets::{Packet, PacketTrait}, }; @@ -18,7 +17,7 @@ impl PacketTrait for ServerboundStatusRequestPacket { fn write(&self, _buf: &mut Vec) {} async fn read( - buf: &mut BufReader, + _buf: &mut BufReader, ) -> Result { Err("ServerboundStatusRequestPacket::read not implemented".to_string()) } diff --git a/minecraft-protocol/src/resolver.rs b/minecraft-protocol/src/resolver.rs index 5dc6df8b..b751e05f 100644 --- a/minecraft-protocol/src/resolver.rs +++ b/minecraft-protocol/src/resolver.rs @@ -14,7 +14,7 @@ pub async fn resolve_address(address: &ServerAddress) -> Result() { return Ok(ServerIpAddress { - ip: ip, + ip, port: address.port, }); } diff --git a/minecraft-protocol/src/server_status_pinger.rs b/minecraft-protocol/src/server_status_pinger.rs index a86be553..0e12a6a7 100644 --- a/minecraft-protocol/src/server_status_pinger.rs +++ b/minecraft-protocol/src/server_status_pinger.rs @@ -9,7 +9,7 @@ use crate::{ }; pub async fn ping_server(address: &ServerAddress) -> Result<(), String> { - let resolved_address = resolver::resolve_address(&address).await?; + let resolved_address = resolver::resolve_address(address).await?; let mut conn = Connection::new(&resolved_address).await?;