From a09b1c8ac643d448db479a108ac6726307075453 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Sat, 18 Sep 2021 21:07:00 -0600 Subject: Clippy cleanup And this time, start running Clippy in CI --- test/sys/test_aio.rs | 14 +++++++------- test/sys/test_epoll.rs | 5 ++--- test/sys/test_inotify.rs | 6 ++---- test/sys/test_socket.rs | 44 ++++++++++++++++---------------------------- test/test_dir.rs | 1 + test/test_fcntl.rs | 11 ++++++----- test/test_kmod/mod.rs | 11 +++++------ test/test_mount.rs | 5 ++--- test/test_pty.rs | 2 ++ test/test_stat.rs | 1 + test/test_unistd.rs | 6 +++--- 11 files changed, 47 insertions(+), 59 deletions(-) (limited to 'test') diff --git a/test/sys/test_aio.rs b/test/sys/test_aio.rs index 3208410e..b4eb3129 100644 --- a/test/sys/test_aio.rs +++ b/test/sys/test_aio.rs @@ -1,5 +1,5 @@ use libc::{c_int, c_void}; -use nix::{Error, Result}; +use nix::Result; use nix::errno::*; use nix::sys::aio::*; use nix::sys::signal::{SaFlags, SigAction, sigaction, SigevNotify, SigHandler, Signal, SigSet}; @@ -16,7 +16,7 @@ use tempfile::tempfile; fn poll_aio(aiocb: &mut Pin>) -> Result<()> { loop { let err = aiocb.error(); - if err != Err(Error::from(Errno::EINPROGRESS)) { return err; }; + if err != Err(Errno::EINPROGRESS) { return err; }; thread::sleep(time::Duration::from_millis(10)); } } @@ -26,7 +26,7 @@ fn poll_aio(aiocb: &mut Pin>) -> Result<()> { fn poll_lio(liocb: &mut LioCb, i: usize) -> Result<()> { loop { let err = liocb.error(i); - if err != Err(Error::from(Errno::EINPROGRESS)) { return err; }; + if err != Err(Errno::EINPROGRESS) { return err; }; thread::sleep(time::Duration::from_millis(10)); } } @@ -70,7 +70,7 @@ fn test_cancel() { LioOpcode::LIO_NOP); aiocb.write().unwrap(); let err = aiocb.error(); - assert!(err == Ok(()) || err == Err(Error::from(Errno::EINPROGRESS))); + assert!(err == Ok(()) || err == Err(Errno::EINPROGRESS)); let cancelstat = aiocb.cancel(); assert!(cancelstat.is_ok()); @@ -95,7 +95,7 @@ fn test_aio_cancel_all() { LioOpcode::LIO_NOP); aiocb.write().unwrap(); let err = aiocb.error(); - assert!(err == Ok(()) || err == Err(Error::from(Errno::EINPROGRESS))); + assert!(err == Ok(()) || err == Err(Errno::EINPROGRESS)); let cancelstat = aio_cancel_all(f.as_raw_fd()); assert!(cancelstat.is_ok()); @@ -182,8 +182,8 @@ fn test_aio_suspend() { Ok(_) => () }; } - if rcb.error() != Err(Error::from(Errno::EINPROGRESS)) && - wcb.error() != Err(Error::from(Errno::EINPROGRESS)) { + if rcb.error() != Err(Errno::EINPROGRESS) && + wcb.error() != Err(Errno::EINPROGRESS) { break } } diff --git a/test/sys/test_epoll.rs b/test/sys/test_epoll.rs index 57bc4844..8d44cd08 100644 --- a/test/sys/test_epoll.rs +++ b/test/sys/test_epoll.rs @@ -1,6 +1,5 @@ use nix::sys::epoll::{EpollCreateFlags, EpollFlags, EpollOp, EpollEvent}; use nix::sys::epoll::{epoll_create1, epoll_ctl}; -use nix::Error; use nix::errno::Errno; #[test] @@ -8,11 +7,11 @@ pub fn test_epoll_errno() { let efd = epoll_create1(EpollCreateFlags::empty()).unwrap(); let result = epoll_ctl(efd, EpollOp::EpollCtlDel, 1, None); assert!(result.is_err()); - assert_eq!(result.unwrap_err(), Error::from(Errno::ENOENT)); + assert_eq!(result.unwrap_err(), Errno::ENOENT); let result = epoll_ctl(efd, EpollOp::EpollCtlAdd, 1, None); assert!(result.is_err()); - assert_eq!(result.unwrap_err(), Error::from(Errno::EINVAL)); + assert_eq!(result.unwrap_err(), Errno::EINVAL); } #[test] diff --git a/test/sys/test_inotify.rs b/test/sys/test_inotify.rs index 121b726c..137816a3 100644 --- a/test/sys/test_inotify.rs +++ b/test/sys/test_inotify.rs @@ -1,7 +1,5 @@ use nix::sys::inotify::{AddWatchFlags,InitFlags,Inotify}; -use nix::Error; use nix::errno::Errno; -use tempfile; use std::ffi::OsString; use std::fs::{rename, File}; @@ -14,7 +12,7 @@ pub fn test_inotify() { instance.add_watch(tempdir.path(), AddWatchFlags::IN_ALL_EVENTS).unwrap(); let events = instance.read_events(); - assert_eq!(events.unwrap_err(), Error::from(Errno::EAGAIN)); + assert_eq!(events.unwrap_err(), Errno::EAGAIN); File::create(tempdir.path().join("test")).unwrap(); @@ -31,7 +29,7 @@ pub fn test_inotify_multi_events() { instance.add_watch(tempdir.path(), AddWatchFlags::IN_ALL_EVENTS).unwrap(); let events = instance.read_events(); - assert_eq!(events.unwrap_err(), Error::from(Errno::EAGAIN)); + assert_eq!(events.unwrap_err(), Errno::EAGAIN); File::create(tempdir.path().join("test")).unwrap(); rename(tempdir.path().join("test"), tempdir.path().join("test2")).unwrap(); diff --git a/test/sys/test_socket.rs b/test/sys/test_socket.rs index 6b998cb6..0f6fac66 100644 --- a/test/sys/test_socket.rs +++ b/test/sys/test_socket.rs @@ -152,7 +152,7 @@ pub fn test_abstract_sun_path_too_long() { pub fn test_addr_equality_abstract() { let name = String::from("nix\0abstract\0test"); let addr1 = UnixAddr::new_abstract(name.as_bytes()).unwrap(); - let mut addr2 = addr1.clone(); + let mut addr2 = addr1; assert_eq!(addr1, addr2); assert_eq!(calculate_hash(&addr1), calculate_hash(&addr2)); @@ -367,7 +367,6 @@ mod recvfrom { target_os = "freebsd", target_os = "netbsd", ))] - #[allow(clippy::vec_init_then_push)] #[test] pub fn udp_sendmmsg() { use nix::sys::uio::IoVec; @@ -394,14 +393,14 @@ mod recvfrom { let from = sendrecv(rsock, ssock, move |s, m, flags| { let iov = [IoVec::from_slice(m)]; - let mut msgs = Vec::new(); - msgs.push( + let mut msgs = vec![ SendMmsgData { iov: &iov, cmsgs: &[], addr: Some(sock_addr), _lt: Default::default(), - }); + } + ]; let batch_size = 15; @@ -640,7 +639,6 @@ pub fn test_scm_rights() { #[cfg_attr(qemu, ignore)] #[test] pub fn test_af_alg_cipher() { - use libc; use nix::sys::uio::IoVec; use nix::unistd::read; use nix::sys::socket::{socket, sendmsg, bind, accept, setsockopt, @@ -837,7 +835,7 @@ pub fn test_sendmsg_ipv4packetinfo() { if let InetAddr::V4(sin) = inet_addr { cfg_if! { if #[cfg(target_os = "netbsd")] { - drop(sin); + let _dontcare = sin; let pi = libc::in_pktinfo { ipi_ifindex: 0, /* Unspecified interface */ ipi_addr: libc::in_addr { s_addr: 0 }, @@ -925,7 +923,6 @@ fn test_scm_rights_single_cmsg_multiple_fds() { use nix::sys::socket::{ControlMessage, ControlMessageOwned, MsgFlags, sendmsg, recvmsg}; use nix::sys::uio::IoVec; - use libc; let (send, receive) = UnixDatagram::pair().unwrap(); let thread = thread::spawn(move || { @@ -1068,8 +1065,6 @@ fn test_scm_credentials() { #[cfg_attr(qemu, ignore)] #[test] fn test_scm_credentials_and_rights() { - use libc; - let space = cmsg_space!(libc::ucred, RawFd); test_impl_scm_credentials_and_rights(space); } @@ -1278,7 +1273,6 @@ fn loopback_address(family: AddressFamily) -> Option { - let i = if_nametoindex(lo_name.as_bytes()).expect("if_nametoindex"); - assert_eq!( - pktinfo.ipi_ifindex as libc::c_uint, - i, - "unexpected ifindex (expected {}, got {})", - i, - pktinfo.ipi_ifindex - ); - } - _ => (), + if let Some(ControlMessageOwned::Ipv4PacketInfo(pktinfo)) = cmsgs.next() { + let i = if_nametoindex(lo_name.as_bytes()).expect("if_nametoindex"); + assert_eq!( + pktinfo.ipi_ifindex as libc::c_uint, + i, + "unexpected ifindex (expected {}, got {})", + i, + pktinfo.ipi_ifindex + ); } assert!(cmsgs.next().is_none(), "unexpected additional control msg"); assert_eq!(msg.bytes, 8); @@ -1371,7 +1362,6 @@ pub fn test_recv_ipv4pktinfo() { ), ignore)] #[test] pub fn test_recvif() { - use libc; use nix::net::if_::*; use nix::sys::socket::sockopt::{Ipv4RecvIf, Ipv4RecvDstAddr}; use nix::sys::socket::{bind, SockFlag, SockType}; @@ -1485,7 +1475,6 @@ pub fn test_recvif() { ), ignore)] #[test] pub fn test_recv_ipv6pktinfo() { - use libc; use nix::net::if_::*; use nix::sys::socket::sockopt::Ipv6RecvPacketInfo; use nix::sys::socket::{bind, SockFlag, SockType}; @@ -1562,7 +1551,6 @@ pub fn test_recv_ipv6pktinfo() { #[cfg_attr(graviton, ignore = "Not supported by the CI environment")] #[test] pub fn test_vsock() { - use libc; use nix::errno::Errno; use nix::sys::socket::{AddressFamily, socket, bind, connect, listen, SockAddr, SockType, SockFlag}; @@ -1838,7 +1826,7 @@ mod linux_errqueue { } else { panic!("Expected some error origin"); } - return *ext_err + *ext_err } else { panic!("Unexpected control message {:?}", cmsg); } @@ -1884,7 +1872,7 @@ mod linux_errqueue { } else { panic!("Expected some error origin"); } - return *ext_err + *ext_err } else { panic!("Unexpected control message {:?}", cmsg); } diff --git a/test/test_dir.rs b/test/test_dir.rs index 0dc7308e..2940b6ea 100644 --- a/test/test_dir.rs +++ b/test/test_dir.rs @@ -17,6 +17,7 @@ fn flags() -> OFlag { } #[test] +#[allow(clippy::unnecessary_sort_by)] // False positive fn read() { let tmp = tempdir().unwrap(); File::create(&tmp.path().join("foo")).unwrap(); diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index c19a1b0a..76252e6e 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -127,14 +127,14 @@ fn test_renameat2_exchange() { let old_path = old_dir.path().join("old"); { let mut old_f = File::create(&old_path).unwrap(); - old_f.write(b"old").unwrap(); + old_f.write_all(b"old").unwrap(); } let new_dir = tempfile::tempdir().unwrap(); let new_dirfd = open(new_dir.path(), OFlag::empty(), Mode::empty()).unwrap(); let new_path = new_dir.path().join("new"); { let mut new_f = File::create(&new_path).unwrap(); - new_f.write(b"new").unwrap(); + new_f.write_all(b"new").unwrap(); } renameat2( Some(old_dirfd), @@ -319,9 +319,10 @@ mod linux_android { let buf1 = b"abcdef"; let buf2 = b"defghi"; - let mut iovecs = Vec::with_capacity(2); - iovecs.push(IoVec::from_slice(&buf1[0..3])); - iovecs.push(IoVec::from_slice(&buf2[0..3])); + let iovecs = vec![ + IoVec::from_slice(&buf1[0..3]), + IoVec::from_slice(&buf2[0..3]) + ]; let res = vmsplice(wr, &iovecs[..], SpliceFFlags::empty()).unwrap(); diff --git a/test/test_kmod/mod.rs b/test/test_kmod/mod.rs index e53e7a9e..0f7fc48e 100644 --- a/test/test_kmod/mod.rs +++ b/test/test_kmod/mod.rs @@ -34,7 +34,6 @@ fn compile_kernel_module() -> (PathBuf, String, TempDir) { use nix::errno::Errno; use nix::kmod::{delete_module, DeleteModuleFlags}; use nix::kmod::{finit_module, init_module, ModuleInitFlags}; -use nix::Error; use std::ffi::CString; use std::fs::File; use std::io::Read; @@ -90,7 +89,7 @@ fn test_init_and_delete_module() { let mut contents: Vec = Vec::new(); f.read_to_end(&mut contents) .expect("unable to read kernel module content to buffer"); - init_module(&mut contents, &CString::new("").unwrap()).expect("unable to load kernel module"); + init_module(&contents, &CString::new("").unwrap()).expect("unable to load kernel module"); delete_module( &CString::new(kmod_name).unwrap(), @@ -110,7 +109,7 @@ fn test_init_and_delete_module_with_params() { let mut contents: Vec = Vec::new(); f.read_to_end(&mut contents) .expect("unable to read kernel module content to buffer"); - init_module(&mut contents, &CString::new("who=Nix number=2015").unwrap()) + init_module(&contents, &CString::new("who=Nix number=2015").unwrap()) .expect("unable to load kernel module"); delete_module( @@ -130,7 +129,7 @@ fn test_finit_module_invalid() { let f = File::open(kmod_path).expect("unable to open kernel module"); let result = finit_module(&f, &CString::new("").unwrap(), ModuleInitFlags::empty()); - assert_eq!(result.unwrap_err(), Error::from(Errno::EINVAL)); + assert_eq!(result.unwrap_err(), Errno::EINVAL); } #[test] @@ -147,7 +146,7 @@ fn test_finit_module_twice_and_delete_module() { let result = finit_module(&f, &CString::new("").unwrap(), ModuleInitFlags::empty()); - assert_eq!(result.unwrap_err(), Error::from(Errno::EEXIST)); + assert_eq!(result.unwrap_err(), Errno::EEXIST); delete_module( &CString::new(kmod_name).unwrap(), @@ -163,5 +162,5 @@ fn test_delete_module_not_loaded() { let result = delete_module(&CString::new("hello").unwrap(), DeleteModuleFlags::empty()); - assert_eq!(result.unwrap_err(), Error::from(Errno::ENOENT)); + assert_eq!(result.unwrap_err(), Errno::ENOENT); } diff --git a/test/test_mount.rs b/test/test_mount.rs index c1b6c8a3..44287f97 100644 --- a/test/test_mount.rs +++ b/test/test_mount.rs @@ -21,14 +21,13 @@ mod test_mount { use nix::sys::stat::{self, Mode}; use nix::unistd::getuid; - use tempfile; - - static SCRIPT_CONTENTS: &'static [u8] = b"#!/bin/sh + static SCRIPT_CONTENTS: &[u8] = b"#!/bin/sh exit 23"; const EXPECTED_STATUS: i32 = 23; const NONE: Option<&'static [u8]> = None; + #[allow(clippy::bind_instead_of_map)] // False positive pub fn test_mount_tmpfs_without_flags_allows_rwx() { let tempdir = tempfile::tempdir().unwrap(); diff --git a/test/test_pty.rs b/test/test_pty.rs index 52b63342..57874de3 100644 --- a/test/test_pty.rs +++ b/test/test_pty.rs @@ -114,6 +114,8 @@ fn open_ptty_pair() -> (PtyMaster, File) { let slave_fd = open(Path::new(&slave_name), OFlag::O_RDWR, stat::Mode::empty()).unwrap(); #[cfg(target_os = "illumos")] + // TODO: rewrite using ioctl! + #[allow(clippy::comparison_chain)] { use libc::{ioctl, I_FIND, I_PUSH}; diff --git a/test/test_stat.rs b/test/test_stat.rs index 922c25fd..33cf748d 100644 --- a/test/test_stat.rs +++ b/test/test_stat.rs @@ -59,6 +59,7 @@ fn assert_stat_results(stat_result: Result) { #[cfg(not(any(target_os = "netbsd", target_os = "redox")))] // (Android's st_blocks is ulonglong which is always non-negative.) #[cfg_attr(target_os = "android", allow(unused_comparisons))] +#[allow(clippy::absurd_extreme_comparisons)] // Not absurd on all OSes fn assert_lstat_results(stat_result: Result) { let stats = stat_result.expect("stat call failed"); assert!(stats.st_dev > 0); // must be positive integer, exact number machine dependent diff --git a/test/test_unistd.rs b/test/test_unistd.rs index cb23ba75..3a3d49dd 100644 --- a/test/test_unistd.rs +++ b/test/test_unistd.rs @@ -10,7 +10,7 @@ use nix::sys::stat::{self, Mode, SFlag}; #[cfg(not(any(target_os = "redox", target_os = "fuchsia")))] use nix::pty::{posix_openpt, grantpt, unlockpt, ptsname}; use nix::errno::Errno; -use std::{env, iter}; +use std::env; #[cfg(not(any(target_os = "fuchsia", target_os = "redox")))] use std::ffi::CString; #[cfg(not(target_os = "redox"))] @@ -443,7 +443,7 @@ fn test_getcwd() { // 4096 on linux, 1024 on macos) let mut inner_tmp_dir = tmpdir_path; for _ in 0..5 { - let newdir = iter::repeat("a").take(100).collect::(); + let newdir = "a".repeat(100); inner_tmp_dir.push(newdir); assert!(mkdir(inner_tmp_dir.as_path(), Mode::S_IRWXU).is_ok()); } @@ -1051,7 +1051,7 @@ fn test_setfsuid() { dbg!(&temp_path); let temp_path_2 = (&temp_path).to_path_buf(); let mut permissions = fs::metadata(&temp_path).unwrap().permissions(); - permissions.set_mode(640); + permissions.set_mode(0o640); // spawn a new thread where to test setfsuid thread::spawn(move || { -- cgit v1.2.3