From a6ee63ac3212e62518c97252581cfa48e2ad056e Mon Sep 17 00:00:00 2001 From: Michael Baikov Date: Thu, 4 Aug 2022 07:54:45 +0800 Subject: fix clippy assertions_on_result_states https://rust-lang.github.io/rust-clippy/master/index.html#assertions_on_result_states --- test/sys/test_aio.rs | 14 +++++++------- test/sys/test_epoll.rs | 4 ++-- test/sys/test_socket.rs | 2 +- test/sys/test_termios.rs | 2 +- test/sys/test_uio.rs | 35 ++++++++++------------------------- test/sys/test_wait.rs | 24 ++++++++++++------------ test/test_fcntl.rs | 6 ++---- test/test_net.rs | 2 +- test/test_stat.rs | 4 ++-- test/test_time.rs | 15 ++++++++------- test/test_unistd.rs | 30 +++++++++++++++++------------- 11 files changed, 63 insertions(+), 75 deletions(-) (limited to 'test') diff --git a/test/sys/test_aio.rs b/test/sys/test_aio.rs index b4ea6757..12749b1d 100644 --- a/test/sys/test_aio.rs +++ b/test/sys/test_aio.rs @@ -85,7 +85,7 @@ mod aio_fsync { SigevNotify::SigevNone, )); let err = aiof.as_mut().submit(); - assert!(err.is_err()); + err.expect_err("assertion failed"); } #[test] @@ -102,7 +102,7 @@ mod aio_fsync { SigevNotify::SigevNone, )); let err = aiof.as_mut().submit(); - assert!(err.is_ok()); + err.expect("assert failed"); poll_aio!(&mut aiof).unwrap(); aiof.as_mut().aio_return().unwrap(); } @@ -149,7 +149,7 @@ mod aio_read { aior.as_mut().submit().unwrap(); let cancelstat = aior.as_mut().cancel(); - assert!(cancelstat.is_ok()); + cancelstat.expect("assert failed"); // Wait for aiow to complete, but don't care whether it succeeded let _ = poll_aio!(&mut aior); @@ -174,7 +174,7 @@ mod aio_read { 0, //priority SigevNotify::SigevNone, )); - assert!(aior.as_mut().submit().is_err()); + aior.as_mut().submit().expect_err("assertion failed"); } // Test a simple aio operation with no completion notification. We must @@ -342,7 +342,7 @@ mod aio_write { assert!(err == Ok(()) || err == Err(Errno::EINPROGRESS)); let cancelstat = aiow.as_mut().cancel(); - assert!(cancelstat.is_ok()); + cancelstat.expect("assert failed"); // Wait for aiow to complete, but don't care whether it succeeded let _ = poll_aio!(&mut aiow); @@ -426,7 +426,7 @@ mod aio_write { 0, //priority SigevNotify::SigevNone, )); - assert!(aiow.as_mut().submit().is_err()); + aiow.as_mut().submit().expect_err("assertion failed"); // Dropping the AioWrite at this point should not panic } } @@ -565,7 +565,7 @@ fn test_aio_cancel_all() { assert!(err == Ok(()) || err == Err(Errno::EINPROGRESS)); let cancelstat = aio_cancel_all(f.as_raw_fd()); - assert!(cancelstat.is_ok()); + cancelstat.expect("assert failed"); // Wait for aiocb to complete, but don't care whether it succeeded let _ = poll_aio!(&mut aiocb); diff --git a/test/sys/test_epoll.rs b/test/sys/test_epoll.rs index fafbd749..91569159 100644 --- a/test/sys/test_epoll.rs +++ b/test/sys/test_epoll.rs @@ -6,11 +6,11 @@ use nix::sys::epoll::{EpollCreateFlags, EpollEvent, EpollFlags, EpollOp}; 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()); + result.expect_err("assertion failed"); assert_eq!(result.unwrap_err(), Errno::ENOENT); let result = epoll_ctl(efd, EpollOp::EpollCtlAdd, 1, None); - assert!(result.is_err()); + result.expect_err("assertion failed"); assert_eq!(result.unwrap_err(), Errno::EINVAL); } diff --git a/test/sys/test_socket.rs b/test/sys/test_socket.rs index 0a74b5e1..06d5b761 100644 --- a/test/sys/test_socket.rs +++ b/test/sys/test_socket.rs @@ -204,7 +204,7 @@ pub fn test_addr_equality_path() { pub fn test_abstract_sun_path_too_long() { let name = String::from("nix\0abstract\0tesnix\0abstract\0tesnix\0abstract\0tesnix\0abstract\0tesnix\0abstract\0testttttnix\0abstract\0test\0make\0sure\0this\0is\0long\0enough"); let addr = UnixAddr::new_abstract(name.as_bytes()); - assert!(addr.is_err()); + addr.expect_err("assertion failed"); } #[cfg(any(target_os = "android", target_os = "linux"))] diff --git a/test/sys/test_termios.rs b/test/sys/test_termios.rs index e567a52c..11a08cb5 100644 --- a/test/sys/test_termios.rs +++ b/test/sys/test_termios.rs @@ -22,7 +22,7 @@ fn test_tcgetattr_pty() { let _m = crate::PTSNAME_MTX.lock(); let pty = openpty(None, None).expect("openpty failed"); - assert!(termios::tcgetattr(pty.slave).is_ok()); + termios::tcgetattr(pty.slave).expect("assert failed"); close(pty.master).expect("closing the master failed"); close(pty.slave).expect("closing the slave failed"); } diff --git a/test/sys/test_uio.rs b/test/sys/test_uio.rs index f46b1940..0f4b8a65 100644 --- a/test/sys/test_uio.rs +++ b/test/sys/test_uio.rs @@ -41,29 +41,22 @@ fn test_writev() { consumed += slice_len; } let pipe_res = pipe(); - assert!(pipe_res.is_ok()); - let (reader, writer) = pipe_res.ok().unwrap(); + let (reader, writer) = pipe_res.expect("Couldn't create pipe"); // FileDesc will close its filedesc (reader). let mut read_buf: Vec = iter::repeat(0u8).take(128 * 16).collect(); // Blocking io, should write all data. let write_res = writev(writer, &iovecs); - // Successful write - assert!(write_res.is_ok()); - let written = write_res.ok().unwrap(); + let written = write_res.expect("couldn't write"); // Check whether we written all data assert_eq!(to_write.len(), written); let read_res = read(reader, &mut read_buf[..]); - // Successful read - assert!(read_res.is_ok()); - let read = read_res.ok().unwrap() as usize; + let read = read_res.expect("couldn't read"); // Check we have read as much as we written assert_eq!(read, written); // Check equality of written and read data assert_eq!(&to_write, &read_buf); - let close_res = close(writer); - assert!(close_res.is_ok()); - let close_res = close(reader); - assert!(close_res.is_ok()); + close(writer).expect("closed writer"); + close(reader).expect("closed reader"); } #[test] @@ -92,16 +85,10 @@ fn test_readv() { for v in &mut storage { iovecs.push(IoSliceMut::new(&mut v[..])); } - let pipe_res = pipe(); - assert!(pipe_res.is_ok()); - let (reader, writer) = pipe_res.ok().unwrap(); + let (reader, writer) = pipe().expect("couldn't create pipe"); // Blocking io, should write all data. - let write_res = write(writer, &to_write); - // Successful write - assert!(write_res.is_ok()); - let read_res = readv(reader, &mut iovecs[..]); - assert!(read_res.is_ok()); - let read = read_res.ok().unwrap(); + write(writer, &to_write).expect("write failed"); + let read = readv(reader, &mut iovecs[..]).expect("read failed"); // Check whether we've read all data assert_eq!(to_write.len(), read); // Cccumulate data from iovecs @@ -113,10 +100,8 @@ fn test_readv() { assert_eq!(read_buf.len(), to_write.len()); // Check equality of written and read data assert_eq!(&read_buf, &to_write); - let close_res = close(reader); - assert!(close_res.is_ok()); - let close_res = close(writer); - assert!(close_res.is_ok()); + close(reader).expect("couldn't close reader"); + close(writer).expect("couldn't close writer"); } #[test] diff --git a/test/sys/test_wait.rs b/test/sys/test_wait.rs index 1a4a0f87..d472f1ec 100644 --- a/test/sys/test_wait.rs +++ b/test/sys/test_wait.rs @@ -167,17 +167,17 @@ mod ptrace { Ok(WaitStatus::Stopped(child, SIGTRAP)) ); // We want to test a syscall stop and a PTRACE_EVENT stop - assert!(ptrace::setoptions( + ptrace::setoptions( child, - Options::PTRACE_O_TRACESYSGOOD | Options::PTRACE_O_TRACEEXIT + Options::PTRACE_O_TRACESYSGOOD | Options::PTRACE_O_TRACEEXIT, ) - .is_ok()); + .expect("setoptions failed"); // First, stop on the next system call, which will be exit() - assert!(ptrace::syscall(child, None).is_ok()); + ptrace::syscall(child, None).expect("syscall failed"); assert_eq!(waitpid(child, None), Ok(WaitStatus::PtraceSyscall(child))); // Then get the ptrace event for the process exiting - assert!(ptrace::cont(child, None).is_ok()); + ptrace::cont(child, None).expect("cont failed"); assert_eq!( waitpid(child, None), Ok(WaitStatus::PtraceEvent( @@ -187,7 +187,7 @@ mod ptrace { )) ); // Finally get the normal wait() result, now that the process has exited - assert!(ptrace::cont(child, None).is_ok()); + ptrace::cont(child, None).expect("cont failed"); assert_eq!(waitpid(child, None), Ok(WaitStatus::Exited(child, 0))); } @@ -202,20 +202,20 @@ mod ptrace { Ok(WaitStatus::PtraceEvent(child, SIGTRAP, 0)), ); // We want to test a syscall stop and a PTRACE_EVENT stop - assert!(ptrace::setoptions( + ptrace::setoptions( child, - Options::PTRACE_O_TRACESYSGOOD | Options::PTRACE_O_TRACEEXIT + Options::PTRACE_O_TRACESYSGOOD | Options::PTRACE_O_TRACEEXIT, ) - .is_ok()); + .expect("setopts failed"); // First, stop on the next system call, which will be exit() - assert!(ptrace::syscall(child, None).is_ok()); + ptrace::syscall(child, None).expect("syscall failed"); assert_eq!( waitid(Id::Pid(child), WaitPidFlag::WEXITED), Ok(WaitStatus::PtraceSyscall(child)), ); // Then get the ptrace event for the process exiting - assert!(ptrace::cont(child, None).is_ok()); + ptrace::cont(child, None).expect("cont failed"); assert_eq!( waitid(Id::Pid(child), WaitPidFlag::WEXITED), Ok(WaitStatus::PtraceEvent( @@ -225,7 +225,7 @@ mod ptrace { )), ); // Finally get the normal wait() result, now that the process has exited - assert!(ptrace::cont(child, None).is_ok()); + ptrace::cont(child, None).expect("cont failed"); assert_eq!( waitid(Id::Pid(child), WaitPidFlag::WEXITED), Ok(WaitStatus::Exited(child, 0)), diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index d4a12718..f4adee21 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -492,10 +492,8 @@ mod test_posix_fadvise { fn test_success() { let tmp = NamedTempFile::new().unwrap(); let fd = tmp.as_raw_fd(); - let res = - posix_fadvise(fd, 0, 100, PosixFadviseAdvice::POSIX_FADV_WILLNEED); - - assert!(res.is_ok()); + posix_fadvise(fd, 0, 100, PosixFadviseAdvice::POSIX_FADV_WILLNEED) + .expect("posix_fadvise failed"); } #[test] diff --git a/test/test_net.rs b/test/test_net.rs index d1050c16..c44655a4 100644 --- a/test/test_net.rs +++ b/test/test_net.rs @@ -15,5 +15,5 @@ const LOOPBACK: &[u8] = b"loop"; #[test] fn test_if_nametoindex() { - assert!(if_nametoindex(LOOPBACK).is_ok()); + if_nametoindex(LOOPBACK).expect("assertion failed"); } diff --git a/test/test_stat.rs b/test/test_stat.rs index de5a964e..1888c649 100644 --- a/test/test_stat.rs +++ b/test/test_stat.rs @@ -323,7 +323,7 @@ fn test_mkdirat_success_path() { let dirfd = fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty()) .unwrap(); - assert!((mkdirat(dirfd, filename, Mode::S_IRWXU)).is_ok()); + mkdirat(dirfd, filename, Mode::S_IRWXU).expect("mkdirat failed"); assert!(Path::exists(&tempdir.path().join(filename))); } @@ -337,7 +337,7 @@ fn test_mkdirat_success_mode() { let dirfd = fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty()) .unwrap(); - assert!((mkdirat(dirfd, filename, Mode::S_IRWXU)).is_ok()); + mkdirat(dirfd, filename, Mode::S_IRWXU).expect("mkdirat failed"); let permissions = fs::metadata(tempdir.path().join(filename)) .unwrap() .permissions(); diff --git a/test/test_time.rs b/test/test_time.rs index dc307e57..3e8af653 100644 --- a/test/test_time.rs +++ b/test/test_time.rs @@ -11,12 +11,12 @@ use nix::time::{clock_gettime, ClockId}; #[cfg(not(target_os = "redox"))] #[test] pub fn test_clock_getres() { - assert!(nix::time::clock_getres(ClockId::CLOCK_REALTIME).is_ok()); + nix::time::clock_getres(ClockId::CLOCK_REALTIME).expect("assertion failed"); } #[test] pub fn test_clock_gettime() { - assert!(clock_gettime(ClockId::CLOCK_REALTIME).is_ok()); + clock_gettime(ClockId::CLOCK_REALTIME).expect("assertion failed"); } #[cfg(any( @@ -29,18 +29,18 @@ pub fn test_clock_gettime() { #[test] pub fn test_clock_getcpuclockid() { let clock_id = clock_getcpuclockid(nix::unistd::Pid::this()).unwrap(); - assert!(clock_gettime(clock_id).is_ok()); + clock_gettime(clock_id).expect("assert failed"); } #[cfg(not(target_os = "redox"))] #[test] pub fn test_clock_id_res() { - assert!(ClockId::CLOCK_REALTIME.res().is_ok()); + ClockId::CLOCK_REALTIME.res().expect("assert failed"); } #[test] pub fn test_clock_id_now() { - assert!(ClockId::CLOCK_REALTIME.now().is_ok()); + ClockId::CLOCK_REALTIME.now().expect("assert failed"); } #[cfg(any( @@ -52,7 +52,8 @@ pub fn test_clock_id_now() { ))] #[test] pub fn test_clock_id_pid_cpu_clock_id() { - assert!(ClockId::pid_cpu_clock_id(nix::unistd::Pid::this()) + ClockId::pid_cpu_clock_id(nix::unistd::Pid::this()) .map(ClockId::now) - .is_ok()); + .expect("assert failed") + .expect("assert failed"); } diff --git a/test/test_unistd.rs b/test/test_unistd.rs index 38d31a3f..337ebe46 100644 --- a/test/test_unistd.rs +++ b/test/test_unistd.rs @@ -101,7 +101,7 @@ fn test_mkstemp() { #[test] fn test_mkstemp_directory() { // mkstemp should fail if a directory is given - assert!(mkstemp(&env::temp_dir()).is_err()); + mkstemp(&env::temp_dir()).expect_err("assertion failed"); } #[test] @@ -121,7 +121,7 @@ fn test_mkfifo() { #[cfg(not(target_os = "redox"))] fn test_mkfifo_directory() { // mkfifo should fail if a directory is given - assert!(mkfifo(&env::temp_dir(), Mode::S_IRUSR).is_err()); + mkfifo(&env::temp_dir(), Mode::S_IRUSR).expect_err("assertion failed"); } #[test] @@ -180,7 +180,8 @@ fn test_mkfifoat_directory_none() { let _m = crate::CWD_LOCK.read(); // mkfifoat should fail if a directory is given - assert!(mkfifoat(None, &env::temp_dir(), Mode::S_IRUSR).is_err()); + mkfifoat(None, &env::temp_dir(), Mode::S_IRUSR) + .expect_err("assertion failed"); } #[test] @@ -198,7 +199,8 @@ fn test_mkfifoat_directory() { let mkfifoat_dir = "mkfifoat_dir"; stat::mkdirat(dirfd, mkfifoat_dir, Mode::S_IRUSR).unwrap(); - assert!(mkfifoat(Some(dirfd), mkfifoat_dir, Mode::S_IRUSR).is_err()); + mkfifoat(Some(dirfd), mkfifoat_dir, Mode::S_IRUSR) + .expect_err("assertion failed"); } #[test] @@ -456,10 +458,10 @@ fn test_fchdir() { let tmpdir_path = tmpdir.path().canonicalize().unwrap(); let tmpdir_fd = File::open(&tmpdir_path).unwrap().into_raw_fd(); - assert!(fchdir(tmpdir_fd).is_ok()); + fchdir(tmpdir_fd).expect("assertion failed"); assert_eq!(getcwd().unwrap(), tmpdir_path); - assert!(close(tmpdir_fd).is_ok()); + close(tmpdir_fd).expect("assertion failed"); } #[test] @@ -469,7 +471,7 @@ fn test_getcwd() { let tmpdir = tempdir().unwrap(); let tmpdir_path = tmpdir.path().canonicalize().unwrap(); - assert!(chdir(&tmpdir_path).is_ok()); + chdir(&tmpdir_path).expect("assertion failed"); assert_eq!(getcwd().unwrap(), tmpdir_path); // make path 500 chars longer so that buffer doubling in getcwd @@ -480,9 +482,10 @@ fn test_getcwd() { for _ in 0..5 { let newdir = "a".repeat(100); inner_tmp_dir.push(newdir); - assert!(mkdir(inner_tmp_dir.as_path(), Mode::S_IRWXU).is_ok()); + mkdir(inner_tmp_dir.as_path(), Mode::S_IRWXU) + .expect("assertion failed"); } - assert!(chdir(inner_tmp_dir.as_path()).is_ok()); + chdir(inner_tmp_dir.as_path()).expect("assertion failed"); assert_eq!(getcwd().unwrap(), inner_tmp_dir.as_path()); } @@ -1166,7 +1169,8 @@ fn test_access_file_exists() { let tempdir = tempdir().unwrap(); let path = tempdir.path().join("does_exist.txt"); let _file = File::create(path.clone()).unwrap(); - assert!(access(&path, AccessFlags::R_OK | AccessFlags::W_OK).is_ok()); + access(&path, AccessFlags::R_OK | AccessFlags::W_OK) + .expect("assertion failed"); } //Clippy false positive https://github.com/rust-lang/rust-clippy/issues/9111 @@ -1210,8 +1214,8 @@ fn test_setfsuid() { let fuid = setfsuid(nobody.uid); // trying to open the temporary file should fail with EACCES let res = fs::File::open(&temp_path); - assert!(res.is_err()); - assert_eq!(res.err().unwrap().kind(), io::ErrorKind::PermissionDenied); + let err = res.expect_err("assertion failed"); + assert_eq!(err.kind(), io::ErrorKind::PermissionDenied); // assert fuid actually changes let prev_fuid = setfsuid(Uid::from_raw(-1i32 as u32)); @@ -1302,5 +1306,5 @@ fn test_getpeereid() { ))] fn test_getpeereid_invalid_fd() { // getpeereid is not POSIX, so error codes are inconsistent between different Unices. - assert!(getpeereid(-1).is_err()); + getpeereid(-1).expect_err("assertion failed"); } -- cgit v1.2.3