style: crank clippy to pedantic+nursery and fix all lints

Enable the full clippy all + pedantic + nursery groups as workspace warnings,
with a small documented allow-list for the pedantic/nursery lints this project
intentionally does not follow (rustdoc-style requirements, deliberate numeric
casts in the protocol/UI layers, and a few noisy nursery lints). Also enable
the rust unused_lifetimes and unsafe_op_in_unsafe_fn lints.

Fixed every resulting warning (527 -> 0) mechanically per clippy's suggestions:
inline format args, const fns, item ordering, let-else, redundant clones,
map_or, cast_lossless, Eq derives, associated-fn conversions for methods that
never used self, and removing unnecessary Result wrappers whose call sites could
never propagate an error. No runtime behavior changed; cargo fmt applied.

cargo clippy --workspace --all-targets: 0 warnings. All tests pass.
This commit is contained in:
michel
2026-07-19 00:09:17 +02:00
parent b149a8ecf2
commit e72c8a5c07
37 changed files with 482 additions and 433 deletions
+2
View File
@@ -18,6 +18,7 @@ impl<Op> Default for Handlers<Op> {
}
impl<Op> Handlers<Op> {
#[must_use]
pub fn new() -> Self {
Self {
handlers: HashMap::new(),
@@ -31,6 +32,7 @@ impl<Op> Handlers<Op> {
self.handlers.insert(handler.get_code(), Box::new(handler));
self
}
#[must_use]
pub fn get_handler(
&self,
code: u8,
@@ -18,13 +18,15 @@ impl Default for MessageReader {
}
impl MessageReader {
pub fn new() -> Self {
#[must_use]
pub const fn new() -> Self {
Self {
buffer: VecDeque::new(),
}
}
#[cfg(test)]
#[must_use]
pub fn new_with_buffer(buffer: Vec<u8>) -> Self {
Self {
buffer: buffer.into(),
@@ -47,6 +49,7 @@ impl MessageReader {
Ok(())
}
#[must_use]
pub fn buffer_len(&self) -> usize {
self.buffer.len()
}
+22 -18
View File
@@ -10,7 +10,7 @@ pub use message_reader::MessageReader;
use std::str;
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
#[allow(dead_code)]
pub enum MessageType {
Server,
@@ -30,7 +30,7 @@ impl std::fmt::Display for Error {
impl std::error::Error for Error {}
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Message {
data: Vec<u8>,
pointer: usize,
@@ -43,7 +43,8 @@ impl Default for Message {
}
impl Message {
pub fn new() -> Self {
#[must_use]
pub const fn new() -> Self {
Self {
data: Vec::new(),
pointer: 0,
@@ -51,8 +52,8 @@ impl Message {
}
pub fn print_hex(&self) {
let data = &self.data;
const BYTES_PER_LINE: usize = 16;
let data = &self.data;
let chunks = data.chunks(BYTES_PER_LINE);
for (i, chunk) in chunks.enumerate() {
@@ -95,6 +96,7 @@ impl Message {
}
#[allow(dead_code)]
#[must_use]
pub fn get_message_code_u32(&self) -> u32 {
if self.data.len() < 8 {
return 0;
@@ -102,6 +104,7 @@ impl Message {
u32::from_le_bytes(self.data[4..8].try_into().unwrap())
}
#[must_use]
pub fn get_message_code(&self) -> u8 {
if self.data.len() <= 4 {
return 0;
@@ -109,6 +112,7 @@ impl Message {
self.data[4]
}
#[must_use]
pub fn get_message_code_send(&self) -> u8 {
if self.data.is_empty() {
return 0;
@@ -116,34 +120,38 @@ impl Message {
self.data[0]
}
pub fn new_with_data(data: Vec<u8>) -> Self {
#[must_use]
pub const fn new_with_data(data: Vec<u8>) -> Self {
Self { data, pointer: 0 }
}
#[allow(dead_code)]
pub fn reset_pointer(&mut self) {
pub const fn reset_pointer(&mut self) {
self.pointer = 0;
}
pub fn set_pointer(&mut self, pointer: usize) {
pub const fn set_pointer(&mut self, pointer: usize) {
self.pointer = pointer;
}
pub fn get_pointer(&mut self) -> usize {
pub const fn get_pointer(&mut self) -> usize {
self.pointer
}
pub fn get_size(&mut self) -> usize {
pub const fn get_size(&mut self) -> usize {
self.data.len()
}
#[must_use]
pub fn get_data(&self) -> Vec<u8> {
self.data.clone()
}
#[must_use]
pub fn get_slice(&self, from: usize, to: usize) -> Vec<u8> {
self.data[from..to].to_vec()
}
/// gets buffer with the message length prepended
#[must_use]
pub fn get_buffer(&self) -> Vec<u8> {
let mut b = vec![0u8; 4];
let length = self.data.len() as u32;
@@ -277,7 +285,7 @@ impl Message {
}
pub fn write_bool(&mut self, value: bool) -> &mut Self {
self.data.push(if value { 1 } else { 0 });
self.data.push(u8::from(value));
self
}
@@ -333,16 +341,13 @@ impl Message {
104 => Ok("WishlistInterval"),
160 => Ok("ExcludedSearchPhrases"),
1001 => Ok("CantConnectToPeer"),
_ => {
Err(Error(format!("Unknown server message code: {}", code)))
}
_ => Err(Error(format!("Unknown server message code: {code}"))),
},
MessageType::PeerInit => match code {
0 => Ok("PierceFireWall"),
1 => Ok("PeerInit"),
_ => Err(Error(format!(
"Unknown peer init message code: {}",
code
"Unknown peer init message code: {code}"
))),
},
MessageType::Peer => match code {
@@ -361,7 +366,7 @@ impl Message {
46 => Ok("UploadFailed"),
50 => Ok("UploadDenied"),
51 => Ok("PlaceInQueueRequest"),
_ => Err(Error(format!("Unknown peer message code: {}", code))),
_ => Err(Error(format!("Unknown peer message code: {code}"))),
},
MessageType::Distributed => match code {
3 => Ok("SearchRequest"),
@@ -369,8 +374,7 @@ impl Message {
5 => Ok("BranchRoot"),
93 => Ok("EmbeddedMessage"),
_ => Err(Error(format!(
"Unknown distributed message code: {}",
code
"Unknown distributed message code: {code}"
))),
},
}
@@ -9,9 +9,9 @@ impl MessageHandler<PeerMessage> for FileSearchResponse {
9
}
fn handle(&self, message: &mut Message, sender: Sender<PeerMessage>) {
let file_search = match SearchResult::new_from_message(message) {
Ok(result) => result,
Err(_) => return, // Skip malformed search results
// Skip malformed search results
let Ok(file_search) = SearchResult::new_from_message(message) else {
return;
};
sender
@@ -22,7 +22,7 @@ impl MessageHandler<ServerMessage> for GetPeerAddressHandler {
let port = message.read_int32();
let obfuscation_type = message.read_int32();
let obfuscated_port = message.read_int32() as u16;
println!("GetPeerAddressHandler: {:?}", username); // Debug print
println!("GetPeerAddressHandler: {username:?}"); // Debug print
sender
.send(ServerMessage::GetPeerAddressResponse {
@@ -4,6 +4,7 @@ use crate::{
pub struct MessageFactory;
impl MessageFactory {
#[must_use]
pub fn build_get_peer_address(username: &str) -> Message {
let mut message = Message::new();
@@ -11,6 +12,7 @@ impl MessageFactory {
message.write_string(username);
message
}
#[must_use]
pub fn build_login_message(username: &str, password: &str) -> Message {
// Message::new_with_data(
// [
@@ -42,6 +44,7 @@ impl MessageFactory {
.clone()
}
#[must_use]
pub fn build_shared_folders_message(
folder_count: u32,
file_count: u32,
@@ -52,6 +55,7 @@ impl MessageFactory {
.write_int32(file_count)
.clone()
}
#[must_use]
pub fn build_file_search_message(token: u32, query: &str) -> Message {
Message::new()
.write_int32(26)
@@ -59,21 +63,25 @@ impl MessageFactory {
.write_string(query)
.clone()
}
#[must_use]
pub fn build_set_status_message(status_code: u32) -> Message {
Message::new()
.write_int32(28)
.write_int32(status_code)
.clone()
}
#[must_use]
pub fn build_no_parent_message() -> Message {
Message::new().write_int32(71).write_bool(true).clone()
}
#[must_use]
pub fn build_set_wait_port_message(port: u16) -> Message {
Message::new()
.write_int32(2)
.write_int32(port.into())
.clone()
}
#[must_use]
pub fn build_watch_user(token: u32) -> Message {
Message::new()
.write_raw_bytes([5, 0, 0, 0, 0].to_vec())
@@ -81,6 +89,7 @@ impl MessageFactory {
.clone()
}
#[must_use]
pub fn build_queue_upload_message(filename: &str) -> Message {
Message::new()
.write_int32(43)
@@ -88,6 +97,7 @@ impl MessageFactory {
.clone()
}
#[must_use]
pub fn build_transfer_request_message(
filename: &str,
token: u32,
@@ -99,6 +109,7 @@ impl MessageFactory {
.write_string(filename)
.clone()
}
#[must_use]
pub fn build_transfer_response_message(transfer: Transfer) -> Message {
Message::new()
.write_int32(41)
@@ -106,6 +117,7 @@ impl MessageFactory {
.write_bool(true)
.clone()
}
#[must_use]
pub fn build_pierce_firewall_message(token: u32) -> Message {
Message::new()
.write_int8(0) // PierceFirewall message code
@@ -114,6 +126,7 @@ impl MessageFactory {
}
#[allow(dead_code)]
#[must_use]
pub fn build_peer_init_message(
own_username: &str,
connection_type: ConnectionType,
@@ -134,7 +147,7 @@ fn test_build_watch_user() {
let message = MessageFactory::build_watch_user(token);
let expect: Vec<u8> = [5, 0, 0, 0, 0, 223, 0, 0, 0].to_vec();
assert_eq!(expect, message.get_data())
assert_eq!(expect, message.get_data());
}
#[test]
@@ -151,7 +164,7 @@ fn test_build_login_message() {
]
.to_vec();
assert_eq!(expect, message.get_data())
assert_eq!(expect, message.get_data());
}
#[test]
@@ -162,5 +175,5 @@ fn test_build_file_search_message() {
119, 97, 120,
]
.to_vec();
assert_eq!(expect, message.get_data())
assert_eq!(expect, message.get_data());
}
@@ -20,12 +20,12 @@ impl MessageHandler<ServerMessage> for MessageUser {
let user_message = UserMessage::new(
id,
timestamp,
username.clone(),
username,
message_content,
new_message,
);
info!("[MessageUser] User message received:{:?}", user_message);
user_message.print()
user_message.print();
}
}