Skip to content

Commit 3fbef95

Browse files
d-e-s-odanielocfb
authored andcommitted
Overhaul and reuse workspace-wide Rust lints
Overhaul the set of Rust lints in use and make them apply in a workspace-wide manner. Fix everything new that is being flagged. Signed-off-by: Daniel Müller <[email protected]>
1 parent 74f5759 commit 3fbef95

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+269
-176
lines changed

.clippy.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# For general information about this file, check
2+
# https://doc.rust-lang.org/stable/clippy/configuration.html
3+
4+
doc-valid-idents = [
5+
"Host1X",
6+
"HugeTLB",
7+
"InfiniBand",
8+
"JITed",
9+
"NetFilter",
10+
"QLogic",
11+
"RxRPC",
12+
"SMBus",
13+
"SunRPC",
14+
"Video4Linux2",
15+
"..",
16+
]

.github/workflows/test.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ jobs:
111111
- uses: Swatinem/rust-cache@v2
112112
- name: Build libbpf-rs sample
113113
run: |
114-
cargo init --bin libbpf-rs-test-project
115-
cd libbpf-rs-test-project
114+
cargo init --bin ../libbpf-rs-test-project
115+
cd ../libbpf-rs-test-project
116116
cat >> Cargo.toml <<EOF
117-
libbpf-rs = { path = "../libbpf-rs", ${{ matrix.args }} }
117+
libbpf-rs = { path = "../libbpf-rs/libbpf-rs", ${{ matrix.args }} }
118118
EOF
119119
cat > src/main.rs <<EOF
120120
fn main() {
@@ -237,7 +237,7 @@ jobs:
237237
# We want the old resolver here as it has more suitable feature
238238
# unification logic for this invocation.
239239
sed -i 's@resolver = "2"@resolver = "1"@' Cargo.toml
240-
cargo clippy --locked --no-deps --all-targets --tests --features=dont-generate-test-files -- -D warnings -D clippy::absolute_paths
240+
cargo clippy --locked --no-deps --all-targets --tests --features=dont-generate-test-files -- -D warnings
241241
242242
rustfmt:
243243
name: Check code formatting

Cargo.toml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,34 @@ members = [
2222
"examples/tproxy",
2323
]
2424
resolver = "2"
25+
26+
[workspace.lints.rust]
27+
deprecated-safe = "warn"
28+
future-incompatible = "warn"
29+
keyword-idents = "warn"
30+
let-underscore = "warn"
31+
missing-debug-implementations = "warn"
32+
missing-docs = "warn"
33+
trivial-numeric-casts = "warn"
34+
unexpected_cfgs = {level = "warn", check-cfg = ['cfg(has_procmap_query_ioctl)', 'cfg(has_large_test_files)']}
35+
unsafe-op-in-unsafe-fn = "warn"
36+
unused = "warn"
37+
38+
[workspace.lints.clippy]
39+
collapsible-else-if = "allow"
40+
collapsible-if = "allow"
41+
fn-to-numeric-cast = "allow"
42+
let-and-return = "allow"
43+
let-unit-value = "allow"
44+
module-inception = "allow"
45+
type-complexity = "allow"
46+
absolute-paths = "warn"
47+
clone-on-ref-ptr = "warn"
48+
dbg-macro = "warn"
49+
doc-markdown = "warn"
50+
join-absolute-paths = "warn"
51+
large-enum-variant = "warn"
52+
redundant-closure-for-method-calls = "warn"
53+
unchecked-duration-subtraction = "warn"
54+
uninlined-format-args = "warn"
55+
wildcard-imports = "warn"

examples/bpf_query/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ clap = { version = "4.0.32", default-features = false, features = ["std", "deriv
1212

1313
[target.'cfg(target_arch = "x86_64")'.dependencies]
1414
iced-x86 = "1.20.0"
15+
16+
[lints]
17+
workspace = true

examples/bpf_query/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Example illustrating how to query certain BPF information.
2+
13
use std::process::exit;
24

35
use clap::Parser;
@@ -46,7 +48,7 @@ fn prog(args: ProgArgs) {
4648
let insn = d.decode();
4749
let mut f_insn = String::new();
4850
f.format(&insn, &mut f_insn);
49-
println!(" {}: {}", ip, f_insn);
51+
println!(" {ip}: {f_insn}");
5052
}
5153
}
5254

examples/capable/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ clap = { version = "4.0.32", default-features = false, features = ["std", "deriv
2020

2121
[features]
2222
static = ["libbpf-rs/static"]
23+
24+
[lints]
25+
workspace = true

examples/capable/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Build script for the `capable` example.
2+
13
use std::env;
24
use std::ffi::OsStr;
35
use std::path::PathBuf;

examples/capable/src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// Author Devasia Thomas <https://www.linkedin.com/in/devasiathomas/>
44
//
55
// Based on capable(8) by Brendan Gregg
6+
7+
//! Example showing basic functionality of capable(8).
8+
69
use core::time::Duration;
710
use std::mem::MaybeUninit;
811
use std::str;
@@ -27,7 +30,7 @@ mod capable {
2730
}
2831

2932
use capable::types::uniqueness;
30-
use capable::*;
33+
use capable::CapableSkelBuilder;
3134

3235
static CAPS: phf::Map<i32, &'static str> = phf_map! {
3336
0i32 => "CAP_CHOWN",

examples/netfilter_blocklist/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ clap = { version = "4.0.32", default-features = false, features = ["std", "deriv
1717
ctrlc = "3.2"
1818
libbpf-rs = { path = "../../libbpf-rs" }
1919
plain = "0.2"
20+
21+
[lints]
22+
workspace = true

examples/netfilter_blocklist/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Build script for the `netfilter_blocklist` example.
2+
13
use std::env;
24
use std::ffi::OsStr;
35
use std::path::PathBuf;

examples/netfilter_blocklist/src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! A NetFilter blocklist example.
2+
13
use std::mem::MaybeUninit;
24
use std::net::Ipv4Addr;
35
use std::str::FromStr;
@@ -26,6 +28,7 @@ mod netfilter {
2628
));
2729
}
2830

31+
#[allow(clippy::wildcard_imports)]
2932
use netfilter::*;
3033

3134
/// Netfilter Blocklist Example
@@ -51,7 +54,7 @@ fn main() -> Result<()> {
5154

5255
// Install Ctrl-C handler
5356
let running = Arc::new(AtomicBool::new(true));
54-
let r = running.clone();
57+
let r = Arc::clone(&running);
5558
ctrlc::set_handler(move || {
5659
r.store(false, Ordering::SeqCst);
5760
})?;

examples/ringbuf_multi/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ vmlinux = { git = "https://github.com/libbpf/vmlinux.h.git", rev = "83a228cf37fc
1313
libbpf-rs = { path = "../../libbpf-rs" }
1414
libc = "0.2"
1515
plain = "0.2"
16+
17+
[lints]
18+
workspace = true

examples/ringbuf_multi/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Build script for the `ringbuf_multi` example.
2+
13
use std::env;
24
use std::ffi::OsStr;
35
use std::path::PathBuf;

examples/ringbuf_multi/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
22

3+
//! Example illustrating `BPF_MAP_TYPE_ARRAY_OF_MAPS` usage.
4+
35
use core::time::Duration;
46
use std::ffi::c_int;
57
use std::mem::MaybeUninit;
@@ -18,6 +20,7 @@ mod ringbuf_multi {
1820
));
1921
}
2022

23+
#[allow(clippy::wildcard_imports)]
2124
use ringbuf_multi::*;
2225

2326
unsafe impl Plain for types::sample {}

examples/runqslower/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ libc = "0.2"
1616
plain = "0.2"
1717
time = { version = "0.3", features = ["formatting", "local-offset", "macros"]}
1818
clap = { version = "4.0.32", default-features = false, features = ["std", "derive", "help", "usage"] }
19+
20+
[lints]
21+
workspace = true

examples/runqslower/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Build script for the `runqslower` example.
2+
13
use std::env;
24
use std::ffi::OsStr;
35
use std::path::PathBuf;

examples/runqslower/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
22

3+
//! An example illustrating how to trace run queue latency using BPF.
4+
35
use std::mem::MaybeUninit;
46
use std::str;
57
use std::time::Duration;
@@ -21,6 +23,7 @@ mod runqslower {
2123
));
2224
}
2325

26+
#[allow(clippy::wildcard_imports)]
2427
use runqslower::*;
2528

2629
/// Trace high run queue latency

examples/tc_port_whitelist/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ libbpf-rs = { path = "../../libbpf-rs" }
1515
libc = "0.2"
1616
nix = { version = "0.28", default-features = false, features = ["net", "user"] }
1717
clap = { version = "4.0.32", default-features = false, features = ["std", "derive", "help", "usage"] }
18+
19+
[lints]
20+
workspace = true

examples/tc_port_whitelist/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Build script for the `rc_port_whitelist` example.
2+
13
use std::env;
24
use std::ffi::OsStr;
35
use std::path::PathBuf;

examples/tc_port_whitelist/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(clippy::let_unit_value)]
1+
//! An example showing how to block ports using TC.
22
33
use std::mem::MaybeUninit;
44
use std::os::unix::io::AsFd as _;
@@ -24,7 +24,7 @@ use nix::net::if_::if_nametoindex;
2424
mod tc {
2525
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/bpf/tc.skel.rs"));
2626
}
27-
use tc::*;
27+
use tc::TcSkelBuilder;
2828

2929
#[derive(Debug, Parser)]
3030
struct Command {

examples/tcp_ca/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ clap = { version = "4.0.32", features = ["derive"] }
1515
# works with that.
1616
the-original-libbpf-rs = { path = "../../libbpf-rs", package = "libbpf-rs" }
1717
libc = "0.2"
18+
19+
[lints]
20+
workspace = true

examples/tcp_ca/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Build script for the `runqslower` example.
2+
13
use std::env;
24
use std::ffi::OsStr;
35
use std::path::PathBuf;

examples/tcp_ca/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
22

3-
#![allow(clippy::let_unit_value)]
3+
//! An example illustrating `struct_opt` usage to change TCP congestion
4+
//! algorithms.
45
56
use std::ffi::c_int;
67
use std::ffi::c_void;
@@ -80,7 +81,7 @@ fn set_tcp_ca(fd: BorrowedFd<'_>, tcp_ca: &OsStr) -> Result<()> {
8081
IPPROTO_TCP,
8182
TCP_CONGESTION,
8283
bytes.as_ptr().cast(),
83-
bytes.len() as _,
84+
bytes.len(),
8485
)
8586
.with_context(|| {
8687
format!(

examples/tcp_option/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ libbpf-rs = { path = "../../libbpf-rs" }
1414
clap = { version = "4.0.32", features = ["derive"] }
1515
libc = "0.2"
1616
ctrlc = "3.2"
17+
18+
[lints]
19+
workspace = true

examples/tcp_option/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Build script for the `tcp_option` example.
2+
13
use std::env;
24
use std::ffi::OsStr;
35
use std::path::PathBuf;

examples/tcp_option/src/main.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Example illustrating how to modify TCP headers.
2+
13
use std::mem::MaybeUninit;
24
use std::net::Ipv4Addr;
35
use std::os::fd::AsFd;
@@ -35,7 +37,7 @@ mod tcp_option {
3537
"/src/bpf/tcp_option.skel.rs"
3638
));
3739
}
38-
use tcp_option::*;
40+
use tcp_option::TcpOptionSkelBuilder;
3941

4042
const SOL_SOCKET: c_int = 1;
4143
const SO_ATTACH_BPF: c_int = 50;
@@ -70,7 +72,7 @@ fn main() -> Result<()> {
7072
let opts = Command::parse();
7173

7274
let running = Arc::new(AtomicBool::new(true));
73-
let r = running.clone();
75+
let r = Arc::clone(&running);
7476
ctrlc::set_handler(move || {
7577
r.store(false, Ordering::SeqCst);
7678
})?;
@@ -110,7 +112,7 @@ fn main() -> Result<()> {
110112
let prog_fd = skel.progs.socket_handler.as_fd();
111113
match unsafe {
112114
libc::setsockopt(
113-
target_socket_fd as c_int,
115+
target_socket_fd,
114116
SOL_SOCKET,
115117
SO_ATTACH_BPF,
116118
&prog_fd as *const _ as *const c_void,

examples/tproxy/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ clap = { version = "4.0.32", default-features = false, features = ["std", "deriv
1818
ctrlc = "3.2"
1919
libbpf-rs = { path = "../../libbpf-rs" }
2020
nix = { version = "0.28", default-features = false, features = ["net", "user"] }
21+
22+
[lints]
23+
workspace = true

examples/tproxy/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Build script for the `tproxy` example.
2+
13
use std::env;
24
use std::ffi::OsStr;
35
use std::path::PathBuf;

examples/tproxy/src/bin/proxy.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! A fake proxy.
2+
13
use std::net::TcpListener;
24
use std::net::TcpStream;
35
use std::os::unix::io::AsRawFd as _;

examples/tproxy/src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Example implementing the classic iptables TPROXY target.
2+
13
use std::mem::MaybeUninit;
24
use std::net::Ipv4Addr;
35
use std::os::unix::io::AsFd as _;
@@ -23,7 +25,7 @@ mod tproxy {
2325
));
2426
}
2527

26-
use tproxy::*;
28+
use tproxy::TproxySkelBuilder;
2729

2830
/// Transparent proxy driver
2931
///
@@ -53,7 +55,7 @@ fn main() -> Result<()> {
5355

5456
// Install Ctrl-C handler
5557
let running = Arc::new(AtomicBool::new(true));
56-
let r = running.clone();
58+
let r = Arc::clone(&running);
5759
ctrlc::set_handler(move || {
5860
r.store(false, Ordering::SeqCst);
5961
})?;

libbpf-cargo/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ clap = { version = "4.0.32", features = ["derive"] }
4545
goblin = "0.9"
4646
test-log = { version = "0.2.16", default-features = false, features = ["log"] }
4747
vmlinux = { git = "https://github.com/libbpf/vmlinux.h.git", rev = "83a228cf37fc65f2d14e4896a04922b5ee531a94" }
48+
49+
[lints]
50+
workspace = true

libbpf-cargo/src/build.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ impl BpfObjBuilder {
6262
}
6363

6464
/// We're essentially going to run:
65-
///
66-
/// clang -g -O2 -target bpf -c -D__TARGET_ARCH_$(ARCH) runqslower.bpf.c -o runqslower.bpf.o
65+
/// ```text
66+
/// clang -g -O2 -target bpf -c -D__TARGET_ARCH_$(ARCH) runqslower.bpf.c -o runqslower.bpf.o
67+
/// ```
6768
///
6869
/// for each prog.
6970
fn compile_single(
@@ -297,6 +298,8 @@ fn extract_clang_or_default(clang: Option<&Path>) -> PathBuf {
297298
}
298299
}
299300

301+
/// Build the project, assuming necessary skeleton files have already
302+
/// been generated.
300303
pub fn build_project(
301304
manifest_path: Option<&Path>,
302305
clang: Option<&Path>,

0 commit comments

Comments
 (0)