Skip to content

Commit 172b20a

Browse files
authored
Merge pull request #4415 from nyurik/lints
chore: apply some clippy lints
2 parents 1b5abd7 + a283b9e commit 172b20a

File tree

23 files changed

+91
-95
lines changed

23 files changed

+91
-95
lines changed

Cargo.toml

+17-11
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,21 @@ members = [
151151
unused_qualifications = "allow"
152152

153153
[lints.clippy]
154-
missing_safety_doc = "allow"
154+
# Enable pedantic lints - use this manually once in a while, but don't enable by default
155+
# pedantic = { level = "warn", priority = -1 }
155156

156-
# FIXME(clippy): all these are default lints and should probably be fixed
157-
identity_op = "allow"
158-
if_same_then_else = "allow"
159-
non_minimal_cfg = "allow"
160-
precedence = "allow"
161-
redundant_field_names = "allow"
162-
redundant_static_lifetimes = "allow"
163-
unnecessary_cast = "allow"
164-
unused_unit = "allow"
165-
zero_ptr = "allow"
157+
# We are okay with the current state of these lints
158+
explicit_iter_loop = "warn"
159+
identity_op = "allow" # some expressions like `0 | x` are clearer for bit ops
160+
manual_assert = "warn"
161+
map_unwrap_or = "warn"
162+
missing_safety_doc = "allow" # safety? in libc? seriously?
163+
non_minimal_cfg = "allow" # for some reason cfg_if! sometimes trigger this
164+
ptr_as_ptr = "warn"
165+
unnecessary_semicolon = "warn"
166+
167+
# FIXME(clippy): these should be fixed if possible
168+
expl_impl_clone_on_copy = "allow"
169+
uninlined_format_args = "allow"
170+
unnecessary_cast = "allow" # some casts like `as usize` are only needed for some targets
171+
used_underscore_binding = "allow"

build.rs

+20-21
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{env, str};
44
// List of cfgs this build script is allowed to set. The list is needed to support check-cfg, as we
55
// need to know all the possible cfgs that this script will set. If you need to set another cfg
66
// make sure to add it to this list as well.
7-
const ALLOWED_CFGS: &'static [&'static str] = &[
7+
const ALLOWED_CFGS: &[&str] = &[
88
"emscripten_old_stat_abi",
99
"espidf_time32",
1010
"freebsd10",
@@ -24,7 +24,7 @@ const ALLOWED_CFGS: &'static [&'static str] = &[
2424
];
2525

2626
// Extra values to allow for check-cfg.
27-
const CHECK_CFG_EXTRA: &'static [(&'static str, &'static [&'static str])] = &[
27+
const CHECK_CFG_EXTRA: &[(&str, &[&str])] = &[
2828
(
2929
"target_os",
3030
&[
@@ -46,7 +46,6 @@ fn main() {
4646
println!("cargo:rerun-if-changed=build.rs");
4747

4848
let (rustc_minor_ver, _is_nightly) = rustc_minor_nightly();
49-
let rustc_dep_of_std = env::var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok();
5049
let libc_ci = env::var("LIBC_CI").is_ok();
5150
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default();
5251
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
@@ -66,10 +65,8 @@ fn main() {
6665
vers
6766
} else if libc_ci {
6867
which_freebsd().unwrap_or(12)
69-
} else if rustc_dep_of_std {
70-
12
7168
} else {
72-
12
69+
12 // regardless of CARGO_FEATURE_RUSTC_DEP_OF_STD env var
7370
};
7471

7572
match which_freebsd {
@@ -163,12 +160,11 @@ fn rustc_version_cmd(is_clippy_driver: bool) -> Output {
163160

164161
let output = cmd.output().expect("Failed to get rustc version");
165162

166-
if !output.status.success() {
167-
panic!(
168-
"failed to run rustc: {}",
169-
String::from_utf8_lossy(output.stderr.as_slice())
170-
);
171-
}
163+
assert!(
164+
output.status.success(),
165+
"failed to run rustc: {}",
166+
String::from_utf8_lossy(output.stderr.as_slice())
167+
);
172168

173169
output
174170
}
@@ -195,9 +191,11 @@ fn rustc_minor_nightly() -> (u32, bool) {
195191

196192
let mut pieces = version.split('.');
197193

198-
if pieces.next() != Some("rustc 1") {
199-
panic!("Failed to get rustc version");
200-
}
194+
assert_eq!(
195+
pieces.next(),
196+
Some("rustc 1"),
197+
"Failed to get rustc version"
198+
);
201199

202200
let minor = pieces.next();
203201

@@ -207,9 +205,9 @@ fn rustc_minor_nightly() -> (u32, bool) {
207205
// since a nightly build should either come from CI
208206
// or a git checkout
209207
let nightly_raw = otry!(pieces.next()).split('-').nth(1);
210-
let nightly = nightly_raw
211-
.map(|raw| raw.starts_with("dev") || raw.starts_with("nightly"))
212-
.unwrap_or(false);
208+
let nightly = nightly_raw.map_or(false, |raw| {
209+
raw.starts_with("dev") || raw.starts_with("nightly")
210+
});
213211
let minor = otry!(otry!(minor).parse().ok());
214212

215213
(minor, nightly)
@@ -260,8 +258,9 @@ fn emcc_version_code() -> Option<u64> {
260258
}
261259

262260
fn set_cfg(cfg: &str) {
263-
if !ALLOWED_CFGS.contains(&cfg) {
264-
panic!("trying to set cfg {cfg}, but it is not in ALLOWED_CFGS");
265-
}
261+
assert!(
262+
ALLOWED_CFGS.contains(&cfg),
263+
"trying to set cfg {cfg}, but it is not in ALLOWED_CFGS",
264+
);
266265
println!("cargo:rustc-cfg={cfg}");
267266
}

src/fuchsia/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3429,9 +3429,9 @@ f! {
34293429

34303430
pub fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr {
34313431
if ((*cmsg).cmsg_len as size_t) < mem::size_of::<cmsghdr>() {
3432-
0 as *mut cmsghdr
3432+
core::ptr::null_mut::<cmsghdr>()
34333433
} else if __CMSG_NEXT(cmsg).add(mem::size_of::<cmsghdr>()) >= __MHDR_END(mhdr) {
3434-
0 as *mut cmsghdr
3434+
core::ptr::null_mut::<cmsghdr>()
34353435
} else {
34363436
__CMSG_NEXT(cmsg).cast()
34373437
}
@@ -3441,7 +3441,7 @@ f! {
34413441
if (*mhdr).msg_controllen as size_t >= mem::size_of::<cmsghdr>() {
34423442
(*mhdr).msg_control.cast()
34433443
} else {
3444-
0 as *mut cmsghdr
3444+
core::ptr::null_mut::<cmsghdr>()
34453445
}
34463446
}
34473447

src/unix/aix/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2441,7 +2441,7 @@ f! {
24412441
if (*mhdr).msg_controllen as usize >= mem::size_of::<cmsghdr>() {
24422442
(*mhdr).msg_control as *mut cmsghdr
24432443
} else {
2444-
0 as *mut cmsghdr
2444+
core::ptr::null_mut::<cmsghdr>()
24452445
}
24462446
}
24472447

@@ -2452,7 +2452,7 @@ f! {
24522452
if (cmsg as usize + (*cmsg).cmsg_len as usize + mem::size_of::<cmsghdr>())
24532453
> ((*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize)
24542454
{
2455-
0 as *mut cmsghdr
2455+
core::ptr::null_mut::<cmsghdr>()
24562456
} else {
24572457
// AIX does not have any alignment/padding for ancillary data, so we don't need _CMSG_ALIGN here.
24582458
(cmsg as usize + (*cmsg).cmsg_len as usize) as *mut cmsghdr

src/unix/bsd/apple/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1653,7 +1653,7 @@ impl siginfo_t {
16531653
si_value: crate::sigval,
16541654
}
16551655

1656-
(*(self as *const siginfo_t as *const siginfo_timer)).si_value
1656+
(*(self as *const siginfo_t).cast::<siginfo_timer>()).si_value
16571657
}
16581658

16591659
pub unsafe fn si_pid(&self) -> crate::pid_t {
@@ -5463,7 +5463,7 @@ pub const VMADDR_PORT_ANY: c_uint = 0xFFFFFFFF;
54635463

54645464
const fn __DARWIN_ALIGN32(p: usize) -> usize {
54655465
const __DARWIN_ALIGNBYTES32: usize = mem::size_of::<u32>() - 1;
5466-
p + __DARWIN_ALIGNBYTES32 & !__DARWIN_ALIGNBYTES32
5466+
(p + __DARWIN_ALIGNBYTES32) & !__DARWIN_ALIGNBYTES32
54675467
}
54685468

54695469
pub const THREAD_EXTENDED_POLICY_COUNT: mach_msg_type_number_t =
@@ -5538,7 +5538,7 @@ f! {
55385538
pub fn CMSG_NXTHDR(mhdr: *const crate::msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr {
55395539
if cmsg.is_null() {
55405540
return crate::CMSG_FIRSTHDR(mhdr);
5541-
};
5541+
}
55425542
let cmsg_len = (*cmsg).cmsg_len as usize;
55435543
let next = cmsg as usize + __DARWIN_ALIGN32(cmsg_len);
55445544
let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize;

src/unix/bsd/freebsdlike/dragonfly/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1560,7 +1560,7 @@ f! {
15601560
if next <= max {
15611561
(cmsg as usize + _CMSG_ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr
15621562
} else {
1563-
0 as *mut cmsghdr
1563+
core::ptr::null_mut::<cmsghdr>()
15641564
}
15651565
}
15661566

src/unix/bsd/freebsdlike/freebsd/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -4906,7 +4906,7 @@ const_fn! {
49064906

49074907
f! {
49084908
pub fn CMSG_DATA(cmsg: *const cmsghdr) -> *mut c_uchar {
4909-
(cmsg as *mut c_uchar).offset(_ALIGN(mem::size_of::<cmsghdr>()) as isize)
4909+
(cmsg as *mut c_uchar).add(_ALIGN(mem::size_of::<cmsghdr>()))
49104910
}
49114911

49124912
pub {const} fn CMSG_LEN(length: c_uint) -> c_uint {
@@ -4921,7 +4921,7 @@ f! {
49214921
cmsg as usize + _ALIGN((*cmsg).cmsg_len as usize) + _ALIGN(mem::size_of::<cmsghdr>());
49224922
let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize;
49234923
if next > max {
4924-
0 as *mut cmsghdr
4924+
core::ptr::null_mut::<cmsghdr>()
49254925
} else {
49264926
(cmsg as usize + _ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr
49274927
}
@@ -4968,14 +4968,12 @@ f! {
49684968
let bitset_bits = 8 * mem::size_of::<c_long>();
49694969
let (idx, offset) = (cpu / bitset_bits, cpu % bitset_bits);
49704970
cpuset.__bits[idx] |= 1 << offset;
4971-
()
49724971
}
49734972

49744973
pub fn CPU_CLR(cpu: usize, cpuset: &mut cpuset_t) -> () {
49754974
let bitset_bits = 8 * mem::size_of::<c_long>();
49764975
let (idx, offset) = (cpu / bitset_bits, cpu % bitset_bits);
49774976
cpuset.__bits[idx] &= !(1 << offset);
4978-
()
49794977
}
49804978

49814979
pub fn CPU_ISSET(cpu: usize, cpuset: &cpuset_t) -> bool {

src/unix/bsd/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ pub const RTAX_BRD: c_int = 7;
596596
f! {
597597
pub fn CMSG_FIRSTHDR(mhdr: *const crate::msghdr) -> *mut cmsghdr {
598598
if (*mhdr).msg_controllen as usize >= mem::size_of::<cmsghdr>() {
599-
(*mhdr).msg_control as *mut cmsghdr
599+
(*mhdr).msg_control.cast::<cmsghdr>()
600600
} else {
601601
core::ptr::null_mut()
602602
}
@@ -623,7 +623,7 @@ f! {
623623
}
624624

625625
pub fn FD_ZERO(set: *mut fd_set) -> () {
626-
for slot in (*set).fds_bits.iter_mut() {
626+
for slot in &mut (*set).fds_bits {
627627
*slot = 0;
628628
}
629629
}

src/unix/bsd/netbsdlike/netbsd/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2423,7 +2423,7 @@ const_fn! {
24232423

24242424
f! {
24252425
pub fn CMSG_DATA(cmsg: *const cmsghdr) -> *mut c_uchar {
2426-
(cmsg as *mut c_uchar).offset(_ALIGN(mem::size_of::<cmsghdr>()) as isize)
2426+
(cmsg as *mut c_uchar).add(_ALIGN(mem::size_of::<cmsghdr>()))
24272427
}
24282428

24292429
pub {const} fn CMSG_LEN(length: c_uint) -> c_uint {
@@ -2438,7 +2438,7 @@ f! {
24382438
cmsg as usize + _ALIGN((*cmsg).cmsg_len as usize) + _ALIGN(mem::size_of::<cmsghdr>());
24392439
let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize;
24402440
if next > max {
2441-
0 as *mut cmsghdr
2441+
core::ptr::null_mut::<cmsghdr>()
24422442
} else {
24432443
(cmsg as usize + _ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr
24442444
}

src/unix/bsd/netbsdlike/openbsd/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1940,7 +1940,7 @@ f! {
19401940
cmsg as usize + _ALIGN((*cmsg).cmsg_len as usize) + _ALIGN(mem::size_of::<cmsghdr>());
19411941
let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize;
19421942
if next > max {
1943-
0 as *mut cmsghdr
1943+
core::ptr::null_mut::<cmsghdr>()
19441944
} else {
19451945
(cmsg as usize + _ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr
19461946
}

src/unix/haiku/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,7 @@ f! {
15701570
if (*mhdr).msg_controllen as usize >= mem::size_of::<cmsghdr>() {
15711571
(*mhdr).msg_control as *mut cmsghdr
15721572
} else {
1573-
0 as *mut cmsghdr
1573+
core::ptr::null_mut::<cmsghdr>()
15741574
}
15751575
}
15761576

@@ -1595,7 +1595,7 @@ f! {
15951595
+ CMSG_ALIGN(mem::size_of::<cmsghdr>());
15961596
let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize;
15971597
if next > max {
1598-
0 as *mut cmsghdr
1598+
core::ptr::null_mut::<cmsghdr>()
15991599
} else {
16001600
(cmsg as usize + CMSG_ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr
16011601
}

src/unix/hurd/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3444,7 +3444,7 @@ f! {
34443444
if (*mhdr).msg_controllen as usize >= mem::size_of::<cmsghdr>() {
34453445
(*mhdr).msg_control as *mut cmsghdr
34463446
} else {
3447-
0 as *mut cmsghdr
3447+
core::ptr::null_mut::<cmsghdr>()
34483448
}
34493449
}
34503450

@@ -3462,14 +3462,14 @@ f! {
34623462

34633463
pub fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr {
34643464
if ((*cmsg).cmsg_len as usize) < mem::size_of::<cmsghdr>() {
3465-
return 0 as *mut cmsghdr;
3465+
return core::ptr::null_mut::<cmsghdr>();
34663466
};
34673467
let next = (cmsg as usize + CMSG_ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr;
34683468
let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize;
34693469
if (next.offset(1)) as usize > max
34703470
|| next as usize + CMSG_ALIGN((*next).cmsg_len as usize) > max
34713471
{
3472-
0 as *mut cmsghdr
3472+
core::ptr::null_mut::<cmsghdr>()
34733473
} else {
34743474
next as *mut cmsghdr
34753475
}

src/unix/linux_like/android/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3611,7 +3611,7 @@ f! {
36113611
let next = (cmsg as usize + super::CMSG_ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr;
36123612
let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize;
36133613
if (next.offset(1)) as usize > max {
3614-
0 as *mut cmsghdr
3614+
core::ptr::null_mut::<cmsghdr>()
36153615
} else {
36163616
next as *mut cmsghdr
36173617
}

src/unix/linux_like/emscripten/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1412,12 +1412,12 @@ pub const SOMAXCONN: c_int = 128;
14121412
f! {
14131413
pub fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr {
14141414
if ((*cmsg).cmsg_len as usize) < mem::size_of::<cmsghdr>() {
1415-
return 0 as *mut cmsghdr;
1415+
return core::ptr::null_mut::<cmsghdr>();
14161416
};
14171417
let next = (cmsg as usize + super::CMSG_ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr;
14181418
let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize;
14191419
if (next.offset(1)) as usize > max {
1420-
0 as *mut cmsghdr
1420+
core::ptr::null_mut::<cmsghdr>()
14211421
} else {
14221422
next as *mut cmsghdr
14231423
}

src/unix/linux_like/linux/gnu/b64/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ cfg_if! {
119119
} else if #[cfg(any(target_arch = "s390x"))] {
120120
mod s390x;
121121
pub use self::s390x::*;
122-
} else if #[cfg(any(target_arch = "x86_64"))] {
122+
} else if #[cfg(target_arch = "x86_64")] {
123123
mod x86_64;
124124
pub use self::x86_64::*;
125125
} else if #[cfg(any(target_arch = "riscv64"))] {

src/unix/linux_like/linux/gnu/b64/riscv64/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ pub const REG_NARGS: usize = 8;
601601

602602
pub const COMPAT_HWCAP_ISA_I: c_ulong = 1 << (b'I' - b'A');
603603
pub const COMPAT_HWCAP_ISA_M: c_ulong = 1 << (b'M' - b'A');
604+
#[allow(clippy::eq_op)]
604605
pub const COMPAT_HWCAP_ISA_A: c_ulong = 1 << (b'A' - b'A');
605606
pub const COMPAT_HWCAP_ISA_F: c_ulong = 1 << (b'F' - b'A');
606607
pub const COMPAT_HWCAP_ISA_D: c_ulong = 1 << (b'D' - b'A');

src/unix/linux_like/linux/gnu/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ impl siginfo_t {
425425
_si_code: c_int,
426426
si_addr: *mut c_void,
427427
}
428-
(*(self as *const siginfo_t as *const siginfo_sigfault)).si_addr
428+
(*(self as *const siginfo_t).cast::<siginfo_sigfault>()).si_addr
429429
}
430430

431431
pub unsafe fn si_value(&self) -> crate::sigval {
@@ -438,7 +438,7 @@ impl siginfo_t {
438438
_si_overrun: c_int,
439439
si_sigval: crate::sigval,
440440
}
441-
(*(self as *const siginfo_t as *const siginfo_timer)).si_sigval
441+
(*(self as *const siginfo_t).cast::<siginfo_timer>()).si_sigval
442442
}
443443
}
444444

@@ -502,7 +502,7 @@ struct siginfo_f {
502502

503503
impl siginfo_t {
504504
unsafe fn sifields(&self) -> &sifields {
505-
&(*(self as *const siginfo_t as *const siginfo_f)).sifields
505+
&(*(self as *const siginfo_t).cast::<siginfo_f>()).sifields
506506
}
507507

508508
pub unsafe fn si_pid(&self) -> crate::pid_t {

0 commit comments

Comments
 (0)