From 3480351b4ba5490a8a436db2338358e69382a7ce Mon Sep 17 00:00:00 2001 From: "bors[bot]" <26634292+bors[bot]@users.noreply.github.com> Date: Sun, 6 Aug 2023 19:34:43 +0000 Subject: [PATCH] Merge #2086 2086: Clippy cleanup r=asomers a=asomers Clippy found some const-correctness issues in internal APIs, but nothing user-facing. Co-authored-by: Alan Somers --- .cirrus.yml | 5 ++++- Cargo.toml | 4 +++- src/dir.rs | 3 +++ src/sys/socket/mod.rs | 22 +++++++++++----------- src/sys/uio.rs | 6 ++++++ 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/.cirrus.yml b/.cirrus.yml index 1e073d707e..4f97ec5822 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -5,6 +5,7 @@ cargo_cache: env: # Build by default; don't just check BUILD: build + CLIPPYFLAGS: -D warnings -A unknown-lints RUSTFLAGS: -D warnings RUSTDOCFLAGS: -D warnings TOOL: cargo @@ -20,7 +21,7 @@ build: &BUILD - rustc +$TOOLCHAIN -Vv - $TOOL +$TOOLCHAIN $BUILD $ZFLAGS --target $TARGET --all-targets - $TOOL +$TOOLCHAIN doc $ZFLAGS --no-deps --target $TARGET - - $TOOL +$TOOLCHAIN clippy $ZFLAGS --target $TARGET --all-targets -- -D warnings + - $TOOL +$TOOLCHAIN clippy $ZFLAGS --target $TARGET --all-targets -- $CLIPPYFLAGS - if [ -z "$NOHACK" ]; then mkdir -p $HOME/.cargo/bin; export PATH=$HOME/.cargo/bin:$PATH; fi - if [ -z "$NOHACK" ]; then curl -LsSf https://github.com/taiki-e/cargo-hack/releases/latest/download/cargo-hack-${HOST:-$TARGET}.tar.gz | tar xzf - -C ~/.cargo/bin; fi - if [ -z "$NOHACK" ]; then $TOOL +$TOOLCHAIN hack $ZFLAGS check --target $TARGET --each-feature; fi @@ -259,6 +260,7 @@ task: TARGET: x86_64-unknown-redox # Redox's MSRV policy is unclear. Until they define it, use nightly. TOOLCHAIN: nightly + CLIPPYFLAGS: -D warnings setup_script: - rustup target add $TARGET - rustup toolchain install $TOOLCHAIN --profile minimal --target $TARGET @@ -275,6 +277,7 @@ task: HOST: x86_64-unknown-linux-gnu TOOLCHAIN: nightly ZFLAGS: -Zbuild-std + CLIPPYFLAGS: -D warnings matrix: - name: DragonFly BSD x86_64 env: diff --git a/Cargo.toml b/Cargo.toml index acb03363bc..a51ccbe254 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -82,7 +82,9 @@ assert-impl = "0.1" lazy_static = "1.4" parking_lot = "0.12" rand = "0.8" -tempfile = "3.3.0" +# tempfile 3.7.0 doesn't build on Haiku +# https://github.com/Stebalien/tempfile/issues/246 +tempfile = ">=3.3.0, < 3.7.0" semver = "1.0.7" [target.'cfg(any(target_os = "android", target_os = "linux"))'.dev-dependencies] diff --git a/src/dir.rs b/src/dir.rs index 5ce503644e..ad10ce16e7 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -101,6 +101,9 @@ impl Drop for Dir { } } +// The pass by mut is technically needless only because the inner NonNull is +// Copy. But philosophically we're mutating the Dir, so we pass by mut. +#[allow(clippy::needless_pass_by_ref_mut)] fn next(dir: &mut Dir) -> Option> { unsafe { // Note: POSIX specifies that portable applications should dynamically allocate a diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index 7908cd96c1..a8f2c034c5 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -1617,18 +1617,18 @@ impl MultiHeaders { // we'll need a cmsg_buffer for each slice, we preallocate a vector and split // it into "slices" parts - let cmsg_buffers = + let mut cmsg_buffers = cmsg_buffer.map(|v| vec![0u8; v.capacity() * num_slices].into_boxed_slice()); let items = addresses .iter_mut() .enumerate() .map(|(ix, address)| { - let (ptr, cap) = match &cmsg_buffers { - Some(v) => ((&v[ix * msg_controllen] as *const u8), msg_controllen), - None => (std::ptr::null(), 0), + let (ptr, cap) = match &mut cmsg_buffers { + Some(v) => ((&mut v[ix * msg_controllen] as *mut u8), msg_controllen), + None => (std::ptr::null_mut(), 0), }; - let msg_hdr = unsafe { pack_mhdr_to_receive(std::ptr::null(), 0, ptr, cap, address.as_mut_ptr()) }; + let msg_hdr = unsafe { pack_mhdr_to_receive(std::ptr::null_mut(), 0, ptr, cap, address.as_mut_ptr()) }; libc::mmsghdr { msg_hdr, msg_len: 0, @@ -1959,9 +1959,9 @@ unsafe fn read_mhdr<'a, 'i, S>( /// /// Buffers must remain valid for the whole lifetime of msghdr unsafe fn pack_mhdr_to_receive( - iov_buffer: *const IoSliceMut, + iov_buffer: *mut IoSliceMut, iov_buffer_len: usize, - cmsg_buffer: *const u8, + cmsg_buffer: *mut u8, cmsg_capacity: usize, address: *mut S, ) -> msghdr @@ -1997,7 +1997,7 @@ fn pack_mhdr_to_send<'a, I, C, S>( // The message header must be initialized before the individual cmsgs. let cmsg_ptr = if capacity > 0 { - cmsg_buffer.as_ptr() as *mut c_void + cmsg_buffer.as_mut_ptr() as *mut c_void } else { ptr::null_mut() }; @@ -2061,7 +2061,7 @@ pub fn recvmsg<'a, 'outer, 'inner, S>(fd: RawFd, iov: &'outer mut [IoSliceMut<'i .map(|v| (v.as_mut_ptr(), v.capacity())) .unwrap_or((ptr::null_mut(), 0)); let mut mhdr = unsafe { - pack_mhdr_to_receive(iov.as_ref().as_ptr(), iov.len(), msg_control, msg_controllen, address.as_mut_ptr()) + pack_mhdr_to_receive(iov.as_mut().as_mut_ptr(), iov.len(), msg_control, msg_controllen, address.as_mut_ptr()) }; let ret = unsafe { libc::recvmsg(fd, &mut mhdr, flags.bits()) }; @@ -2207,7 +2207,7 @@ pub fn recv(sockfd: RawFd, buf: &mut [u8], flags: MsgFlags) -> Result { unsafe { let ret = libc::recv( sockfd, - buf.as_ptr() as *mut c_void, + buf.as_mut_ptr() as *mut c_void, buf.len() as size_t, flags.bits(), ); @@ -2231,7 +2231,7 @@ pub fn recvfrom( let ret = Errno::result(libc::recvfrom( sockfd, - buf.as_ptr() as *mut c_void, + buf.as_mut_ptr() as *mut c_void, buf.len() as size_t, 0, addr.as_mut_ptr() as *mut libc::sockaddr, diff --git a/src/sys/uio.rs b/src/sys/uio.rs index ea9d214d21..c96ade37ff 100644 --- a/src/sys/uio.rs +++ b/src/sys/uio.rs @@ -28,6 +28,9 @@ pub fn writev(fd: RawFd, iov: &[IoSlice<'_>]) -> Result { /// Low-level vectored read from a raw file descriptor /// /// See also [readv(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/readv.html) +// Clippy doesn't know that we need to pass iov mutably only because the +// mutation happens after converting iov to a pointer +#[allow(clippy::needless_pass_by_ref_mut)] pub fn readv(fd: RawFd, iov: &mut [IoSliceMut<'_>]) -> Result { // SAFETY: same as in writev(), IoSliceMut is ABI-compatible with iovec let res = unsafe { @@ -71,6 +74,9 @@ pub fn pwritev(fd: RawFd, iov: &[IoSlice<'_>], offset: off_t) -> Result { /// See also: [`readv`](fn.readv.html) and [`pread`](fn.pread.html) #[cfg(not(any(target_os = "redox", target_os = "haiku")))] #[cfg_attr(docsrs, doc(cfg(all())))] +// Clippy doesn't know that we need to pass iov mutably only because the +// mutation happens after converting iov to a pointer +#[allow(clippy::needless_pass_by_ref_mut)] pub fn preadv( fd: RawFd, iov: &mut [IoSliceMut<'_>],