Skip to content

Commit 056b683

Browse files
committed
Fix clippy warnings on nightly
Clippy is now smarter about detecting unnecessary casts and useless conversions, which means we need to be more explicit about when the conversions are needed for a subset of platforms. Required changes found by repeatedly running the following command against a list of the supported platforms. `xargs -t -I {} sh -c "cargo clippy -Zbuild-std --target {} --all-targets -- -D warnings || exit 255"` I removed the casts it complained about, and then restored them with an `#[allow]` if a later target needed the cast.
1 parent fabf610 commit 056b683

File tree

14 files changed

+64
-23
lines changed

14 files changed

+64
-23
lines changed

src/dir.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ pub enum Type {
208208
impl Entry {
209209
/// Returns the inode number (`d_ino`) of the underlying `dirent`.
210210
#[allow(clippy::useless_conversion)] // Not useless on all OSes
211+
// The cast is not unnecessary on all platforms.
212+
#[allow(clippy::unnecessary_cast)]
211213
pub fn ino(&self) -> u64 {
212214
cfg_if! {
213215
if #[cfg(any(target_os = "android",

src/errno.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn clear() {
4747

4848
/// Returns the platform-specific value of errno
4949
pub fn errno() -> i32 {
50-
unsafe { (*errno_location()) as i32 }
50+
unsafe { *errno_location() }
5151
}
5252

5353
impl Errno {

src/sys/pthread.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ feature! {
2828
/// won't send any signal.
2929
///
3030
/// [`pthread_kill(3)`]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_kill.html
31+
#[allow(clippy::not_unsafe_ptr_arg_deref)]
3132
#[cfg(not(target_os = "redox"))]
3233
pub fn pthread_kill<T>(thread: Pthread, signal: T) -> Result<()>
3334
where T: Into<Option<crate::sys::signal::Signal>>

src/sys/socket/addr.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ impl SockaddrLike for UnixAddr {
935935
return None;
936936
}
937937
}
938-
if (*addr).sa_family as i32 != libc::AF_UNIX as i32 {
938+
if (*addr).sa_family as i32 != libc::AF_UNIX {
939939
return None;
940940
}
941941
let mut su: libc::sockaddr_un = mem::zeroed();
@@ -1192,7 +1192,7 @@ impl SockaddrLike for SockaddrIn {
11921192
return None;
11931193
}
11941194
}
1195-
if (*addr).sa_family as i32 != libc::AF_INET as i32 {
1195+
if (*addr).sa_family as i32 != libc::AF_INET {
11961196
return None;
11971197
}
11981198
Some(Self(ptr::read_unaligned(addr as *const _)))
@@ -1298,7 +1298,7 @@ impl SockaddrLike for SockaddrIn6 {
12981298
return None;
12991299
}
13001300
}
1301-
if (*addr).sa_family as i32 != libc::AF_INET6 as i32 {
1301+
if (*addr).sa_family as i32 != libc::AF_INET6 {
13021302
return None;
13031303
}
13041304
Some(Self(ptr::read_unaligned(addr as *const _)))
@@ -2185,7 +2185,7 @@ pub mod netlink {
21852185
return None;
21862186
}
21872187
}
2188-
if (*addr).sa_family as i32 != libc::AF_NETLINK as i32 {
2188+
if (*addr).sa_family as i32 != libc::AF_NETLINK {
21892189
return None;
21902190
}
21912191
Some(Self(ptr::read_unaligned(addr as *const _)))
@@ -2229,7 +2229,7 @@ pub mod alg {
22292229
return None;
22302230
}
22312231
}
2232-
if (*addr).sa_family as i32 != libc::AF_ALG as i32 {
2232+
if (*addr).sa_family as i32 != libc::AF_ALG {
22332233
return None;
22342234
}
22352235
Some(Self(ptr::read_unaligned(addr as *const _)))
@@ -2343,7 +2343,7 @@ pub mod sys_control {
23432343
return None;
23442344
}
23452345
}
2346-
if (*addr).sa_family as i32 != libc::AF_SYSTEM as i32 {
2346+
if (*addr).sa_family as i32 != libc::AF_SYSTEM {
23472347
return None;
23482348
}
23492349
Some(Self(ptr::read_unaligned(addr as *const _)))
@@ -2450,12 +2450,12 @@ mod datalink {
24502450
// Returns an Option just for cross-platform compatibility
24512451
pub fn addr(&self) -> Option<[u8; 6]> {
24522452
Some([
2453-
self.0.sll_addr[0] as u8,
2454-
self.0.sll_addr[1] as u8,
2455-
self.0.sll_addr[2] as u8,
2456-
self.0.sll_addr[3] as u8,
2457-
self.0.sll_addr[4] as u8,
2458-
self.0.sll_addr[5] as u8,
2453+
self.0.sll_addr[0],
2454+
self.0.sll_addr[1],
2455+
self.0.sll_addr[2],
2456+
self.0.sll_addr[3],
2457+
self.0.sll_addr[4],
2458+
self.0.sll_addr[5],
24592459
])
24602460
}
24612461
}
@@ -2486,7 +2486,7 @@ mod datalink {
24862486
return None;
24872487
}
24882488
}
2489-
if (*addr).sa_family as i32 != libc::AF_PACKET as i32 {
2489+
if (*addr).sa_family as i32 != libc::AF_PACKET {
24902490
return None;
24912491
}
24922492
Some(Self(ptr::read_unaligned(addr as *const _)))
@@ -2561,6 +2561,8 @@ mod datalink {
25612561
}
25622562

25632563
/// Physical-layer address (MAC)
2564+
// The cast is not unnecessary on all platforms.
2565+
#[allow(clippy::unnecessary_cast)]
25642566
pub fn addr(&self) -> Option<[u8; 6]> {
25652567
let nlen = self.nlen();
25662568
let data = self.0.sdl_data;
@@ -2606,7 +2608,7 @@ mod datalink {
26062608
return None;
26072609
}
26082610
}
2609-
if (*addr).sa_family as i32 != libc::AF_LINK as i32 {
2611+
if (*addr).sa_family as i32 != libc::AF_LINK {
26102612
return None;
26112613
}
26122614
Some(Self(ptr::read_unaligned(addr as *const _)))
@@ -2650,7 +2652,7 @@ pub mod vsock {
26502652
return None;
26512653
}
26522654
}
2653-
if (*addr).sa_family as i32 != libc::AF_VSOCK as i32 {
2655+
if (*addr).sa_family as i32 != libc::AF_VSOCK {
26542656
return None;
26552657
}
26562658
Some(Self(ptr::read_unaligned(addr as *const _)))

src/sys/socket/mod.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,8 @@ impl ControlMessageOwned {
839839
unsafe fn decode_from(header: &cmsghdr) -> ControlMessageOwned
840840
{
841841
let p = CMSG_DATA(header);
842+
// The cast is not unnecessary on all platforms.
843+
#[allow(clippy::unnecessary_cast)]
842844
let len = header as *const _ as usize + header.cmsg_len as usize
843845
- p as usize;
844846
match (header.cmsg_level, header.cmsg_type) {
@@ -1643,7 +1645,7 @@ pub fn recvmmsg<'a, I, S>(
16431645
}
16441646
);
16451647

1646-
(msg_controllen as usize, &mut d.cmsg_buffer)
1648+
(msg_controllen, &mut d.cmsg_buffer)
16471649
}).collect();
16481650

16491651
let timeout = if let Some(mut t) = timeout {
@@ -1662,6 +1664,8 @@ pub fn recvmmsg<'a, I, S>(
16621664
.zip(addresses.iter().map(|addr| unsafe{addr.assume_init()}))
16631665
.zip(results.into_iter())
16641666
.map(|((mmsghdr, address), (msg_controllen, cmsg_buffer))| {
1667+
// The cast is not unnecessary on all platforms.
1668+
#[allow(clippy::unnecessary_cast)]
16651669
unsafe {
16661670
read_mhdr(
16671671
mmsghdr.msg_hdr,
@@ -1684,6 +1688,8 @@ unsafe fn read_mhdr<'a, 'b, S>(
16841688
) -> RecvMsg<'b, S>
16851689
where S: SockaddrLike
16861690
{
1691+
// The cast is not unnecessary on all platforms.
1692+
#[allow(clippy::unnecessary_cast)]
16871693
let cmsghdr = {
16881694
if mhdr.msg_controllen > 0 {
16891695
// got control message(s)
@@ -2131,15 +2137,15 @@ pub fn sockaddr_storage_to_addr(
21312137
match c_int::from(addr.ss_family) {
21322138
#[cfg(feature = "net")]
21332139
libc::AF_INET => {
2134-
assert!(len as usize >= mem::size_of::<sockaddr_in>());
2140+
assert!(len >= mem::size_of::<sockaddr_in>());
21352141
let sin = unsafe {
21362142
*(addr as *const sockaddr_storage as *const sockaddr_in)
21372143
};
21382144
Ok(SockAddr::Inet(InetAddr::V4(sin)))
21392145
}
21402146
#[cfg(feature = "net")]
21412147
libc::AF_INET6 => {
2142-
assert!(len as usize >= mem::size_of::<sockaddr_in6>());
2148+
assert!(len >= mem::size_of::<sockaddr_in6>());
21432149
let sin6 = unsafe {
21442150
*(addr as *const _ as *const sockaddr_in6)
21452151
};

src/sys/socket/sockopt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ struct SetU8 {
863863

864864
impl<'a> Set<'a, u8> for SetU8 {
865865
fn new(val: &'a u8) -> SetU8 {
866-
SetU8 { val: *val as u8 }
866+
SetU8 { val: *val }
867867
}
868868

869869
fn ffi_ptr(&self) -> *const c_void {

src/sys/statfs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,8 @@ mod test {
702702
assert_fs_equals(fs, vfs);
703703
}
704704

705+
// The cast is not unnecessary on all platforms.
706+
#[allow(clippy::unnecessary_cast)]
705707
fn assert_fs_equals(fs: Statfs, vfs: Statvfs) {
706708
assert_eq!(fs.files() as u64, vfs.files() as u64);
707709
assert_eq!(fs.blocks() as u64, vfs.blocks() as u64);
@@ -749,6 +751,8 @@ mod test {
749751
assert_fs_equals_strict(fs.unwrap(), vfs.unwrap())
750752
}
751753

754+
// The cast is not unnecessary on all platforms.
755+
#[allow(clippy::unnecessary_cast)]
752756
fn assert_fs_equals_strict(fs: Statfs, vfs: Statvfs) {
753757
assert_eq!(fs.files_free() as u64, vfs.files_free() as u64);
754758
assert_eq!(fs.blocks_free() as u64, vfs.blocks_free() as u64);

src/sys/sysinfo.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ impl SysInfo {
3030
}
3131

3232
/// Returns the time since system boot.
33+
// The cast is not unnecessary on all platforms.
34+
#[allow(clippy::unnecessary_cast)]
3335
pub fn uptime(&self) -> Duration {
3436
// Truncate negative values to 0
3537
Duration::from_secs(cmp::max(self.0.uptime, 0) as u64)
@@ -64,6 +66,8 @@ impl SysInfo {
6466
self.scale_mem(self.0.freeram)
6567
}
6668

69+
// The cast is not unnecessary on all platforms.
70+
#[allow(clippy::unnecessary_cast)]
6771
fn scale_mem(&self, units: mem_blocks_t) -> u64 {
6872
units as u64 * self.0.mem_unit as u64
6973
}

src/sys/termios.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,8 @@ cfg_if!{
894894
/// [cfgetispeed(3p)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/cfgetispeed.html)).
895895
///
896896
/// `cfgetispeed()` extracts the input baud rate from the given `Termios` structure.
897+
// The cast is not unnecessary on all platforms.
898+
#[allow(clippy::unnecessary_cast)]
897899
pub fn cfgetispeed(termios: &Termios) -> u32 {
898900
let inner_termios = termios.get_libc_termios();
899901
unsafe { libc::cfgetispeed(&*inner_termios) as u32 }
@@ -903,6 +905,8 @@ cfg_if!{
903905
/// [cfgetospeed(3p)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/cfgetospeed.html)).
904906
///
905907
/// `cfgetospeed()` extracts the output baud rate from the given `Termios` structure.
908+
// The cast is not unnecessary on all platforms.
909+
#[allow(clippy::unnecessary_cast)]
906910
pub fn cfgetospeed(termios: &Termios) -> u32 {
907911
let inner_termios = termios.get_libc_termios();
908912
unsafe { libc::cfgetospeed(&*inner_termios) as u32 }

src/sys/time.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ impl TimeValLike for TimeSpec {
304304
TimeSpec(ts)
305305
}
306306

307+
// The cast is not unnecessary on all platforms.
308+
#[allow(clippy::unnecessary_cast)]
307309
fn num_seconds(&self) -> i64 {
308310
if self.tv_sec() < 0 && self.tv_nsec() > 0 {
309311
(self.tv_sec() + 1) as i64
@@ -320,6 +322,8 @@ impl TimeValLike for TimeSpec {
320322
self.num_nanoseconds() / 1_000
321323
}
322324

325+
// The cast is not unnecessary on all platforms.
326+
#[allow(clippy::unnecessary_cast)]
323327
fn num_nanoseconds(&self) -> i64 {
324328
let secs = self.num_seconds() * 1_000_000_000;
325329
let nsec = self.nanos_mod_sec();
@@ -547,6 +551,8 @@ impl TimeValLike for TimeVal {
547551
})
548552
}
549553

554+
// The cast is not unnecessary on all platforms.
555+
#[allow(clippy::unnecessary_cast)]
550556
fn num_seconds(&self) -> i64 {
551557
if self.tv_sec() < 0 && self.tv_usec() > 0 {
552558
(self.tv_sec() + 1) as i64
@@ -559,6 +565,8 @@ impl TimeValLike for TimeVal {
559565
self.num_microseconds() / 1_000
560566
}
561567

568+
// The cast is not unnecessary on all platforms.
569+
#[allow(clippy::unnecessary_cast)]
562570
fn num_microseconds(&self) -> i64 {
563571
let secs = self.num_seconds() * 1_000_000;
564572
let usec = self.micros_mod_sec();

0 commit comments

Comments
 (0)