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 967e3de commit f4f46e3
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 99 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
72 changes: 39 additions & 33 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,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 @@ -81,7 +77,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 @@ -424,7 +424,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 @@ -550,7 +554,7 @@ impl InetAddr {

#[deprecated(since = "0.23.0", note = "use .to_string() instead")]
pub fn to_str(&self) -> String {
format!("{}", self)
format!("{self}")
}
}

Expand Down Expand Up @@ -896,10 +900,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 @@ -1572,7 +1577,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 @@ -1647,7 +1654,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 @@ -1707,12 +1714,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 @@ -1736,12 +1744,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 @@ -2105,7 +2114,7 @@ impl SockAddr {

#[deprecated(since = "0.23.0", note = "use .to_string() instead")]
pub fn to_str(&self) -> String {
format!("{}", self)
format!("{self}")
}

/// Creates a `SockAddr` struct from libc's sockaddr.
Expand Down Expand Up @@ -3142,7 +3151,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 @@ -3162,7 +3171,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 @@ -3181,9 +3190,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 @@ -3193,9 +3201,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 @@ -3204,9 +3211,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
10 changes: 7 additions & 3 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl TryFrom<i32> for SockType {
libc::SOCK_RAW => Ok(Self::Raw),
#[cfg(not(any(target_os = "haiku")))]
libc::SOCK_RDM => Ok(Self::Rdm),
_ => Err(Errno::EINVAL)
_ => Err(Errno::EINVAL),
}
}
}
Expand Down Expand Up @@ -2447,7 +2447,7 @@ pub fn sockaddr_storage_to_addr(
let svm = unsafe { *(addr as *const _ as *const sockaddr_vm) };
Ok(SockAddr::Vsock(VsockAddr(svm)))
}
af => panic!("unexpected address family {}", af),
af => panic!("unexpected address family {af}"),
}
}

Expand Down Expand Up @@ -2485,7 +2485,11 @@ mod tests {
let _ = cmsg_space!(u8);
}

#[cfg(not(any(target_os = "redox", target_os = "linux", target_os = "android")))]
#[cfg(not(any(
target_os = "redox",
target_os = "linux",
target_os = "android"
)))]
#[test]
fn can_open_routing_socket() {
let _ = super::socket(
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 @@ -722,7 +722,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 @@ -1368,7 +1368,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 @@ -1646,7 +1646,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 @@ -2443,7 +2443,7 @@ mod linux_errqueue {
}
*ext_err
} else {
panic!("Unexpected control message {:?}", cmsg);
panic!("Unexpected control message {cmsg:?}");
}
},
)
Expand Down Expand Up @@ -2494,7 +2494,7 @@ mod linux_errqueue {
}
*ext_err
} else {
panic!("Unexpected control message {:?}", cmsg);
panic!("Unexpected control message {cmsg:?}");
}
},
)
Expand Down Expand Up @@ -2528,7 +2528,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
2 changes: 1 addition & 1 deletion test/test_fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ mod test_posix_fallocate {
let err = posix_fallocate(rd as RawFd, 0, 100).unwrap_err();
match err {
Errno::EINVAL | Errno::ENODEV | Errno::ESPIPE | Errno::EBADF => (),
errno => panic!("unexpected errno {}", errno,),
errno => panic!("unexpected errno {errno}",),
}
}
}
Loading

0 comments on commit f4f46e3

Please sign in to comment.