Skip to content

Commit

Permalink
chore: replace allow clippy lints with expect
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiapple852 committed Sep 30, 2024
1 parent f652285 commit 7ac5cbd
Show file tree
Hide file tree
Showing 30 changed files with 63 additions and 70 deletions.
2 changes: 1 addition & 1 deletion crates/trippy-core/src/flows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ mod tests {
assert_eq!(FlowId(2), flow2_id);
}

#[allow(clippy::unnecessary_wraps)]
#[expect(clippy::unnecessary_wraps)]
fn addr(addr: &str) -> Option<IpAddr> {
Some(IpAddr::V4(Ipv4Addr::from_str(addr).unwrap()))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/trippy-core/src/net/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl ErrorMapper {
}

/// Convert a given [`ErrorKind`] to [`Error::ProbeFailed`].
#[allow(clippy::needless_pass_by_value)]
#[expect(clippy::needless_pass_by_value)]
pub fn probe_failed(err: Error, kind: ErrorKind) -> Error {
match err {
Error::IoError(io_err) if io_err.kind() == kind => Error::ProbeFailed(io_err),
Expand Down
5 changes: 1 addition & 4 deletions crates/trippy-core/src/net/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mod address {
/// Linux accepts either network byte order or host byte order for the `total_length` field, and
/// so we skip the check and return network byte order unconditionally.
#[cfg(target_os = "linux")]
#[allow(clippy::unnecessary_wraps)]
#[expect(clippy::unnecessary_wraps)]
pub const fn for_address(_src_addr: IpAddr) -> Result<Ipv4ByteOrder> {
Ok(Ipv4ByteOrder::Network)
}
Expand Down Expand Up @@ -168,7 +168,6 @@ mod socket {
use std::time::Duration;
use tracing::instrument;

#[allow(clippy::unnecessary_wraps)]
#[instrument]
pub fn startup() -> Result<()> {
Ok(())
Expand Down Expand Up @@ -475,12 +474,10 @@ mod socket {
})
.map_err(|err| IoError::Other(err, IoOperation::TakeError))
}
#[allow(clippy::unused_self, clippy::unnecessary_wraps)]
#[instrument(skip(self), ret)]
fn icmp_error_info(&mut self) -> IoResult<IpAddr> {
Ok(IpAddr::V4(Ipv4Addr::UNSPECIFIED))
}
#[allow(clippy::unused_self, clippy::unnecessary_wraps)]
#[instrument(skip(self))]
fn close(&mut self) -> IoResult<()> {
Ok(())
Expand Down
38 changes: 19 additions & 19 deletions crates/trippy-core/src/net/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use windows_sys::Win32::System::IO::OVERLAPPED;
/// returned.
macro_rules! syscall {
($fn: ident ( $($arg: expr),* $(,)* ), $err_fn: expr) => {{
#[allow(unsafe_code)]
#[expect(unsafe_code)]
let res = unsafe { windows_sys::Win32::Networking::WinSock::$fn($($arg, )*) };
if $err_fn(res) {
Err(StdIoError::last_os_error())
Expand All @@ -47,7 +47,7 @@ macro_rules! syscall {
/// The raw result of the syscall is returned.
macro_rules! syscall_ip_helper {
($fn: ident ( $($arg: expr),* $(,)* )) => {{
#[allow(unsafe_code)]
#[expect(unsafe_code)]
unsafe { windows_sys::Win32::NetworkManagement::IpHelper::$fn($($arg, )*) }
}};
}
Expand All @@ -57,15 +57,15 @@ macro_rules! syscall_ip_helper {
/// The raw result of the syscall is returned.
macro_rules! syscall_threading {
($fn: ident ( $($arg: expr),* $(,)* ) ) => {{
#[allow(unsafe_code)]
#[expect(unsafe_code)]
unsafe { windows_sys::Win32::System::Threading::$fn($($arg, )*) }
}};
}

pub struct PlatformImpl;

impl Platform for PlatformImpl {
#[allow(clippy::unnecessary_wraps)]
#[expect(clippy::unnecessary_wraps)]
fn byte_order_for_address(_addr: IpAddr) -> Result<Ipv4ByteOrder> {
Ok(Ipv4ByteOrder::Network)
}
Expand Down Expand Up @@ -99,7 +99,7 @@ pub struct SocketImpl {
bytes_read: u32,
}

#[allow(clippy::cast_possible_wrap, clippy::redundant_closure_call)]
#[expect(clippy::cast_possible_wrap, clippy::redundant_closure_call)]
impl SocketImpl {
fn startup() -> IoResult<()> {
let mut wsa_data = Self::new_wsa_data();
Expand Down Expand Up @@ -255,32 +255,32 @@ impl SocketImpl {
Ok(())
}

#[allow(unsafe_code)]
#[expect(unsafe_code)]
const fn new_wsa_data() -> WSADATA {
// Safety: an all-zero value is valid for WSADATA.
unsafe { zeroed::<WSADATA>() }
}

#[allow(unsafe_code)]
#[expect(unsafe_code)]
const fn new_sockaddr_storage() -> SOCKADDR_STORAGE {
// Safety: an all-zero value is valid for SOCKADDR_STORAGE.
unsafe { zeroed::<SOCKADDR_STORAGE>() }
}

#[allow(unsafe_code)]
#[expect(unsafe_code)]
const fn new_overlapped() -> OVERLAPPED {
// Safety: an all-zero value is valid for OVERLAPPED.
unsafe { zeroed::<OVERLAPPED>() }
}

#[allow(unsafe_code)]
#[expect(unsafe_code)]
const fn new_icmp_error_info() -> ICMP_ERROR_INFO {
// Safety: an all-zero value is valid for ICMP_ERROR_INFO.
unsafe { zeroed::<ICMP_ERROR_INFO>() }
}
}

#[allow(clippy::redundant_closure_call)]
#[expect(clippy::redundant_closure_call)]
impl Drop for SocketImpl {
fn drop(&mut self) {
self.close().unwrap_or_default();
Expand All @@ -290,7 +290,7 @@ impl Drop for SocketImpl {
}
}

#[allow(clippy::cast_possible_wrap, clippy::redundant_closure_call)]
#[expect(clippy::cast_possible_wrap, clippy::redundant_closure_call)]
impl Socket for SocketImpl {
#[instrument]
fn new_icmp_send_socket_ipv4(raw: bool) -> IoResult<Self> {
Expand Down Expand Up @@ -556,7 +556,7 @@ impl Socket for SocketImpl {
}

#[instrument(skip(self), ret)]
#[allow(unsafe_code)]
#[expect(unsafe_code)]
fn icmp_error_info(&mut self) -> IoResult<IpAddr> {
let icmp_error_info = self
.getsockopt::<ICMP_ERROR_INFO>(
Expand Down Expand Up @@ -626,7 +626,7 @@ impl From<ErrorKind> for StdIoError {
/// NOTE under Windows, we cannot use a bind connect/getsockname as "If the socket
/// is using a connectionless protocol, the address may not be available until I/O
/// occurs on the socket." We use `SIO_ROUTING_INTERFACE_QUERY` instead.
#[allow(clippy::cast_sign_loss, clippy::redundant_closure_call)]
#[expect(clippy::cast_sign_loss, clippy::redundant_closure_call)]
#[instrument]
fn routing_interface_query(target: IpAddr) -> Result<IpAddr> {
let src: *mut c_void = [0; 1024].as_mut_ptr().cast();
Expand Down Expand Up @@ -659,7 +659,7 @@ fn routing_interface_query(target: IpAddr) -> Result<IpAddr> {
.map_err(|err| Error::IoError(IoError::Other(err, IoOperation::ConvertSocketAddress)))
}

#[allow(unsafe_code)]
#[expect(unsafe_code)]
fn sockaddrptr_to_ipaddr(sockaddr: *mut SOCKADDR_STORAGE) -> StdIoResult<IpAddr> {
// Safety: TODO
match sockaddr_to_socketaddr(unsafe { sockaddr.as_ref().unwrap() }) {
Expand All @@ -671,7 +671,7 @@ fn sockaddrptr_to_ipaddr(sockaddr: *mut SOCKADDR_STORAGE) -> StdIoResult<IpAddr>
}
}

#[allow(unsafe_code)]
#[expect(unsafe_code)]
fn sockaddr_to_socketaddr(sockaddr: &SOCKADDR_STORAGE) -> StdIoResult<SocketAddr> {
let ptr = sockaddr as *const SOCKADDR_STORAGE;
let af = sockaddr.ss_family;
Expand All @@ -686,7 +686,7 @@ fn sockaddr_to_socketaddr(sockaddr: &SOCKADDR_STORAGE) -> StdIoResult<SocketAddr
port,
)))
} else if af == AF_INET6 {
#[allow(clippy::cast_ptr_alignment)]
#[expect(clippy::cast_ptr_alignment)]
let sockaddr_in6_ptr = ptr.cast::<SOCKADDR_IN6>();
// Safety: TODO
let sockaddr_in6 = unsafe { *sockaddr_in6_ptr };
Expand All @@ -710,8 +710,8 @@ fn sockaddr_to_socketaddr(sockaddr: &SOCKADDR_STORAGE) -> StdIoResult<SocketAddr
}
}

#[allow(unsafe_code)]
#[allow(clippy::cast_possible_wrap)]
#[expect(unsafe_code)]
#[expect(clippy::cast_possible_wrap)]
#[must_use]
fn socketaddr_to_sockaddr(socketaddr: SocketAddr) -> (SOCKADDR_STORAGE, i32) {
#[repr(C)]
Expand Down Expand Up @@ -880,7 +880,7 @@ mod adapter {
None
} else {
// Safety: `next` is not null and points to a valid IP_ADAPTER_ADDRESSES_LH
#[allow(unsafe_code)]
#[expect(unsafe_code)]
unsafe {
let friendly_name = WideCString::from_ptr_str((*self.next).FriendlyName)
.to_string()
Expand Down
2 changes: 1 addition & 1 deletion crates/trippy-core/src/net/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub enum SocketError {
ConnectionRefused,
#[allow(dead_code)]
HostUnreachable,
Other(#[allow(dead_code)] std::io::Error),
Other(#[expect(dead_code)] std::io::Error),
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions crates/trippy-core/src/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub struct Probe {
impl Probe {
/// Create a new probe.
#[must_use]
#[allow(clippy::too_many_arguments)]
#[expect(clippy::too_many_arguments)]
pub(crate) const fn new(
sequence: Sequence,
identifier: TraceId,
Expand Down Expand Up @@ -362,7 +362,7 @@ pub struct ResponseSeqUdp {
}

impl ResponseSeqUdp {
#[allow(clippy::too_many_arguments)]
#[expect(clippy::too_many_arguments)]
pub const fn new(
identifier: u16,
dest_addr: IpAddr,
Expand Down
2 changes: 1 addition & 1 deletion crates/trippy-core/src/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ mod state {
use std::net::{IpAddr, Ipv4Addr};
use std::time::Duration;

#[allow(
#[expect(
clippy::cognitive_complexity,
clippy::too_many_lines,
clippy::bool_assert_comparison
Expand Down
4 changes: 2 additions & 2 deletions crates/trippy-core/src/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Tracer {
/// Create a `Tracer`.
///
/// Use the [`crate::Builder`] type to create a [`Tracer`].
#[allow(clippy::too_many_arguments)]
#[expect(clippy::too_many_arguments)]
#[must_use]
pub(crate) fn new(
interface: Option<String>,
Expand Down Expand Up @@ -469,7 +469,7 @@ mod inner {
}

impl TracerInner {
#[allow(clippy::too_many_arguments)]
#[expect(clippy::too_many_arguments)]
pub(super) fn new(
interface: Option<String>,
source_addr: Option<IpAddr>,
Expand Down
2 changes: 1 addition & 1 deletion crates/trippy-core/tests/sim/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use trippy_packet::IpProtocol;

const READ_TIMEOUT: Duration = Duration::from_millis(10);

#[allow(clippy::too_many_lines)]
#[expect(clippy::too_many_lines)]
pub async fn run(
tun: Arc<Mutex<TunDevice>>,
sim: Arc<Simulation>,
Expand Down
4 changes: 2 additions & 2 deletions crates/trippy-core/tests/sim/tun_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ impl TunDevice {
}

#[cfg(target_os = "linux")]
#[allow(clippy::unnecessary_wraps)]
#[expect(clippy::unnecessary_wraps)]
const fn create_route() -> anyhow::Result<()> {
Ok(())
}

#[cfg(target_os = "windows")]
#[allow(clippy::unnecessary_wraps)]
#[expect(clippy::unnecessary_wraps)]
fn create_route() -> anyhow::Result<()> {
// allow time for the routing table to reflect the tun device.
std::thread::sleep(std::time::Duration::from_millis(10000));
Expand Down
2 changes: 1 addition & 1 deletion crates/trippy-dns/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub struct AsInfo {

impl Display for DnsEntry {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
#[allow(clippy::match_same_arms)]
#[expect(clippy::match_same_arms)]
match self {
Self::Resolved(Resolved::Normal(_, hosts)) => write!(f, "{}", hosts.join(" ")),
Self::Resolved(Resolved::WithAsInfo(_, hosts, asinfo)) => {
Expand Down
4 changes: 2 additions & 2 deletions crates/trippy-packet/src/icmp_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ pub mod extension_splitter {
// object with a single member. The packet does not have a `length`
// field and is therefore rfc4884 non-complaint.
#[test]
#[allow(clippy::cognitive_complexity)]
#[expect(clippy::cognitive_complexity)]
fn test_split_extension_ipv4_time_exceeded_non_compliant_mpls() {
let buf = hex_literal::hex!(
"
Expand Down Expand Up @@ -1191,7 +1191,7 @@ pub mod extension_splitter {
// This example contain an MPLS extension stack which contains
// two member (i.e. labels)
#[test]
#[allow(clippy::cognitive_complexity)]
#[expect(clippy::cognitive_complexity)]
fn test_ipv6() {
let buf = hex_literal::hex!(
"
Expand Down
14 changes: 6 additions & 8 deletions crates/trippy-privilege/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ impl Privilege {
// Unix (excl. Linux)

#[cfg(all(unix, not(target_os = "linux")))]
#[allow(clippy::unnecessary_wraps)]
/// Acquire privileges, if possible.
///
/// This is a no-op on non-Linux unix systems.
Expand All @@ -187,7 +186,7 @@ impl Privilege {
}

#[cfg(all(unix, not(target_os = "linux")))]
#[allow(clippy::unnecessary_wraps)]
#[expect(clippy::unnecessary_wraps)]
/// Do we have the required privileges?
///
/// Checks if the effective user is root.
Expand All @@ -196,7 +195,6 @@ impl Privilege {
}

#[cfg(all(unix, not(target_os = "linux")))]
#[allow(clippy::unnecessary_wraps)]
/// Drop all privileges.
///
/// This is a no-op on non-Linux unix systems.
Expand Down Expand Up @@ -231,7 +229,7 @@ impl Privilege {
// Windows

#[cfg(windows)]
#[allow(clippy::unnecessary_wraps)]
#[expect(clippy::unnecessary_wraps)]
/// Acquire privileges, if possible.
///
/// This is a no-op on `Windows`.
Expand All @@ -240,14 +238,14 @@ impl Privilege {
}

#[cfg(windows)]
#[allow(clippy::unnecessary_wraps)]
#[expect(clippy::unnecessary_wraps)]
/// Do we have the required privileges?
///
/// Check if the current process has an elevated token.
fn check_has_privileges() -> Result<bool> {
macro_rules! syscall {
($p: path, $fn: ident ( $($arg: expr),* $(,)* ) ) => {{
#[allow(unsafe_code)]
#[expect(unsafe_code)]
unsafe { paste::paste!(windows_sys::Win32::$p::$fn) ($($arg, )*) }
}};
}
Expand Down Expand Up @@ -279,7 +277,7 @@ impl Privilege {
use windows_sys::Win32::Security::TokenElevation;
use windows_sys::Win32::Security::TOKEN_ELEVATION;
let mut elevation = TOKEN_ELEVATION { TokenIsElevated: 0 };
#[allow(clippy::cast_possible_truncation)]
#[expect(clippy::cast_possible_truncation)]
let size = std::mem::size_of::<TOKEN_ELEVATION>() as u32;
let mut ret_size = 0u32;
let ret = syscall!(
Expand Down Expand Up @@ -311,7 +309,7 @@ impl Privilege {
}

#[cfg(windows)]
#[allow(clippy::unnecessary_wraps)]
#[expect(clippy::unnecessary_wraps)]
/// Drop all capabilities.
///
/// This is a no-op on `Windows`.
Expand Down
Loading

0 comments on commit 7ac5cbd

Please sign in to comment.