Skip to content

Commit 57c37bb

Browse files
authored
Fix clippy::all lints in tests/examples to unblock 0.7.0 release (#40)
* chore: fix clippy lints for release CI Clean up tests and examples so release CI passes `clippy::all` on current stable and nightly without changing library behavior. Replace unnecessary transmutes with safer casts/conversions, add explicit transmute annotations where still needed, simplify loops and sorting, remove unnecessary mutable references, and keep intentional async `Arc<Ndisapi>` usage documented with an allow. Also fix minor formatting and redundant-reference lints; verified with clippy, tests, fmt, and publish dry-run.
1 parent 767cf4d commit 57c37bb

8 files changed

Lines changed: 28 additions & 25 deletions

File tree

examples/async-packthru.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ async fn update_display(shared_table: Arc<Mutex<HashMap<PacketInfo, u32>>>) {
515515
.iter()
516516
.map(|(pi, &count)| (pi, count))
517517
.collect();
518-
counts.sort_by(|a, b| b.1.cmp(&a.1));
518+
counts.sort_by_key(|c| std::cmp::Reverse(c.1));
519519
let top_entries = &counts[..std::cmp::min(10, counts.len())];
520520

521521
for (packet_info, count) in top_entries {
@@ -668,6 +668,9 @@ async fn main() -> Result<()> {
668668
interface_index -= 1;
669669

670670
// Create a new Ndisapi driver instance.
671+
// The async API takes an `Arc<Ndisapi>`; `Ndisapi` is intentionally not `Send`/`Sync`
672+
// (it owns a raw `HANDLE`), so clippy's `Arc<!Send + !Sync>` suggestion does not apply here.
673+
#[allow(clippy::arc_with_non_send_sync)]
671674
let driver = Arc::new(
672675
Ndisapi::new("NDISRD").expect("WinpkFilter driver is not installed or failed to load!"),
673676
);

examples/async-passthru.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async fn async_loop(adapter: &mut AsyncNdisapiAdapter) -> Result<()> {
4040
Err(err) => println!("Error sending packet to adapter. Error code = {err}"),
4141
};
4242
} else {
43-
match adapter.send_packet_to_mstcp(&mut packet) {
43+
match adapter.send_packet_to_mstcp(&packet) {
4444
Ok(_) => {}
4545
Err(err) => println!("Error sending packet to mstcp. Error code = {err}"),
4646
}
@@ -104,6 +104,9 @@ async fn main() -> Result<()> {
104104
interface_index -= 1;
105105

106106
// Create a new Ndisapi driver instance.
107+
// The async API takes an `Arc<Ndisapi>`; `Ndisapi` is intentionally not `Send`/`Sync`
108+
// (it owns a raw `HANDLE`), so clippy's `Arc<!Send + !Sync>` suggestion does not apply here.
109+
#[allow(clippy::arc_with_non_send_sync)]
107110
let driver = Arc::new(
108111
Ndisapi::new("NDISRD").expect("WinpkFilter driver is not installed or failed to load!"),
109112
);

examples/listadapters.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
/// adapter name conversion functions, and `Ndisapi::get_mtu_decrement` and etc.. It retrieves information about the
33
/// network interfaces, including their indexes, which can be passed to the `packthru` and `passthru`
44
/// examples. The collected information is dumped to the console screen.
5-
use std::{
6-
mem::{self, size_of},
7-
ptr::write_bytes,
8-
};
5+
use std::{mem::size_of, ptr::write_bytes};
96

107
use ndisapi::{IphlpNetworkAdapterInfo, MacAddress, Ndisapi, PacketOidData, RasLinks};
118
use windows::core::Result;
@@ -102,7 +99,7 @@ fn main() -> Result<()> {
10299
// zero initialize the vector allocated memory and then set a vector length to one
103100
unsafe {
104101
write_bytes::<u8>(
105-
mem::transmute(ras_links_vec.as_mut_ptr()),
102+
ras_links_vec.as_mut_ptr().cast::<u8>(),
106103
0,
107104
size_of::<RasLinks>(),
108105
);

examples/packthru.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,35 +122,35 @@ fn main() -> Result<()> {
122122
packets_number = packets_number.saturating_sub(packets_read);
123123

124124
// Process each packet.
125-
for i in 0..packets_read {
126-
let direction_flags = packets[i].get_device_flags();
125+
for (i, packet) in packets.iter().take(packets_read).enumerate() {
126+
let direction_flags = packet.get_device_flags();
127127

128128
if verbose {
129129
// Print packet direction and remaining packets.
130130
if direction_flags == DirectionFlags::PACKET_FLAG_ON_SEND {
131131
println!(
132132
"\nMSTCP --> Interface ({} bytes) remaining packets {}\n",
133-
packets[i].get_length(),
133+
packet.get_length(),
134134
packets_number + (packets_read - i)
135135
);
136136
} else {
137137
println!(
138138
"\nInterface --> MSTCP ({} bytes) remaining packets {}\n",
139-
packets[i].get_length(),
139+
packet.get_length(),
140140
packets_number + (packets_read - i)
141141
);
142142
}
143143
}
144144

145145
if verbose {
146146
// Print packet information
147-
print_packet_info(&packets[i]);
147+
print_packet_info(packet);
148148
}
149149

150150
if direction_flags == DirectionFlags::PACKET_FLAG_ON_SEND {
151-
to_adapter.push(&packets[i])?;
151+
to_adapter.push(packet)?;
152152
} else {
153-
to_mstcp.push(&packets[i])?;
153+
to_mstcp.push(packet)?;
154154
}
155155
}
156156

examples/unsorted-packthru.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,26 +114,26 @@ fn main() -> Result<()> {
114114

115115
if verbose {
116116
// Process each packet.
117-
for i in 0..packets_read {
118-
let direction_flags = packets[i].get_device_flags();
117+
for (i, packet) in packets.iter().take(packets_read).enumerate() {
118+
let direction_flags = packet.get_device_flags();
119119

120120
// Print packet direction and remaining packets.
121121
if direction_flags == DirectionFlags::PACKET_FLAG_ON_SEND {
122122
println!(
123123
"\nMSTCP --> Interface ({} bytes) remaining packets {}\n",
124-
packets[i].get_length(),
124+
packet.get_length(),
125125
packets_number + (packets_read - i)
126126
);
127127
} else {
128128
println!(
129129
"\nInterface --> MSTCP ({} bytes) remaining packets {}\n",
130-
packets[i].get_length(),
130+
packet.get_length(),
131131
packets_number + (packets_read - i)
132132
);
133133
}
134134

135135
// Print packet information
136-
print_packet_info(&packets[i]);
136+
print_packet_info(packet);
137137
}
138138
}
139139

src/ndisapi/static_api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ impl Ndisapi {
312312

313313
let friendly_name_key = format!(
314314
"SYSTEM\\CurrentControlSet\\Control\\Network\\{{4D36E972-E325-11CE-BFC1-08002BE10318}}\\{}\\Connection",
315-
&adapter_name
315+
adapter_name
316316
);
317317

318318
// Convert the string to UTF16 array and get a pointer to it as PCWSTR

src/netlib/ip_helper/network_adapter_info.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ impl IphlpNetworkAdapterInfo {
262262

263263
// create the registry key path for the adapter's connection settings
264264
let friendly_name_key = format!(
265-
"SYSTEM\\CurrentControlSet\\Control\\Network\\{{4D36E972-E325-11CE-BFC1-08002BE10318}}\\{}\\Connection",
266-
&self.adapter_name
267-
);
265+
"SYSTEM\\CurrentControlSet\\Control\\Network\\{{4D36E972-E325-11CE-BFC1-08002BE10318}}\\{}\\Connection",
266+
self.adapter_name
267+
);
268268

269269
// Convert the string to UTF16 array and get a pointer to it as PCWSTR
270270
let mut friendly_name_key = friendly_name_key.encode_utf16().collect::<Vec<u16>>();

src/netlib/ip_helper/sockaddr_storage.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ mod tests {
540540
let sockaddr_in = SOCKADDR_IN {
541541
sin_family: AF_INET,
542542
sin_port: 0,
543-
sin_addr: unsafe { mem::transmute(ipv4) },
543+
sin_addr: ipv4.into(),
544544
sin_zero: [0; 8],
545545
};
546546
let ip_address_info = SockAddrStorage::from_sockaddr_in(sockaddr_in);
@@ -553,7 +553,7 @@ mod tests {
553553
sin6_family: AF_INET6,
554554
sin6_port: 0,
555555
sin6_flowinfo: 0,
556-
sin6_addr: unsafe { mem::transmute(ipv6) },
556+
sin6_addr: ipv6.into(),
557557
Anonymous: SOCKADDR_IN6_0 { sin6_scope_id: 0 },
558558
};
559559
let ip_address_info = SockAddrStorage::from_sockaddr_in6(sockaddr_in6);

0 commit comments

Comments
 (0)