Skip to content

Commit a598506

Browse files
nyuriktgross35
authored andcommitted
chore: apply some clippy lints
(backport <rust-lang#4415>) [ drop changes around the FreeBSD version in build.rs since the logic isn't the same - Trevor ] (cherry picked from commit a283b9e)
1 parent b21e075 commit a598506

File tree

23 files changed

+90
-91
lines changed

23 files changed

+90
-91
lines changed

Cargo.toml

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,21 @@ members = [
159159
unused_qualifications = "allow"
160160

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

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

build.rs

Lines changed: 19 additions & 17 deletions
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",
@@ -25,7 +25,7 @@ const ALLOWED_CFGS: &'static [&'static str] = &[
2525
];
2626

2727
// Extra values to allow for check-cfg.
28-
const CHECK_CFG_EXTRA: &'static [(&'static str, &'static [&'static str])] = &[
28+
const CHECK_CFG_EXTRA: &[(&str, &[&str])] = &[
2929
(
3030
"target_os",
3131
&[
@@ -169,12 +169,11 @@ fn rustc_version_cmd(is_clippy_driver: bool) -> Output {
169169

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

172-
if !output.status.success() {
173-
panic!(
174-
"failed to run rustc: {}",
175-
String::from_utf8_lossy(output.stderr.as_slice())
176-
);
177-
}
172+
assert!(
173+
output.status.success(),
174+
"failed to run rustc: {}",
175+
String::from_utf8_lossy(output.stderr.as_slice())
176+
);
178177

179178
output
180179
}
@@ -201,9 +200,11 @@ fn rustc_minor_nightly() -> (u32, bool) {
201200

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

204-
if pieces.next() != Some("rustc 1") {
205-
panic!("Failed to get rustc version");
206-
}
203+
assert_eq!(
204+
pieces.next(),
205+
Some("rustc 1"),
206+
"Failed to get rustc version"
207+
);
207208

208209
let minor = pieces.next();
209210

@@ -213,9 +214,9 @@ fn rustc_minor_nightly() -> (u32, bool) {
213214
// since a nightly build should either come from CI
214215
// or a git checkout
215216
let nightly_raw = otry!(pieces.next()).split('-').nth(1);
216-
let nightly = nightly_raw
217-
.map(|raw| raw.starts_with("dev") || raw.starts_with("nightly"))
218-
.unwrap_or(false);
217+
let nightly = nightly_raw.map_or(false, |raw| {
218+
raw.starts_with("dev") || raw.starts_with("nightly")
219+
});
219220
let minor = otry!(otry!(minor).parse().ok());
220221

221222
(minor, nightly)
@@ -266,8 +267,9 @@ fn emcc_version_code() -> Option<u64> {
266267
}
267268

268269
fn set_cfg(cfg: &str) {
269-
if !ALLOWED_CFGS.contains(&cfg) {
270-
panic!("trying to set cfg {cfg}, but it is not in ALLOWED_CFGS");
271-
}
270+
assert!(
271+
ALLOWED_CFGS.contains(&cfg),
272+
"trying to set cfg {cfg}, but it is not in ALLOWED_CFGS",
273+
);
272274
println!("cargo:rustc-cfg={cfg}");
273275
}

src/fuchsia/mod.rs

Lines changed: 3 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2442,7 +2442,7 @@ f! {
24422442
if (*mhdr).msg_controllen as usize >= mem::size_of::<cmsghdr>() {
24432443
(*mhdr).msg_control as *mut cmsghdr
24442444
} else {
2445-
0 as *mut cmsghdr
2445+
core::ptr::null_mut::<cmsghdr>()
24462446
}
24472447
}
24482448

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

src/unix/bsd/apple/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,7 +1691,7 @@ impl siginfo_t {
16911691
si_value: crate::sigval,
16921692
}
16931693

1694-
(*(self as *const siginfo_t as *const siginfo_timer)).si_value
1694+
(*(self as *const siginfo_t).cast::<siginfo_timer>()).si_value
16951695
}
16961696

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

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

56415641
pub const THREAD_EXTENDED_POLICY_COUNT: mach_msg_type_number_t =
@@ -5710,7 +5710,7 @@ f! {
57105710
pub fn CMSG_NXTHDR(mhdr: *const crate::msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr {
57115711
if cmsg.is_null() {
57125712
return crate::CMSG_FIRSTHDR(mhdr);
5713-
};
5713+
}
57145714
let cmsg_len = (*cmsg).cmsg_len as usize;
57155715
let next = cmsg as usize + __DARWIN_ALIGN32(cmsg_len);
57165716
let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize;

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4930,7 +4930,7 @@ const_fn! {
49304930

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

49364936
pub {const} fn CMSG_LEN(length: c_uint) -> c_uint {
@@ -4945,7 +4945,7 @@ f! {
49454945
cmsg as usize + _ALIGN((*cmsg).cmsg_len as usize) + _ALIGN(mem::size_of::<cmsghdr>());
49464946
let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize;
49474947
if next > max {
4948-
0 as *mut cmsghdr
4948+
core::ptr::null_mut::<cmsghdr>()
49494949
} else {
49504950
(cmsg as usize + _ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr
49514951
}
@@ -4992,14 +4992,12 @@ f! {
49924992
let bitset_bits = 8 * mem::size_of::<c_long>();
49934993
let (idx, offset) = (cpu / bitset_bits, cpu % bitset_bits);
49944994
cpuset.__bits[idx] |= 1 << offset;
4995-
()
49964995
}
49974996

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

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

src/unix/bsd/mod.rs

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,7 @@ f! {
19571957
cmsg as usize + _ALIGN((*cmsg).cmsg_len as usize) + _ALIGN(mem::size_of::<cmsghdr>());
19581958
let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize;
19591959
if next > max {
1960-
0 as *mut cmsghdr
1960+
core::ptr::null_mut::<cmsghdr>()
19611961
} else {
19621962
(cmsg as usize + _ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr
19631963
}

0 commit comments

Comments
 (0)