Skip to content

Commit e679015

Browse files
committed
chore: lint libc-test/build.rs
run `cargo clippy --all-targets` on `libc-test/build.rs`, and fix all default issues. ### Notes * `copy_dir_hotfix` had a `replace` parameter that was never used
1 parent f2dc14f commit e679015

File tree

4 files changed

+83
-124
lines changed

4 files changed

+83
-124
lines changed

ctest/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,10 +2299,7 @@ impl<'a, 'v> Visitor<'v> for Generator<'a> {
22992299
self.assert_no_generics(i.ident, generics);
23002300
for arg in &decl.inputs {
23012301
if let ast::TyKind::Array(_, _) = arg.ty.node {
2302-
panic!(
2303-
"Foreign Function decl `{}` uses array in C FFI",
2304-
i.ident
2305-
);
2302+
panic!("Foreign Function decl `{}` uses array in C FFI", i.ident);
23062303
}
23072304
}
23082305

libc-test/Cargo.toml

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,7 @@ harness = true
108108

109109
[lints.rust]
110110
# FIXME(cleanup): make ident usage consistent in each file
111+
# Switching this to "warn" causes issues with generated code
111112
unused_qualifications = "allow"
112113

113114
[lints.clippy]
114-
# FIXME(clippy): fix these
115-
needless_return = "allow"
116-
comparison_to_empty = "allow"
117-
unused_io_amount = "allow"
118-
write_with_newline = "allow"
119-
needless_borrows_for_generic_args = "allow"
120-
only_used_in_recursion = "allow"
121-
match_like_matches_macro = "allow"
122-
useless_format = "allow"
123-
wildcard_in_or_patterns = "allow"
124-
nonminimal_bool = "allow"
125-
match_single_binding = "allow"

libc-test/build.rs

Lines changed: 78 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,23 @@ fn do_cc() {
5050

5151
fn do_ctest() {
5252
match &env::var("TARGET").unwrap() {
53-
t if t.contains("android") => return test_android(t),
54-
t if t.contains("apple") => return test_apple(t),
55-
t if t.contains("dragonfly") => return test_dragonflybsd(t),
56-
t if t.contains("emscripten") => return test_emscripten(t),
57-
t if t.contains("freebsd") => return test_freebsd(t),
58-
t if t.contains("haiku") => return test_haiku(t),
59-
t if t.contains("linux") => return test_linux(t),
60-
t if t.contains("netbsd") => return test_netbsd(t),
61-
t if t.contains("openbsd") => return test_openbsd(t),
62-
t if t.contains("cygwin") => return test_cygwin(t),
63-
t if t.contains("redox") => return test_redox(t),
64-
t if t.contains("solaris") => return test_solarish(t),
65-
t if t.contains("illumos") => return test_solarish(t),
66-
t if t.contains("wasi") => return test_wasi(t),
67-
t if t.contains("windows") => return test_windows(t),
68-
t if t.contains("vxworks") => return test_vxworks(t),
69-
t if t.contains("nto-qnx") => return test_neutrino(t),
53+
t if t.contains("android") => test_android(t),
54+
t if t.contains("apple") => test_apple(t),
55+
t if t.contains("dragonfly") => test_dragonflybsd(t),
56+
t if t.contains("emscripten") => test_emscripten(t),
57+
t if t.contains("freebsd") => test_freebsd(t),
58+
t if t.contains("haiku") => test_haiku(t),
59+
t if t.contains("linux") => test_linux(t),
60+
t if t.contains("netbsd") => test_netbsd(t),
61+
t if t.contains("openbsd") => test_openbsd(t),
62+
t if t.contains("cygwin") => test_cygwin(t),
63+
t if t.contains("redox") => test_redox(t),
64+
t if t.contains("solaris") => test_solarish(t),
65+
t if t.contains("illumos") => test_solarish(t),
66+
t if t.contains("wasi") => test_wasi(t),
67+
t if t.contains("windows") => test_windows(t),
68+
t if t.contains("vxworks") => test_vxworks(t),
69+
t if t.contains("nto-qnx") => test_neutrino(t),
7070
t => panic!("unknown target {t}"),
7171
}
7272
}
@@ -104,7 +104,7 @@ fn do_semver() {
104104
process_semver_file(&mut output, &mut semver_root, &os);
105105
let os_arch = format!("{os}-{arch}");
106106
process_semver_file(&mut output, &mut semver_root, &os_arch);
107-
if target_env != "" {
107+
if !target_env.is_empty() {
108108
let os_env = format!("{os}-{target_env}");
109109
process_semver_file(&mut output, &mut semver_root, &os_env);
110110

@@ -129,21 +129,21 @@ fn process_semver_file<W: Write, P: AsRef<Path>>(output: &mut W, path: &mut Path
129129
};
130130
let input = BufReader::new(input_file);
131131

132-
write!(output, "// Source: {}.\n", path.display()).unwrap();
133-
output.write(b"use libc::{\n").unwrap();
132+
writeln!(output, "// Source: {}.", path.display()).unwrap();
133+
output.write_all(b"use libc::{\n").unwrap();
134134
for line in input.lines() {
135135
let line = line.unwrap().into_bytes();
136136
match line.first() {
137137
// Ignore comments and empty lines.
138138
Some(b'#') | None => continue,
139139
_ => {
140-
output.write(b" ").unwrap();
141-
output.write(&line).unwrap();
142-
output.write(b",\n").unwrap();
140+
output.write_all(b" ").unwrap();
141+
output.write_all(&line).unwrap();
142+
output.write_all(b",\n").unwrap();
143143
}
144144
}
145145
}
146-
output.write(b"};\n\n").unwrap();
146+
output.write_all(b"};\n\n").unwrap();
147147
path.pop();
148148
}
149149

@@ -165,8 +165,10 @@ fn main() {
165165
do_semver();
166166
}
167167

168+
// FIXME(clippy): removing `replace` somehow fails the `Test tier1 (x86_64-pc-windows-msvc, windows-2022)` CI job
169+
#[allow(clippy::only_used_in_recursion)]
168170
fn copy_dir_hotfix(src: &Path, dst: &Path, regex: &regex::bytes::Regex, replace: &[u8]) {
169-
std::fs::create_dir(&dst).unwrap();
171+
std::fs::create_dir(dst).unwrap();
170172
for entry in src.read_dir().unwrap() {
171173
let entry = entry.unwrap();
172174
let src_path = entry.path();
@@ -433,10 +435,7 @@ fn test_apple(target: &str) {
433435

434436
cfg.volatile_item(|i| {
435437
use ctest::VolatileItemKind::*;
436-
match i {
437-
StructField(ref n, ref f) if n == "aiocb" && f == "aio_buf" => true,
438-
_ => false,
439-
}
438+
matches!(i, StructField(ref n, ref f) if n == "aiocb" && f == "aio_buf")
440439
});
441440

442441
cfg.type_name(move |ty, is_struct, is_union| {
@@ -712,7 +711,7 @@ fn test_cygwin(target: &str) {
712711
t if t.ends_with("_t") => t.to_string(),
713712

714713
// sigval is a struct in Rust, but a union in C:
715-
"sigval" => format!("union sigval"),
714+
"sigval" => "union sigval".to_string(),
716715

717716
// put `struct` in front of all structs:.
718717
t if is_struct => format!("struct {t}"),
@@ -913,10 +912,7 @@ fn test_windows(target: &str) {
913912
}
914913
});
915914

916-
cfg.skip_field(move |s, field| match s {
917-
"CONTEXT" if field == "Fp" => true,
918-
_ => false,
919-
});
915+
cfg.skip_field(|s, field| s == "CONTEXT" && field == "Fp");
920916
// FIXME(windows): All functions point to the wrong addresses?
921917
cfg.skip_fn_ptrcheck(|_| true);
922918

@@ -1102,10 +1098,7 @@ fn test_solarish(target: &str) {
11021098
}
11031099
}
11041100

1105-
cfg.skip_type(move |ty| match ty {
1106-
"sighandler_t" => true,
1107-
_ => false,
1108-
});
1101+
cfg.skip_type(|ty| ty == "sighandler_t");
11091102

11101103
cfg.type_name(move |ty, is_struct, is_union| match ty {
11111104
"FILE" => "__FILE".to_string(),
@@ -1449,6 +1442,7 @@ fn test_netbsd(target: &str) {
14491442
});
14501443

14511444
cfg.skip_fn(move |name| {
1445+
#[expect(clippy::wildcard_in_or_patterns)]
14521446
match name {
14531447
// FIXME(netbsd): netbsd 10 minimum
14541448
"getentropy" | "getrandom" => true,
@@ -1597,7 +1591,7 @@ fn test_dragonflybsd(target: &str) {
15971591
t if t.ends_with("_t") => t.to_string(),
15981592

15991593
// sigval is a struct in Rust, but a union in C:
1600-
"sigval" => format!("union sigval"),
1594+
"sigval" => "union sigval".to_string(),
16011595

16021596
// put `struct` in front of all structs:.
16031597
t if is_struct => format!("struct {t}"),
@@ -1984,7 +1978,7 @@ fn test_android(target: &str) {
19841978
t if t.ends_with("_t") => t.to_string(),
19851979

19861980
// sigval is a struct in Rust, but a union in C:
1987-
"sigval" => format!("union sigval"),
1981+
"sigval" => "union sigval".to_string(),
19881982

19891983
// put `struct` in front of all structs:.
19901984
t if is_struct => format!("struct {t}"),
@@ -2346,18 +2340,9 @@ fn test_freebsd(target: &str) {
23462340
// Required for making freebsd11_stat available in the headers
23472341
cfg.define("_WANT_FREEBSD11_STAT", None);
23482342

2349-
let freebsd13 = match freebsd_ver {
2350-
Some(n) if n >= 13 => true,
2351-
_ => false,
2352-
};
2353-
let freebsd14 = match freebsd_ver {
2354-
Some(n) if n >= 14 => true,
2355-
_ => false,
2356-
};
2357-
let freebsd15 = match freebsd_ver {
2358-
Some(n) if n >= 15 => true,
2359-
_ => false,
2360-
};
2343+
let freebsd13 = matches!(freebsd_ver, Some(n) if n >= 13);
2344+
let freebsd14 = matches!(freebsd_ver, Some(n) if n >= 14);
2345+
let freebsd15 = matches!(freebsd_ver, Some(n) if n >= 15);
23612346

23622347
headers! { cfg:
23632348
"aio.h",
@@ -2500,7 +2485,7 @@ fn test_freebsd(target: &str) {
25002485
t if t.ends_with("_t") => t.to_string(),
25012486

25022487
// sigval is a struct in Rust, but a union in C:
2503-
"sigval" => format!("union sigval"),
2488+
"sigval" => "union sigval".to_string(),
25042489

25052490
// put `struct` in front of all structs:.
25062491
t if is_struct => format!("struct {t}"),
@@ -3237,7 +3222,7 @@ fn test_neutrino(target: &str) {
32373222

32383223
let mut cfg = ctest_cfg();
32393224
if target.ends_with("_iosock") {
3240-
let qnx_target_val = std::env::var("QNX_TARGET")
3225+
let qnx_target_val = env::var("QNX_TARGET")
32413226
.unwrap_or_else(|_| "QNX_TARGET_not_set_please_source_qnxsdp".into());
32423227

32433228
cfg.include(qnx_target_val + "/usr/include/io-sock");
@@ -3484,17 +3469,17 @@ fn test_neutrino(target: &str) {
34843469
struct_ == "_idle_hook" && field == "time"
34853470
});
34863471

3487-
cfg.skip_field(move |struct_, field| {
3488-
(struct_ == "__sched_param" && field == "reserved") ||
3489-
(struct_ == "sched_param" && field == "reserved") ||
3490-
(struct_ == "sigevent" && field == "__padding1") || // ensure alignment
3491-
(struct_ == "sigevent" && field == "__padding2") || // union
3492-
(struct_ == "sigevent" && field == "__sigev_un2") || // union
3493-
// sighandler_t type is super weird
3494-
(struct_ == "sigaction" && field == "sa_sigaction") ||
3495-
// does not exist
3496-
(struct_ == "syspage_entry" && field == "__reserved") ||
3497-
false // keep me for smaller diffs when something is added above
3472+
cfg.skip_field(|struct_, field| {
3473+
matches!(
3474+
(struct_, field),
3475+
("__sched_param", "reserved")
3476+
| ("sched_param", "reserved")
3477+
| ("sigevent", "__padding1") // ensure alignment
3478+
| ("sigevent", "__padding2") // union
3479+
| ("sigevent", "__sigev_un2") // union
3480+
| ("sigaction", "sa_sigaction") // sighandler_t type is super weird
3481+
| ("syspage_entry", "__reserved") // does not exist
3482+
)
34983483
});
34993484

35003485
cfg.skip_static(move |name| (name == "__dso_handle"));
@@ -3574,19 +3559,16 @@ fn test_vxworks(target: &str) {
35743559
_ => false,
35753560
});
35763561
// FIXME(vxworks)
3577-
cfg.skip_type(move |ty| match ty {
3578-
"stat64" | "sighandler_t" | "off64_t" => true,
3579-
_ => false,
3580-
});
3562+
cfg.skip_type(|ty| matches!(ty, "stat64" | "sighandler_t" | "off64_t"));
35813563

3582-
cfg.skip_field_type(move |struct_, field| match (struct_, field) {
3583-
("siginfo_t", "si_value") | ("stat", "st_size") | ("sigaction", "sa_u") => true,
3584-
_ => false,
3564+
cfg.skip_field_type(|struct_, field| {
3565+
matches!(
3566+
(struct_, field),
3567+
("siginfo_t", "si_value") | ("stat", "st_size") | ("sigaction", "sa_u")
3568+
)
35853569
});
35863570

3587-
cfg.skip_roundtrip(move |s| match s {
3588-
_ => false,
3589-
});
3571+
cfg.skip_roundtrip(|_| false);
35903572

35913573
cfg.type_name(move |ty, is_struct, is_union| match ty {
35923574
"DIR" | "FILE" | "Dl_info" | "RTP_DESC" => ty.to_string(),
@@ -4842,12 +4824,9 @@ fn test_linux_like_apis(target: &str) {
48424824
cfg.skip_type(|_| true).skip_static(|_| true);
48434825

48444826
headers! { cfg: "string.h" }
4845-
cfg.skip_fn(|f| match f {
4846-
"strerror_r" => false,
4847-
_ => true,
4848-
})
4849-
.skip_const(|_| true)
4850-
.skip_struct(|_| true);
4827+
cfg.skip_fn(|f| f != "strerror_r")
4828+
.skip_const(|_| true)
4829+
.skip_struct(|_| true);
48514830
cfg.generate(src_hotfix_dir().join("lib.rs"), "linux_strerror_r.rs");
48524831
}
48534832

@@ -4891,10 +4870,11 @@ fn test_linux_like_apis(target: &str) {
48914870
cfg.skip_type(|_| true)
48924871
.skip_static(|_| true)
48934872
.skip_fn(|_| true)
4894-
.skip_const(|c| match c {
4895-
"BOTHER" | "IBSHIFT" => false,
4896-
"TCGETS2" | "TCSETS2" | "TCSETSW2" | "TCSETSF2" => false,
4897-
_ => true,
4873+
.skip_const(|c| {
4874+
!matches!(
4875+
c,
4876+
"BOTHER" | "IBSHIFT" | "TCGETS2" | "TCSETS2" | "TCSETSW2" | "TCSETSF2"
4877+
)
48984878
})
48994879
.skip_struct(|s| s != "termios2")
49004880
.type_name(move |ty, is_struct, is_union| match ty {
@@ -4920,13 +4900,15 @@ fn test_linux_like_apis(target: &str) {
49204900
.skip_fn(|_| true)
49214901
.skip_const(|_| true)
49224902
.skip_struct(|_| true)
4923-
.skip_const(move |name| match name {
4924-
"IPV6_FLOWINFO"
4925-
| "IPV6_FLOWLABEL_MGR"
4926-
| "IPV6_FLOWINFO_SEND"
4927-
| "IPV6_FLOWINFO_FLOWLABEL"
4928-
| "IPV6_FLOWINFO_PRIORITY" => false,
4929-
_ => true,
4903+
.skip_const(|name| {
4904+
!matches!(
4905+
name,
4906+
"IPV6_FLOWINFO"
4907+
| "IPV6_FLOWLABEL_MGR"
4908+
| "IPV6_FLOWINFO_SEND"
4909+
| "IPV6_FLOWINFO_FLOWLABEL"
4910+
| "IPV6_FLOWINFO_PRIORITY"
4911+
)
49304912
})
49314913
.type_name(move |ty, is_struct, is_union| match ty {
49324914
t if is_struct => format!("struct {t}"),
@@ -4948,14 +4930,8 @@ fn test_linux_like_apis(target: &str) {
49484930
.skip_static(|_| true)
49494931
.skip_const(|_| true)
49504932
.type_name(move |ty, _is_struct, _is_union| ty.to_string())
4951-
.skip_struct(move |ty| match ty {
4952-
"Elf64_Phdr" | "Elf32_Phdr" => false,
4953-
_ => true,
4954-
})
4955-
.skip_type(move |ty| match ty {
4956-
"Elf64_Phdr" | "Elf32_Phdr" => false,
4957-
_ => true,
4958-
});
4933+
.skip_struct(|ty| !matches!(ty, "Elf64_Phdr" | "Elf32_Phdr"))
4934+
.skip_type(|ty| !matches!(ty, "Elf64_Phdr" | "Elf32_Phdr"));
49594935
cfg.generate(src_hotfix_dir().join("lib.rs"), "linux_elf.rs");
49604936
}
49614937

@@ -4966,10 +4942,7 @@ fn test_linux_like_apis(target: &str) {
49664942
cfg.header("linux/if_arp.h");
49674943
cfg.skip_fn(|_| true)
49684944
.skip_static(|_| true)
4969-
.skip_const(move |name| match name {
4970-
"ARPHRD_CAN" => false,
4971-
_ => true,
4972-
})
4945+
.skip_const(|name| name != "ARPHRD_CAN")
49734946
.skip_struct(|_| true)
49744947
.skip_type(|_| true);
49754948
cfg.generate(src_hotfix_dir().join("lib.rs"), "linux_if_arp.rs");
@@ -5314,7 +5287,7 @@ fn test_haiku(target: &str) {
53145287
}
53155288

53165289
// is actually a union
5317-
"sigval" => format!("union sigval"),
5290+
"sigval" => "union sigval".to_string(),
53185291
t if is_union => format!("union {t}"),
53195292
t if t.ends_with("_t") => t.to_string(),
53205293
t if is_struct => format!("struct {t}"),

0 commit comments

Comments
 (0)