Skip to content

Commit 8341e4f

Browse files
bors[bot]rtzoeller
andauthored
Merge #1839
1839: Fix clippy warnings on nightly r=asomers a=rtzoeller 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. Co-authored-by: Ryan Zoeller <rtzoeller@rtzoeller.com>
2 parents e5913c6 + 6e7bddd commit 8341e4f

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 _)))
@@ -2101,7 +2101,7 @@ pub mod netlink {
21012101
return None;
21022102
}
21032103
}
2104-
if (*addr).sa_family as i32 != libc::AF_NETLINK as i32 {
2104+
if (*addr).sa_family as i32 != libc::AF_NETLINK {
21052105
return None;
21062106
}
21072107
Some(Self(ptr::read_unaligned(addr as *const _)))
@@ -2145,7 +2145,7 @@ pub mod alg {
21452145
return None;
21462146
}
21472147
}
2148-
if (*addr).sa_family as i32 != libc::AF_ALG as i32 {
2148+
if (*addr).sa_family as i32 != libc::AF_ALG {
21492149
return None;
21502150
}
21512151
Some(Self(ptr::read_unaligned(addr as *const _)))
@@ -2259,7 +2259,7 @@ pub mod sys_control {
22592259
return None;
22602260
}
22612261
}
2262-
if (*addr).sa_family as i32 != libc::AF_SYSTEM as i32 {
2262+
if (*addr).sa_family as i32 != libc::AF_SYSTEM {
22632263
return None;
22642264
}
22652265
Some(Self(ptr::read_unaligned(addr as *const _)))
@@ -2366,12 +2366,12 @@ mod datalink {
23662366
// Returns an Option just for cross-platform compatibility
23672367
pub fn addr(&self) -> Option<[u8; 6]> {
23682368
Some([
2369-
self.0.sll_addr[0] as u8,
2370-
self.0.sll_addr[1] as u8,
2371-
self.0.sll_addr[2] as u8,
2372-
self.0.sll_addr[3] as u8,
2373-
self.0.sll_addr[4] as u8,
2374-
self.0.sll_addr[5] as u8,
2369+
self.0.sll_addr[0],
2370+
self.0.sll_addr[1],
2371+
self.0.sll_addr[2],
2372+
self.0.sll_addr[3],
2373+
self.0.sll_addr[4],
2374+
self.0.sll_addr[5],
23752375
])
23762376
}
23772377
}
@@ -2402,7 +2402,7 @@ mod datalink {
24022402
return None;
24032403
}
24042404
}
2405-
if (*addr).sa_family as i32 != libc::AF_PACKET as i32 {
2405+
if (*addr).sa_family as i32 != libc::AF_PACKET {
24062406
return None;
24072407
}
24082408
Some(Self(ptr::read_unaligned(addr as *const _)))
@@ -2477,6 +2477,8 @@ mod datalink {
24772477
}
24782478

24792479
/// Physical-layer address (MAC)
2480+
// The cast is not unnecessary on all platforms.
2481+
#[allow(clippy::unnecessary_cast)]
24802482
pub fn addr(&self) -> Option<[u8; 6]> {
24812483
let nlen = self.nlen();
24822484
let data = self.0.sdl_data;
@@ -2522,7 +2524,7 @@ mod datalink {
25222524
return None;
25232525
}
25242526
}
2525-
if (*addr).sa_family as i32 != libc::AF_LINK as i32 {
2527+
if (*addr).sa_family as i32 != libc::AF_LINK {
25262528
return None;
25272529
}
25282530
Some(Self(ptr::read_unaligned(addr as *const _)))
@@ -2566,7 +2568,7 @@ pub mod vsock {
25662568
return None;
25672569
}
25682570
}
2569-
if (*addr).sa_family as i32 != libc::AF_VSOCK as i32 {
2571+
if (*addr).sa_family as i32 != libc::AF_VSOCK {
25702572
return None;
25712573
}
25722574
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
@@ -705,6 +705,8 @@ mod test {
705705
assert_fs_equals(fs, vfs);
706706
}
707707

708+
// The cast is not unnecessary on all platforms.
709+
#[allow(clippy::unnecessary_cast)]
708710
fn assert_fs_equals(fs: Statfs, vfs: Statvfs) {
709711
assert_eq!(fs.files() as u64, vfs.files() as u64);
710712
assert_eq!(fs.blocks() as u64, vfs.blocks() as u64);
@@ -752,6 +754,8 @@ mod test {
752754
assert_fs_equals_strict(fs.unwrap(), vfs.unwrap())
753755
}
754756

757+
// The cast is not unnecessary on all platforms.
758+
#[allow(clippy::unnecessary_cast)]
755759
fn assert_fs_equals_strict(fs: Statfs, vfs: Statvfs) {
756760
assert_eq!(fs.files_free() as u64, vfs.files_free() as u64);
757761
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
@@ -933,6 +933,8 @@ cfg_if!{
933933
/// [cfgetispeed(3p)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/cfgetispeed.html)).
934934
///
935935
/// `cfgetispeed()` extracts the input baud rate from the given `Termios` structure.
936+
// The cast is not unnecessary on all platforms.
937+
#[allow(clippy::unnecessary_cast)]
936938
pub fn cfgetispeed(termios: &Termios) -> u32 {
937939
let inner_termios = termios.get_libc_termios();
938940
unsafe { libc::cfgetispeed(&*inner_termios) as u32 }
@@ -942,6 +944,8 @@ cfg_if!{
942944
/// [cfgetospeed(3p)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/cfgetospeed.html)).
943945
///
944946
/// `cfgetospeed()` extracts the output baud rate from the given `Termios` structure.
947+
// The cast is not unnecessary on all platforms.
948+
#[allow(clippy::unnecessary_cast)]
945949
pub fn cfgetospeed(termios: &Termios) -> u32 {
946950
let inner_termios = termios.get_libc_termios();
947951
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
@@ -306,6 +306,8 @@ impl TimeValLike for TimeSpec {
306306
})
307307
}
308308

309+
// The cast is not unnecessary on all platforms.
310+
#[allow(clippy::unnecessary_cast)]
309311
fn num_seconds(&self) -> i64 {
310312
if self.tv_sec() < 0 && self.tv_nsec() > 0 {
311313
(self.tv_sec() + 1) as i64
@@ -322,6 +324,8 @@ impl TimeValLike for TimeSpec {
322324
self.num_nanoseconds() / 1_000
323325
}
324326

327+
// The cast is not unnecessary on all platforms.
328+
#[allow(clippy::unnecessary_cast)]
325329
fn num_nanoseconds(&self) -> i64 {
326330
let secs = self.num_seconds() * 1_000_000_000;
327331
let nsec = self.nanos_mod_sec();
@@ -549,6 +553,8 @@ impl TimeValLike for TimeVal {
549553
})
550554
}
551555

556+
// The cast is not unnecessary on all platforms.
557+
#[allow(clippy::unnecessary_cast)]
552558
fn num_seconds(&self) -> i64 {
553559
if self.tv_sec() < 0 && self.tv_usec() > 0 {
554560
(self.tv_sec() + 1) as i64
@@ -561,6 +567,8 @@ impl TimeValLike for TimeVal {
561567
self.num_microseconds() / 1_000
562568
}
563569

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

0 commit comments

Comments
 (0)