Skip to content

chore: apply some clippy lints #4415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,21 @@ members = [
unused_qualifications = "allow"

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

# FIXME(clippy): all these are default lints and should probably be fixed
identity_op = "allow"
if_same_then_else = "allow"
non_minimal_cfg = "allow"
precedence = "allow"
redundant_field_names = "allow"
redundant_static_lifetimes = "allow"
unnecessary_cast = "allow"
unused_unit = "allow"
zero_ptr = "allow"
# We are okay with the current state of these lints
explicit_iter_loop = "warn"
identity_op = "allow" # some expressions like `0 | x` are clearer for bit ops
manual_assert = "warn"
map_unwrap_or = "warn"
missing_safety_doc = "allow" # safety? in libc? seriously?
non_minimal_cfg = "allow" # for some reason cfg_if! sometimes trigger this
ptr_as_ptr = "warn"
unnecessary_semicolon = "warn"

# FIXME(clippy): these should be fixed if possible
expl_impl_clone_on_copy = "allow"
uninlined_format_args = "allow"
unnecessary_cast = "allow" # some casts like `as usize` are only needed for some targets
used_underscore_binding = "allow"
41 changes: 20 additions & 21 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{env, str};
// List of cfgs this build script is allowed to set. The list is needed to support check-cfg, as we
// need to know all the possible cfgs that this script will set. If you need to set another cfg
// make sure to add it to this list as well.
const ALLOWED_CFGS: &'static [&'static str] = &[
const ALLOWED_CFGS: &[&str] = &[
"emscripten_old_stat_abi",
"espidf_time32",
"freebsd10",
Expand All @@ -24,7 +24,7 @@ const ALLOWED_CFGS: &'static [&'static str] = &[
];

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

let (rustc_minor_ver, _is_nightly) = rustc_minor_nightly();
let rustc_dep_of_std = env::var("CARGO_FEATURE_RUSTC_DEP_OF_STD").is_ok();
let libc_ci = env::var("LIBC_CI").is_ok();
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default();
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
Expand All @@ -66,10 +65,8 @@ fn main() {
vers
} else if libc_ci {
which_freebsd().unwrap_or(12)
} else if rustc_dep_of_std {
12
} else {
12
12 // regardless of CARGO_FEATURE_RUSTC_DEP_OF_STD env var
};

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

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

if !output.status.success() {
panic!(
"failed to run rustc: {}",
String::from_utf8_lossy(output.stderr.as_slice())
);
}
assert!(
output.status.success(),
"failed to run rustc: {}",
String::from_utf8_lossy(output.stderr.as_slice())
);

output
}
Expand All @@ -195,9 +191,11 @@ fn rustc_minor_nightly() -> (u32, bool) {

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

if pieces.next() != Some("rustc 1") {
panic!("Failed to get rustc version");
}
assert_eq!(
pieces.next(),
Some("rustc 1"),
"Failed to get rustc version"
);

let minor = pieces.next();

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

(minor, nightly)
Expand Down Expand Up @@ -254,8 +252,9 @@ fn emcc_version_code() -> Option<u64> {
}

fn set_cfg(cfg: &str) {
if !ALLOWED_CFGS.contains(&cfg) {
panic!("trying to set cfg {cfg}, but it is not in ALLOWED_CFGS");
}
assert!(
ALLOWED_CFGS.contains(&cfg),
"trying to set cfg {cfg}, but it is not in ALLOWED_CFGS",
);
println!("cargo:rustc-cfg={cfg}");
}
6 changes: 3 additions & 3 deletions src/fuchsia/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3429,9 +3429,9 @@ f! {

pub fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr {
if ((*cmsg).cmsg_len as size_t) < mem::size_of::<cmsghdr>() {
0 as *mut cmsghdr
core::ptr::null_mut::<cmsghdr>()
} else if __CMSG_NEXT(cmsg).add(mem::size_of::<cmsghdr>()) >= __MHDR_END(mhdr) {
0 as *mut cmsghdr
core::ptr::null_mut::<cmsghdr>()
} else {
__CMSG_NEXT(cmsg).cast()
}
Expand All @@ -3441,7 +3441,7 @@ f! {
if (*mhdr).msg_controllen as size_t >= mem::size_of::<cmsghdr>() {
(*mhdr).msg_control.cast()
} else {
0 as *mut cmsghdr
core::ptr::null_mut::<cmsghdr>()
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/unix/aix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2441,7 +2441,7 @@ f! {
if (*mhdr).msg_controllen as usize >= mem::size_of::<cmsghdr>() {
(*mhdr).msg_control as *mut cmsghdr
} else {
0 as *mut cmsghdr
core::ptr::null_mut::<cmsghdr>()
}
}

Expand All @@ -2452,7 +2452,7 @@ f! {
if (cmsg as usize + (*cmsg).cmsg_len as usize + mem::size_of::<cmsghdr>())
> ((*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize)
{
0 as *mut cmsghdr
core::ptr::null_mut::<cmsghdr>()
} else {
// AIX does not have any alignment/padding for ancillary data, so we don't need _CMSG_ALIGN here.
(cmsg as usize + (*cmsg).cmsg_len as usize) as *mut cmsghdr
Expand Down
6 changes: 3 additions & 3 deletions src/unix/bsd/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,7 @@ impl siginfo_t {
si_value: crate::sigval,
}

(*(self as *const siginfo_t as *const siginfo_timer)).si_value
(*(self as *const siginfo_t).cast::<siginfo_timer>()).si_value
}

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

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

pub const THREAD_EXTENDED_POLICY_COUNT: mach_msg_type_number_t =
Expand Down Expand Up @@ -5538,7 +5538,7 @@ f! {
pub fn CMSG_NXTHDR(mhdr: *const crate::msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr {
if cmsg.is_null() {
return crate::CMSG_FIRSTHDR(mhdr);
};
}
let cmsg_len = (*cmsg).cmsg_len as usize;
let next = cmsg as usize + __DARWIN_ALIGN32(cmsg_len);
let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize;
Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/freebsdlike/dragonfly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1560,7 +1560,7 @@ f! {
if next <= max {
(cmsg as usize + _CMSG_ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr
} else {
0 as *mut cmsghdr
core::ptr::null_mut::<cmsghdr>()
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/unix/bsd/freebsdlike/freebsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4906,7 +4906,7 @@ const_fn! {

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

pub {const} fn CMSG_LEN(length: c_uint) -> c_uint {
Expand All @@ -4921,7 +4921,7 @@ f! {
cmsg as usize + _ALIGN((*cmsg).cmsg_len as usize) + _ALIGN(mem::size_of::<cmsghdr>());
let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize;
if next > max {
0 as *mut cmsghdr
core::ptr::null_mut::<cmsghdr>()
} else {
(cmsg as usize + _ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr
}
Expand Down Expand Up @@ -4968,14 +4968,12 @@ f! {
let bitset_bits = 8 * mem::size_of::<c_long>();
let (idx, offset) = (cpu / bitset_bits, cpu % bitset_bits);
cpuset.__bits[idx] |= 1 << offset;
()
}

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

pub fn CPU_ISSET(cpu: usize, cpuset: &cpuset_t) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions src/unix/bsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ pub const RTAX_BRD: c_int = 7;
f! {
pub fn CMSG_FIRSTHDR(mhdr: *const crate::msghdr) -> *mut cmsghdr {
if (*mhdr).msg_controllen as usize >= mem::size_of::<cmsghdr>() {
(*mhdr).msg_control as *mut cmsghdr
(*mhdr).msg_control.cast::<cmsghdr>()
} else {
core::ptr::null_mut()
}
Expand All @@ -623,7 +623,7 @@ f! {
}

pub fn FD_ZERO(set: *mut fd_set) -> () {
for slot in (*set).fds_bits.iter_mut() {
for slot in &mut (*set).fds_bits {
*slot = 0;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/unix/bsd/netbsdlike/netbsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2423,7 +2423,7 @@ const_fn! {

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

pub {const} fn CMSG_LEN(length: c_uint) -> c_uint {
Expand All @@ -2438,7 +2438,7 @@ f! {
cmsg as usize + _ALIGN((*cmsg).cmsg_len as usize) + _ALIGN(mem::size_of::<cmsghdr>());
let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize;
if next > max {
0 as *mut cmsghdr
core::ptr::null_mut::<cmsghdr>()
} else {
(cmsg as usize + _ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr
}
Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/netbsdlike/openbsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1940,7 +1940,7 @@ f! {
cmsg as usize + _ALIGN((*cmsg).cmsg_len as usize) + _ALIGN(mem::size_of::<cmsghdr>());
let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize;
if next > max {
0 as *mut cmsghdr
core::ptr::null_mut::<cmsghdr>()
} else {
(cmsg as usize + _ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr
}
Expand Down
4 changes: 2 additions & 2 deletions src/unix/haiku/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,7 @@ f! {
if (*mhdr).msg_controllen as usize >= mem::size_of::<cmsghdr>() {
(*mhdr).msg_control as *mut cmsghdr
} else {
0 as *mut cmsghdr
core::ptr::null_mut::<cmsghdr>()
}
}

Expand All @@ -1595,7 +1595,7 @@ f! {
+ CMSG_ALIGN(mem::size_of::<cmsghdr>());
let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize;
if next > max {
0 as *mut cmsghdr
core::ptr::null_mut::<cmsghdr>()
} else {
(cmsg as usize + CMSG_ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr
}
Expand Down
6 changes: 3 additions & 3 deletions src/unix/hurd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3444,7 +3444,7 @@ f! {
if (*mhdr).msg_controllen as usize >= mem::size_of::<cmsghdr>() {
(*mhdr).msg_control as *mut cmsghdr
} else {
0 as *mut cmsghdr
core::ptr::null_mut::<cmsghdr>()
}
}

Expand All @@ -3462,14 +3462,14 @@ f! {

pub fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr {
if ((*cmsg).cmsg_len as usize) < mem::size_of::<cmsghdr>() {
return 0 as *mut cmsghdr;
return core::ptr::null_mut::<cmsghdr>();
};
let next = (cmsg as usize + CMSG_ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr;
let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize;
if (next.offset(1)) as usize > max
|| next as usize + CMSG_ALIGN((*next).cmsg_len as usize) > max
{
0 as *mut cmsghdr
core::ptr::null_mut::<cmsghdr>()
} else {
next as *mut cmsghdr
}
Expand Down
2 changes: 1 addition & 1 deletion src/unix/linux_like/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3611,7 +3611,7 @@ f! {
let next = (cmsg as usize + super::CMSG_ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr;
let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize;
if (next.offset(1)) as usize > max {
0 as *mut cmsghdr
core::ptr::null_mut::<cmsghdr>()
} else {
next as *mut cmsghdr
}
Expand Down
4 changes: 2 additions & 2 deletions src/unix/linux_like/emscripten/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1412,12 +1412,12 @@ pub const SOMAXCONN: c_int = 128;
f! {
pub fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr {
if ((*cmsg).cmsg_len as usize) < mem::size_of::<cmsghdr>() {
return 0 as *mut cmsghdr;
return core::ptr::null_mut::<cmsghdr>();
};
let next = (cmsg as usize + super::CMSG_ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr;
let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize;
if (next.offset(1)) as usize > max {
0 as *mut cmsghdr
core::ptr::null_mut::<cmsghdr>()
} else {
next as *mut cmsghdr
}
Expand Down
2 changes: 1 addition & 1 deletion src/unix/linux_like/linux/gnu/b64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ cfg_if! {
} else if #[cfg(any(target_arch = "s390x"))] {
mod s390x;
pub use self::s390x::*;
} else if #[cfg(any(target_arch = "x86_64"))] {
} else if #[cfg(target_arch = "x86_64")] {
mod x86_64;
pub use self::x86_64::*;
} else if #[cfg(any(target_arch = "riscv64"))] {
Expand Down
1 change: 1 addition & 0 deletions src/unix/linux_like/linux/gnu/b64/riscv64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ pub const REG_NARGS: usize = 8;

pub const COMPAT_HWCAP_ISA_I: c_ulong = 1 << (b'I' - b'A');
pub const COMPAT_HWCAP_ISA_M: c_ulong = 1 << (b'M' - b'A');
#[allow(clippy::eq_op)]
pub const COMPAT_HWCAP_ISA_A: c_ulong = 1 << (b'A' - b'A');
pub const COMPAT_HWCAP_ISA_F: c_ulong = 1 << (b'F' - b'A');
pub const COMPAT_HWCAP_ISA_D: c_ulong = 1 << (b'D' - b'A');
Expand Down
6 changes: 3 additions & 3 deletions src/unix/linux_like/linux/gnu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ impl siginfo_t {
_si_code: c_int,
si_addr: *mut c_void,
}
(*(self as *const siginfo_t as *const siginfo_sigfault)).si_addr
(*(self as *const siginfo_t).cast::<siginfo_sigfault>()).si_addr
}

pub unsafe fn si_value(&self) -> crate::sigval {
Expand All @@ -438,7 +438,7 @@ impl siginfo_t {
_si_overrun: c_int,
si_sigval: crate::sigval,
}
(*(self as *const siginfo_t as *const siginfo_timer)).si_sigval
(*(self as *const siginfo_t).cast::<siginfo_timer>()).si_sigval
}
}

Expand Down Expand Up @@ -502,7 +502,7 @@ struct siginfo_f {

impl siginfo_t {
unsafe fn sifields(&self) -> &sifields {
&(*(self as *const siginfo_t as *const siginfo_f)).sifields
&(*(self as *const siginfo_t).cast::<siginfo_f>()).sifields
}

pub unsafe fn si_pid(&self) -> crate::pid_t {
Expand Down
Loading
Loading