Skip to content

Commit

Permalink
Fix clippy lints
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
  • Loading branch information
SUPERCILEX committed Dec 3, 2022
1 parent 2605aa7 commit 85fd3a3
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 107 deletions.
2 changes: 1 addition & 1 deletion src/sys/aio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl AioCb {
0 => Ok(()),
num if num > 0 => Err(Errno::from_i32(num)),
-1 => Err(Errno::last()),
num => panic!("unknown aio_error return value {:?}", num),
num => panic!("unknown aio_error return value {num:?}"),
}
}

Expand Down
13 changes: 6 additions & 7 deletions src/sys/mman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::Result;
#[cfg(feature = "fs")]
use crate::{fcntl::OFlag, sys::stat::Mode};
use libc::{self, c_int, c_void, off_t, size_t};
use std::{os::unix::io::RawFd, num::NonZeroUsize};
use std::{num::NonZeroUsize, os::unix::io::RawFd};

libc_bitflags! {
/// Desired memory protection of a memory mapping.
Expand Down Expand Up @@ -424,12 +424,11 @@ pub unsafe fn mmap(
fd: RawFd,
offset: off_t,
) -> Result<*mut c_void> {
let ptr = addr.map_or(
std::ptr::null_mut(),
|a| usize::from(a) as *mut c_void
);

let ret = libc::mmap(ptr, length.into(), prot.bits(), flags.bits(), fd, offset);
let ptr =
addr.map_or(std::ptr::null_mut(), |a| usize::from(a) as *mut c_void);

let ret =
libc::mmap(ptr, length.into(), prot.bits(), flags.bits(), fd, offset);

if ret == libc::MAP_FAILED {
Err(Errno::last())
Expand Down
68 changes: 37 additions & 31 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,15 @@ pub(crate) const fn ipv4addr_to_libc(addr: net::Ipv4Addr) -> libc::in_addr {
static_assertions::assert_eq_size!(net::Ipv4Addr, libc::in_addr);
// Safe because both types have the same memory layout, and no fancy Drop
// impls.
unsafe {
mem::transmute(addr)
}
unsafe { mem::transmute(addr) }
}

/// Convert a std::net::Ipv6Addr into the libc form.
#[cfg(feature = "net")]
pub(crate) const fn ipv6addr_to_libc(addr: &net::Ipv6Addr) -> libc::in6_addr {
static_assertions::assert_eq_size!(net::Ipv6Addr, libc::in6_addr);
// Safe because both are Newtype wrappers around the same libc type
unsafe {
mem::transmute(*addr)
}
unsafe { mem::transmute(*addr) }
}

/// These constants specify the protocol family to be used
Expand All @@ -79,7 +75,11 @@ pub enum AddressFamily {
#[cfg_attr(docsrs, doc(cfg(all())))]
Netlink = libc::AF_NETLINK,
/// Kernel interface for interacting with the routing table
#[cfg(not(any(target_os = "redox", target_os = "linux", target_os = "android")))]
#[cfg(not(any(
target_os = "redox",
target_os = "linux",
target_os = "android"
)))]
Route = libc::PF_ROUTE,
/// Low level packet interface (see [`packet(7)`](https://man7.org/linux/man-pages/man7/packet.7.html))
#[cfg(any(
Expand Down Expand Up @@ -422,7 +422,11 @@ impl AddressFamily {
libc::AF_NETLINK => Some(AddressFamily::Netlink),
#[cfg(any(target_os = "macos", target_os = "macos"))]
libc::AF_SYSTEM => Some(AddressFamily::System),
#[cfg(not(any(target_os = "redox", target_os = "linux", target_os = "android")))]
#[cfg(not(any(
target_os = "redox",
target_os = "linux",
target_os = "android"
)))]
libc::PF_ROUTE => Some(AddressFamily::Route),
#[cfg(any(target_os = "android", target_os = "linux"))]
libc::AF_PACKET => Some(AddressFamily::Packet),
Expand Down Expand Up @@ -592,10 +596,11 @@ impl UnixAddr {
pub fn new_unnamed() -> UnixAddr {
let ret = libc::sockaddr_un {
sun_family: AddressFamily::Unix as sa_family_t,
.. unsafe { mem::zeroed() }
..unsafe { mem::zeroed() }
};

let sun_len: u8 = offset_of!(libc::sockaddr_un, sun_path).try_into().unwrap();
let sun_len: u8 =
offset_of!(libc::sockaddr_un, sun_path).try_into().unwrap();

unsafe { UnixAddr::from_raw_parts(ret, sun_len) }
}
Expand Down Expand Up @@ -1268,7 +1273,9 @@ impl SockaddrLike for SockaddrStorage {
if i32::from(ss.ss_family) == libc::AF_UNIX {
// Safe because we UnixAddr is strictly smaller than
// SockaddrStorage, and we just initialized the structure.
(*(&mut ss as *mut libc::sockaddr_storage as *mut UnixAddr)).sun_len = len as u8;
(*(&mut ss as *mut libc::sockaddr_storage
as *mut UnixAddr))
.sun_len = len as u8;
}
Some(Self { ss })
}
Expand Down Expand Up @@ -1343,7 +1350,7 @@ impl SockaddrLike for SockaddrStorage {
// The UnixAddr type knows its own length
Some(ua) => ua.len(),
// For all else, we're just a boring SockaddrStorage
None => mem::size_of_val(self) as libc::socklen_t
None => mem::size_of_val(self) as libc::socklen_t,
}
}
}
Expand Down Expand Up @@ -1403,12 +1410,13 @@ impl SockaddrStorage {
}
}
// Sanity checks
if self.family() != Some(AddressFamily::Unix) ||
len < offset_of!(libc::sockaddr_un, sun_path) ||
len > mem::size_of::<libc::sockaddr_un>() {
if self.family() != Some(AddressFamily::Unix)
|| len < offset_of!(libc::sockaddr_un, sun_path)
|| len > mem::size_of::<libc::sockaddr_un>()
{
None
} else {
Some(unsafe{&self.su})
Some(unsafe { &self.su })
}
}

Expand All @@ -1432,12 +1440,13 @@ impl SockaddrStorage {
}
}
// Sanity checks
if self.family() != Some(AddressFamily::Unix) ||
len < offset_of!(libc::sockaddr_un, sun_path) ||
len > mem::size_of::<libc::sockaddr_un>() {
if self.family() != Some(AddressFamily::Unix)
|| len < offset_of!(libc::sockaddr_un, sun_path)
|| len > mem::size_of::<libc::sockaddr_un>()
{
None
} else {
Some(unsafe{&mut self.su})
Some(unsafe { &mut self.su })
}
}

Expand Down Expand Up @@ -2486,7 +2495,7 @@ mod tests {
fn display() {
let s = "127.0.0.1:8080";
let addr = SockaddrIn::from_str(s).unwrap();
assert_eq!(s, format!("{}", addr));
assert_eq!(s, format!("{addr}"));
}

#[test]
Expand All @@ -2506,7 +2515,7 @@ mod tests {
fn display() {
let s = "[1234:5678:90ab:cdef::1111:2222]:8080";
let addr = SockaddrIn6::from_str(s).unwrap();
assert_eq!(s, format!("{}", addr));
assert_eq!(s, format!("{addr}"));
}

#[test]
Expand All @@ -2525,9 +2534,8 @@ mod tests {
fn from_sockaddr_un_named() {
let ua = UnixAddr::new("/var/run/mysock").unwrap();
let ptr = ua.as_ptr() as *const libc::sockaddr;
let ss = unsafe {
SockaddrStorage::from_raw(ptr, Some(ua.len()))
}.unwrap();
let ss = unsafe { SockaddrStorage::from_raw(ptr, Some(ua.len())) }
.unwrap();
assert_eq!(ss.len(), ua.len());
}

Expand All @@ -2537,9 +2545,8 @@ mod tests {
let name = String::from("nix\0abstract\0test");
let ua = UnixAddr::new_abstract(name.as_bytes()).unwrap();
let ptr = ua.as_ptr() as *const libc::sockaddr;
let ss = unsafe {
SockaddrStorage::from_raw(ptr, Some(ua.len()))
}.unwrap();
let ss = unsafe { SockaddrStorage::from_raw(ptr, Some(ua.len())) }
.unwrap();
assert_eq!(ss.len(), ua.len());
}

Expand All @@ -2548,9 +2555,8 @@ mod tests {
fn from_sockaddr_un_abstract_unnamed() {
let ua = UnixAddr::new_unnamed();
let ptr = ua.as_ptr() as *const libc::sockaddr;
let ss = unsafe {
SockaddrStorage::from_raw(ptr, Some(ua.len()))
}.unwrap();
let ss = unsafe { SockaddrStorage::from_raw(ptr, Some(ua.len())) }
.unwrap();
assert_eq!(ss.len(), ua.len());
}
}
Expand Down
1 change: 0 additions & 1 deletion src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use libc::{
self, c_int, c_void, iovec, size_t, socklen_t, CMSG_DATA, CMSG_FIRSTHDR,
CMSG_LEN, CMSG_NXTHDR,
};
use std::convert::TryFrom;
use std::io::{IoSlice, IoSliceMut};
#[cfg(feature = "net")]
use std::net;
Expand Down
7 changes: 2 additions & 5 deletions src/sys/socket/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use crate::Result;
use cfg_if::cfg_if;
use libc::{self, c_int, c_void, socklen_t};
use std::ffi::{OsStr, OsString};
use std::{
convert::TryFrom,
mem::{self, MaybeUninit}
};
use std::mem::{self, MaybeUninit};
#[cfg(target_family = "unix")]
use std::os::unix::ffi::OsStrExt;
use std::os::unix::io::RawFd;
Expand Down Expand Up @@ -107,7 +104,7 @@ macro_rules! getsockopt_impl {

match <$ty>::try_from(getter.assume_init()) {
Err(_) => Err(Errno::EINVAL),
Ok(r) => Ok(r)
Ok(r) => Ok(r),
}
}
}
Expand Down
32 changes: 15 additions & 17 deletions src/sys/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,7 @@ impl TimeValLike for TimeSpec {
fn seconds(seconds: i64) -> TimeSpec {
assert!(
(TS_MIN_SECONDS..=TS_MAX_SECONDS).contains(&seconds),
"TimeSpec out of bounds; seconds={}",
seconds
"TimeSpec out of bounds; seconds={seconds}",
);
let mut ts = zero_init_timespec();
ts.tv_sec = seconds as time_t;
Expand Down Expand Up @@ -428,20 +427,20 @@ impl fmt::Display for TimeSpec {

let sec = abs.tv_sec();

write!(f, "{}", sign)?;
write!(f, "{sign}")?;

if abs.tv_nsec() == 0 {
if abs.tv_sec() == 1 {
write!(f, "{} second", sec)?;
if sec == 1 {
write!(f, "1 second")?;
} else {
write!(f, "{} seconds", sec)?;
write!(f, "{sec} seconds")?;
}
} else if abs.tv_nsec() % 1_000_000 == 0 {
write!(f, "{}.{:03} seconds", sec, abs.tv_nsec() / 1_000_000)?;
write!(f, "{sec}.{:03} seconds", abs.tv_nsec() / 1_000_000)?;
} else if abs.tv_nsec() % 1_000 == 0 {
write!(f, "{}.{:06} seconds", sec, abs.tv_nsec() / 1_000)?;
write!(f, "{sec}.{:06} seconds", abs.tv_nsec() / 1_000)?;
} else {
write!(f, "{}.{:09} seconds", sec, abs.tv_nsec())?;
write!(f, "{sec}.{:09} seconds", abs.tv_nsec())?;
}

Ok(())
Expand Down Expand Up @@ -497,8 +496,7 @@ impl TimeValLike for TimeVal {
fn seconds(seconds: i64) -> TimeVal {
assert!(
(TV_MIN_SECONDS..=TV_MAX_SECONDS).contains(&seconds),
"TimeVal out of bounds; seconds={}",
seconds
"TimeVal out of bounds; seconds={seconds}"
);
#[cfg_attr(target_env = "musl", allow(deprecated))]
// https://github.com/rust-lang/libc/issues/1848
Expand Down Expand Up @@ -662,18 +660,18 @@ impl fmt::Display for TimeVal {

let sec = abs.tv_sec();

write!(f, "{}", sign)?;
write!(f, "{sign}")?;

if abs.tv_usec() == 0 {
if abs.tv_sec() == 1 {
write!(f, "{} second", sec)?;
if sec == 1 {
write!(f, "1 second")?;
} else {
write!(f, "{} seconds", sec)?;
write!(f, "{sec} seconds")?;
}
} else if abs.tv_usec() % 1000 == 0 {
write!(f, "{}.{:03} seconds", sec, abs.tv_usec() / 1000)?;
write!(f, "{sec}.{:03} seconds", abs.tv_usec() / 1000)?;
} else {
write!(f, "{}.{:06} seconds", sec, abs.tv_usec())?;
write!(f, "{sec}.{:06} seconds", abs.tv_usec())?;
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion test/sys/test_aio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ fn test_aio_suspend() {
let r = aio_suspend(&cbbuf[..], Some(timeout));
match r {
Err(Errno::EINTR) => continue,
Err(e) => panic!("aio_suspend returned {:?}", e),
Err(e) => panic!("aio_suspend returned {e:?}"),
Ok(_) => (),
};
}
Expand Down
8 changes: 3 additions & 5 deletions test/sys/test_signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ fn test_sigprocmask() {
// test don't make sense.
assert!(
!old_signal_set.contains(SIGNAL),
"the {:?} signal is already blocked, please change to a \
different one",
SIGNAL
"the {SIGNAL:?} signal is already blocked, please change to a \
different one"
);

// Now block the signal.
Expand All @@ -71,8 +70,7 @@ fn test_sigprocmask() {
.expect("expect to be able to retrieve old signals");
assert!(
old_signal_set.contains(SIGNAL),
"expected the {:?} to be blocked",
SIGNAL
"expected the {SIGNAL:?} to be blocked"
);

// Reset the signal.
Expand Down
12 changes: 6 additions & 6 deletions test/sys/test_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ mod recvfrom {
println!("IPv6 not available, skipping test.");
return;
}
Err(e) => panic!("bind: {}", e),
Err(e) => panic!("bind: {e}"),
Ok(()) => (),
}
let ssock = socket(
Expand Down Expand Up @@ -1272,7 +1272,7 @@ fn test_scm_credentials() {
ControlMessageOwned::ScmCredentials(cred) => cred,
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
ControlMessageOwned::ScmCreds(cred) => cred,
other => panic!("unexpected cmsg {:?}", other),
other => panic!("unexpected cmsg {other:?}"),
};
assert!(received_cred.is_none());
assert_eq!(cred.pid(), getpid().as_raw());
Expand Down Expand Up @@ -1550,7 +1550,7 @@ fn loopback_address(
Err(e) => {
let stdioerr = io::stderr();
let mut handle = stdioerr.lock();
writeln!(handle, "getifaddrs: {:?}", e).unwrap();
writeln!(handle, "getifaddrs: {e:?}").unwrap();
return None;
}
};
Expand Down Expand Up @@ -2347,7 +2347,7 @@ mod linux_errqueue {
}
*ext_err
} else {
panic!("Unexpected control message {:?}", cmsg);
panic!("Unexpected control message {cmsg:?}");
}
},
)
Expand Down Expand Up @@ -2398,7 +2398,7 @@ mod linux_errqueue {
}
*ext_err
} else {
panic!("Unexpected control message {:?}", cmsg);
panic!("Unexpected control message {cmsg:?}");
}
},
)
Expand Down Expand Up @@ -2432,7 +2432,7 @@ mod linux_errqueue {
MsgFlags::empty(),
) {
assert_eq!(e, Errno::EADDRNOTAVAIL);
println!("{:?} not available, skipping test.", af);
println!("{af:?} not available, skipping test.");
return;
}

Expand Down
Loading

0 comments on commit 85fd3a3

Please sign in to comment.