From 6e7bddd1540565a9da7f7e56ddf5851d1786a3dd Mon Sep 17 00:00:00 2001 From: Ryan Zoeller Date: Sat, 8 Oct 2022 13:48:59 -0500 Subject: Fix clippy warnings on nightly Clippy is now smarter about detecting unnecessary casts and useless conversions, which means we need to be more explicit about when the conversions are needed for a subset of platforms. Required changes found by repeatedly running the following command against a list of the supported platforms. `xargs -t -I {} sh -c "cargo clippy -Zbuild-std --target {} --all-targets -- -D warnings || exit 255"` I removed the casts it complained about, and then restored them with an `#[allow]` if a later target needed the cast. --- src/dir.rs | 2 ++ src/errno.rs | 2 +- src/sys/pthread.rs | 1 + src/sys/socket/addr.rs | 32 +++++++++++++++++--------------- src/sys/socket/mod.rs | 12 +++++++++--- src/sys/socket/sockopt.rs | 2 +- src/sys/statfs.rs | 4 ++++ src/sys/sysinfo.rs | 4 ++++ src/sys/termios.rs | 4 ++++ src/sys/time.rs | 8 ++++++++ src/sys/uio.rs | 2 +- test/sys/test_ioctl.rs | 8 ++++++++ test/sys/test_stat.rs | 2 ++ test/test_mount.rs | 4 ++-- 14 files changed, 64 insertions(+), 23 deletions(-) diff --git a/src/dir.rs b/src/dir.rs index cbcd1ea7..e36a8aa6 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -208,6 +208,8 @@ pub enum Type { impl Entry { /// Returns the inode number (`d_ino`) of the underlying `dirent`. #[allow(clippy::useless_conversion)] // Not useless on all OSes + // The cast is not unnecessary on all platforms. + #[allow(clippy::unnecessary_cast)] pub fn ino(&self) -> u64 { cfg_if! { if #[cfg(any(target_os = "android", diff --git a/src/errno.rs b/src/errno.rs index 912fb94f..0c5b0eda 100644 --- a/src/errno.rs +++ b/src/errno.rs @@ -47,7 +47,7 @@ fn clear() { /// Returns the platform-specific value of errno pub fn errno() -> i32 { - unsafe { (*errno_location()) as i32 } + unsafe { *errno_location() } } impl Errno { diff --git a/src/sys/pthread.rs b/src/sys/pthread.rs index fd81f3ed..6bad03a4 100644 --- a/src/sys/pthread.rs +++ b/src/sys/pthread.rs @@ -28,6 +28,7 @@ feature! { /// won't send any signal. /// /// [`pthread_kill(3)`]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_kill.html +#[allow(clippy::not_unsafe_ptr_arg_deref)] #[cfg(not(target_os = "redox"))] pub fn pthread_kill(thread: Pthread, signal: T) -> Result<()> where T: Into> diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index 6b2ba21c..07d68e1f 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -935,7 +935,7 @@ impl SockaddrLike for UnixAddr { return None; } } - if (*addr).sa_family as i32 != libc::AF_UNIX as i32 { + if (*addr).sa_family as i32 != libc::AF_UNIX { return None; } let mut su: libc::sockaddr_un = mem::zeroed(); @@ -1192,7 +1192,7 @@ impl SockaddrLike for SockaddrIn { return None; } } - if (*addr).sa_family as i32 != libc::AF_INET as i32 { + if (*addr).sa_family as i32 != libc::AF_INET { return None; } Some(Self(ptr::read_unaligned(addr as *const _))) @@ -1298,7 +1298,7 @@ impl SockaddrLike for SockaddrIn6 { return None; } } - if (*addr).sa_family as i32 != libc::AF_INET6 as i32 { + if (*addr).sa_family as i32 != libc::AF_INET6 { return None; } Some(Self(ptr::read_unaligned(addr as *const _))) @@ -2101,7 +2101,7 @@ pub mod netlink { return None; } } - if (*addr).sa_family as i32 != libc::AF_NETLINK as i32 { + if (*addr).sa_family as i32 != libc::AF_NETLINK { return None; } Some(Self(ptr::read_unaligned(addr as *const _))) @@ -2145,7 +2145,7 @@ pub mod alg { return None; } } - if (*addr).sa_family as i32 != libc::AF_ALG as i32 { + if (*addr).sa_family as i32 != libc::AF_ALG { return None; } Some(Self(ptr::read_unaligned(addr as *const _))) @@ -2259,7 +2259,7 @@ pub mod sys_control { return None; } } - if (*addr).sa_family as i32 != libc::AF_SYSTEM as i32 { + if (*addr).sa_family as i32 != libc::AF_SYSTEM { return None; } Some(Self(ptr::read_unaligned(addr as *const _))) @@ -2366,12 +2366,12 @@ mod datalink { // Returns an Option just for cross-platform compatibility pub fn addr(&self) -> Option<[u8; 6]> { Some([ - self.0.sll_addr[0] as u8, - self.0.sll_addr[1] as u8, - self.0.sll_addr[2] as u8, - self.0.sll_addr[3] as u8, - self.0.sll_addr[4] as u8, - self.0.sll_addr[5] as u8, + self.0.sll_addr[0], + self.0.sll_addr[1], + self.0.sll_addr[2], + self.0.sll_addr[3], + self.0.sll_addr[4], + self.0.sll_addr[5], ]) } } @@ -2402,7 +2402,7 @@ mod datalink { return None; } } - if (*addr).sa_family as i32 != libc::AF_PACKET as i32 { + if (*addr).sa_family as i32 != libc::AF_PACKET { return None; } Some(Self(ptr::read_unaligned(addr as *const _))) @@ -2477,6 +2477,8 @@ mod datalink { } /// Physical-layer address (MAC) + // The cast is not unnecessary on all platforms. + #[allow(clippy::unnecessary_cast)] pub fn addr(&self) -> Option<[u8; 6]> { let nlen = self.nlen(); let data = self.0.sdl_data; @@ -2522,7 +2524,7 @@ mod datalink { return None; } } - if (*addr).sa_family as i32 != libc::AF_LINK as i32 { + if (*addr).sa_family as i32 != libc::AF_LINK { return None; } Some(Self(ptr::read_unaligned(addr as *const _))) @@ -2566,7 +2568,7 @@ pub mod vsock { return None; } } - if (*addr).sa_family as i32 != libc::AF_VSOCK as i32 { + if (*addr).sa_family as i32 != libc::AF_VSOCK { return None; } Some(Self(ptr::read_unaligned(addr as *const _))) diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index 66774205..461607d1 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -839,6 +839,8 @@ impl ControlMessageOwned { unsafe fn decode_from(header: &cmsghdr) -> ControlMessageOwned { let p = CMSG_DATA(header); + // The cast is not unnecessary on all platforms. + #[allow(clippy::unnecessary_cast)] let len = header as *const _ as usize + header.cmsg_len as usize - p as usize; match (header.cmsg_level, header.cmsg_type) { @@ -1643,7 +1645,7 @@ pub fn recvmmsg<'a, I, S>( } ); - (msg_controllen as usize, &mut d.cmsg_buffer) + (msg_controllen, &mut d.cmsg_buffer) }).collect(); let timeout = if let Some(mut t) = timeout { @@ -1662,6 +1664,8 @@ pub fn recvmmsg<'a, I, S>( .zip(addresses.iter().map(|addr| unsafe{addr.assume_init()})) .zip(results.into_iter()) .map(|((mmsghdr, address), (msg_controllen, cmsg_buffer))| { + // The cast is not unnecessary on all platforms. + #[allow(clippy::unnecessary_cast)] unsafe { read_mhdr( mmsghdr.msg_hdr, @@ -1684,6 +1688,8 @@ unsafe fn read_mhdr<'a, 'b, S>( ) -> RecvMsg<'b, S> where S: SockaddrLike { + // The cast is not unnecessary on all platforms. + #[allow(clippy::unnecessary_cast)] let cmsghdr = { if mhdr.msg_controllen > 0 { // got control message(s) @@ -2131,7 +2137,7 @@ pub fn sockaddr_storage_to_addr( match c_int::from(addr.ss_family) { #[cfg(feature = "net")] libc::AF_INET => { - assert!(len as usize >= mem::size_of::()); + assert!(len >= mem::size_of::()); let sin = unsafe { *(addr as *const sockaddr_storage as *const sockaddr_in) }; @@ -2139,7 +2145,7 @@ pub fn sockaddr_storage_to_addr( } #[cfg(feature = "net")] libc::AF_INET6 => { - assert!(len as usize >= mem::size_of::()); + assert!(len >= mem::size_of::()); let sin6 = unsafe { *(addr as *const _ as *const sockaddr_in6) }; diff --git a/src/sys/socket/sockopt.rs b/src/sys/socket/sockopt.rs index b3828b31..90111ec5 100644 --- a/src/sys/socket/sockopt.rs +++ b/src/sys/socket/sockopt.rs @@ -863,7 +863,7 @@ struct SetU8 { impl<'a> Set<'a, u8> for SetU8 { fn new(val: &'a u8) -> SetU8 { - SetU8 { val: *val as u8 } + SetU8 { val: *val } } fn ffi_ptr(&self) -> *const c_void { diff --git a/src/sys/statfs.rs b/src/sys/statfs.rs index 8d2b283b..a1c1aaa9 100644 --- a/src/sys/statfs.rs +++ b/src/sys/statfs.rs @@ -705,6 +705,8 @@ mod test { assert_fs_equals(fs, vfs); } + // The cast is not unnecessary on all platforms. + #[allow(clippy::unnecessary_cast)] fn assert_fs_equals(fs: Statfs, vfs: Statvfs) { assert_eq!(fs.files() as u64, vfs.files() as u64); assert_eq!(fs.blocks() as u64, vfs.blocks() as u64); @@ -752,6 +754,8 @@ mod test { assert_fs_equals_strict(fs.unwrap(), vfs.unwrap()) } + // The cast is not unnecessary on all platforms. + #[allow(clippy::unnecessary_cast)] fn assert_fs_equals_strict(fs: Statfs, vfs: Statvfs) { assert_eq!(fs.files_free() as u64, vfs.files_free() as u64); assert_eq!(fs.blocks_free() as u64, vfs.blocks_free() as u64); diff --git a/src/sys/sysinfo.rs b/src/sys/sysinfo.rs index 96f04330..e8aa00b0 100644 --- a/src/sys/sysinfo.rs +++ b/src/sys/sysinfo.rs @@ -30,6 +30,8 @@ impl SysInfo { } /// Returns the time since system boot. + // The cast is not unnecessary on all platforms. + #[allow(clippy::unnecessary_cast)] pub fn uptime(&self) -> Duration { // Truncate negative values to 0 Duration::from_secs(cmp::max(self.0.uptime, 0) as u64) @@ -64,6 +66,8 @@ impl SysInfo { self.scale_mem(self.0.freeram) } + // The cast is not unnecessary on all platforms. + #[allow(clippy::unnecessary_cast)] fn scale_mem(&self, units: mem_blocks_t) -> u64 { units as u64 * self.0.mem_unit as u64 } diff --git a/src/sys/termios.rs b/src/sys/termios.rs index b4bb3d85..4ff4e674 100644 --- a/src/sys/termios.rs +++ b/src/sys/termios.rs @@ -933,6 +933,8 @@ cfg_if!{ /// [cfgetispeed(3p)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/cfgetispeed.html)). /// /// `cfgetispeed()` extracts the input baud rate from the given `Termios` structure. + // The cast is not unnecessary on all platforms. + #[allow(clippy::unnecessary_cast)] pub fn cfgetispeed(termios: &Termios) -> u32 { let inner_termios = termios.get_libc_termios(); unsafe { libc::cfgetispeed(&*inner_termios) as u32 } @@ -942,6 +944,8 @@ cfg_if!{ /// [cfgetospeed(3p)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/cfgetospeed.html)). /// /// `cfgetospeed()` extracts the output baud rate from the given `Termios` structure. + // The cast is not unnecessary on all platforms. + #[allow(clippy::unnecessary_cast)] pub fn cfgetospeed(termios: &Termios) -> u32 { let inner_termios = termios.get_libc_termios(); unsafe { libc::cfgetospeed(&*inner_termios) as u32 } diff --git a/src/sys/time.rs b/src/sys/time.rs index da65d516..a7240849 100644 --- a/src/sys/time.rs +++ b/src/sys/time.rs @@ -306,6 +306,8 @@ impl TimeValLike for TimeSpec { }) } + // The cast is not unnecessary on all platforms. + #[allow(clippy::unnecessary_cast)] fn num_seconds(&self) -> i64 { if self.tv_sec() < 0 && self.tv_nsec() > 0 { (self.tv_sec() + 1) as i64 @@ -322,6 +324,8 @@ impl TimeValLike for TimeSpec { self.num_nanoseconds() / 1_000 } + // The cast is not unnecessary on all platforms. + #[allow(clippy::unnecessary_cast)] fn num_nanoseconds(&self) -> i64 { let secs = self.num_seconds() * 1_000_000_000; let nsec = self.nanos_mod_sec(); @@ -549,6 +553,8 @@ impl TimeValLike for TimeVal { }) } + // The cast is not unnecessary on all platforms. + #[allow(clippy::unnecessary_cast)] fn num_seconds(&self) -> i64 { if self.tv_sec() < 0 && self.tv_usec() > 0 { (self.tv_sec() + 1) as i64 @@ -561,6 +567,8 @@ impl TimeValLike for TimeVal { self.num_microseconds() / 1_000 } + // The cast is not unnecessary on all platforms. + #[allow(clippy::unnecessary_cast)] fn num_microseconds(&self) -> i64 { let secs = self.num_seconds() * 1_000_000; let usec = self.micros_mod_sec(); diff --git a/src/sys/uio.rs b/src/sys/uio.rs index 1908973b..7cddb372 100644 --- a/src/sys/uio.rs +++ b/src/sys/uio.rs @@ -153,7 +153,7 @@ impl IoVec { unsafe { slice::from_raw_parts( self.0.iov_base as *const u8, - self.0.iov_len as usize) + self.0.iov_len) } } } diff --git a/test/sys/test_ioctl.rs b/test/sys/test_ioctl.rs index 7a603c5b..40f60cfd 100644 --- a/test/sys/test_ioctl.rs +++ b/test/sys/test_ioctl.rs @@ -30,6 +30,8 @@ ioctl_readwrite_buf!(readwritebuf_test, 0, 0, u32); #[cfg(any(target_os = "linux", target_os = "android"))] mod linux { + // The cast is not unnecessary on all platforms. + #[allow(clippy::unnecessary_cast)] #[test] fn test_op_none() { if cfg!(any( @@ -46,6 +48,8 @@ mod linux { } } + // The cast is not unnecessary on all platforms. + #[allow(clippy::unnecessary_cast)] #[test] fn test_op_write() { if cfg!(any( @@ -78,6 +82,8 @@ mod linux { } } + // The cast is not unnecessary on all platforms. + #[allow(clippy::unnecessary_cast)] #[test] fn test_op_read() { if cfg!(any( @@ -110,6 +116,8 @@ mod linux { } } + // The cast is not unnecessary on all platforms. + #[allow(clippy::unnecessary_cast)] #[test] fn test_op_read_write() { assert_eq!(request_code_readwrite!(b'z', 10, 1) as u32, 0xC001_7A0A); diff --git a/test/sys/test_stat.rs b/test/sys/test_stat.rs index 2f26e789..426b4b65 100644 --- a/test/sys/test_stat.rs +++ b/test/sys/test_stat.rs @@ -1,3 +1,5 @@ +// The conversion is not useless on all platforms. +#[allow(clippy::useless_conversion)] #[cfg(target_os = "freebsd")] #[test] fn test_chflags() { diff --git a/test/test_mount.rs b/test/test_mount.rs index febcadfb..2fd612e3 100644 --- a/test/test_mount.rs +++ b/test/test_mount.rs @@ -108,7 +108,7 @@ exit 23"; // EROFS: Read-only file system assert_eq!( - EROFS as i32, + EROFS, File::create(tempdir.path().join("test")) .unwrap_err() .raw_os_error() @@ -156,7 +156,7 @@ exit 23"; // EACCES: Permission denied assert_eq!( - EACCES as i32, + EACCES, Command::new(&test_path) .status() .unwrap_err() -- cgit v1.2.3