//! Find HTTP-service mistakes in axum/reqwest code.
//! TK011 client-per-request reqwest::Client::new()/builder() inside an async fn or loop
//! TK012 unwrap-on-await-in-handler .await.unwrap() / .await.expect() in an axum handler
//! TK013 outbound-without-timeout reqwest .send().await and no `timeout(` anywhere in the file
//! TK014 no-graceful-shutdown axum::serve(..) without .with_graceful_shutdown(..)
//! TK017 axum07-path-syntax "/:id" or "/*rest" route paths (axum 0.8 uses "/{id}" and "/{*rest}")
//!
//! Build: rustc -O --edition 2021 scripts/service_hygiene.rs -o /tmp/service_hygiene
//! Usage: service_hygiene <FILES|DIRS|-> [--json] [--strict] [--select IDS] [--ignore IDS]
mod async_ctx;
mod cli;
mod rsparse;
use async_ctx::{fn_at, handlers, is_async_fn};
use cli::{Finding, Rule};
use rsparse::{close_of, items, word_positions, Src};
const RULES: &[Rule] = &[
Rule { id: "TK011", name: "client-per-request", severity: "medium",
why: "each reqwest::Client owns a connection pool and TLS config: building one per call throws both away",
fix: "build one Client at startup (with timeouts), store it in AppState, clone the handle (it is an Arc)" },
Rule { id: "TK012", name: "unwrap-on-await-in-handler", severity: "medium",
why: "a panic in a handler kills the request with a dropped connection and no useful status or log",
fix: "return Result<_, AppError> with IntoResponse and use `?`" },
Rule { id: "TK013", name: "outbound-without-timeout", severity: "low",
why: "reqwest has no request timeout by default: one hung upstream pins tasks and sockets forever",
fix: "Client::builder().timeout(..).connect_timeout(..), RequestBuilder::timeout, or tokio::time::timeout" },
Rule { id: "TK014", name: "no-graceful-shutdown", severity: "medium",
why: "without graceful shutdown SIGTERM drops in-flight requests and background work mid-way",
fix: "axum::serve(listener, app).with_graceful_shutdown(shutdown_signal()), then drain tasks (TaskTracker)" },
Rule { id: "TK017", name: "axum07-path-syntax", severity: "high",
why: "axum 0.8 changed path parameters to {id} / {*rest}; the old :id / *rest syntax panics at startup",
fix: "\"/orders/{id}\", \"/files/{*path}\"" },
];
const USAGE: &str = "usage: service_hygiene <FILES|DIRS|-> [options]
Report reqwest clients built per request (TK011), panicking .await.unwrap() in axum
handlers (TK012), outbound HTTP with no timeout anywhere in the file (TK013), axum::serve
without graceful shutdown (TK014) and axum 0.7 path syntax (TK017).";
fn main() {
let opts = cli::parse(USAGE, RULES, &[]);
let inputs = opts.inputs();
let mut findings = Vec::new();
for input in &inputs {
let src = Src::new(input.text.clone());
let code = &src.code;
let (fns, _, _) = items(&src);
let routed = handlers(&src, &fns);
let mut found = Vec::new();
let mut push = |off: usize, id: &'static str, what: String| {
found.push(Finding { file: input.name.clone(), line: src.line_of(off), id, msg: what, fix: String::new() });
};
let uses_reqwest = code.contains("reqwest");
let loops: Vec<(usize, usize)> = ["loop", "while", "for"]
.iter()
.flat_map(|kw| word_positions(code, kw))
.filter_map(|i| code[i..].find('{').map(|b| (i + b, close_of(code, i + b))))
.collect();
for pat in ["Client::new()", "Client::builder()"] {
for (off, _) in code.match_indices(pat) {
let reqwest_client = code[..off].ends_with("reqwest::") || uses_reqwest && !code[..off].ends_with("::");
let f = fn_at(&src, &fns, off);
let per_call = f.is_some_and(|f| is_async_fn(&src, f) && f.name != "main") || loops.iter().any(|&(a, b)| a < off && off < b);
if reqwest_client && per_call {
push(off, "TK011", format!("`{pat}` in {} builds a new pool per call", f.map_or("a loop".into(), |f| format!("fn {}", f.name))));
}
}
}
for f in fns.iter().filter(|f| routed.contains(&f.name) && is_async_fn(&src, f)) {
let squashed: String = f.body.split_whitespace().collect();
if squashed.contains(".await.unwrap()") || squashed.contains(".await.expect(") {
push(f.off, "TK012", format!("handler {} panics on a failed .await (unwrap/expect)", f.name));
}
}
if !code.contains("timeout(") {
let squashed: String = code.split_whitespace().collect();
if uses_reqwest && squashed.contains(".send().await") {
let off = code.find(".send()").unwrap_or(0);
push(off, "TK013", "outbound reqwest calls and no timeout anywhere in this file".into());
}
}
for (off, _) in code.match_indices("serve(") {
let open = off + "serve".len();
let after: String = code[close_of(code, open)..].split(';').next().unwrap_or("").split_whitespace().collect();
if code[..off].ends_with("axum::") && !after.contains("with_graceful_shutdown") {
push(off, "TK014", "axum::serve(..) has no .with_graceful_shutdown(..)".into());
}
}
for pat in [".route(", ".nest(", ".route_service(", ".nest_service("] {
for (off, _) in code.match_indices(pat) {
let open = off + pat.len() - 1;
let lit = src.orig[open + 1..close_of(code, open)].split(',').next().unwrap_or("").trim();
if lit.starts_with('"') && (lit.contains("/:") || lit.contains("/*")) {
push(off, "TK017", format!("route path {lit} uses axum 0.7 syntax"));
}
}
}
let on = |id: &str| RULES.iter().any(|r| r.id == id && opts.enabled(r));
findings.extend(found.into_iter().filter(|f| on(f.id) && !src.noqa(f.line, f.id)));
}
let findings: Vec<Finding> = findings
.into_iter()
.map(|mut f| {
f.fix = RULES.iter().find(|r| r.id == f.id).map_or(String::new(), |r| r.fix.to_string());
f
})
.collect();
cli::finish(&opts, RULES, findings, inputs.len(), "service")
}
rust-borrow-checker-error-explainer
Explain and fix Rust borrow-checker, ownership, move and lifetime compiler errors: E0382 use of moved value, E0499 cannot borrow as mutable more than once, E0502 also borrowed as immutable, E0505, E0506, E0503, E0507 cannot move out of index/Rc/reference, E0515 return reference to local, E0597 does not live long enough, E0716 temporary value dropped while borrowed, E0373 closure may outlive (spawn needs move), E0521 borrowed data escapes, E0384, E0596, E0594, E0381, E0713, 'lifetime may not live long enough', RefCell 'already borrowed: BorrowMutError' panics. Ranks fixes from restructuring (split borrows, entry API, split_at_mut, get_disjoint_mut, mem::take, Option::take, indices, thread::scope) down to clone and Rc<RefCell>/Arc<Mutex>; never unsafe. Covers NLL limits (get-or-insert), iterator invalidation, closure captures, self-referential structs. Ships std-only Rust triage tools.
rust-cargo-workspace-designer
Design, review and fix Cargo workspaces: when to split a crate (compile time, boundaries, publishing, proc-macros), crates/* vs apps/ + crates/ layouts, virtual manifest vs root package, members/exclude/default-members, resolver 2 vs 3 (MSRV-aware, edition 2024), [workspace.package] and [workspace.dependencies] inheritance, [workspace.lints], root-only profiles (lto, codegen-units), additive features with dep: and weak ?/ syntax, feature unification pitfalls, internal path + version deps, publish = false, Cargo.lock and MSRV policy, xtask, CI with --locked, nextest, rust-cache, sccache, cargo-hack/deny/machete/udeps/semver-checks, and cargo build --timings. Ships a workspace auditor and crate-graph tool.
rust-error-handling-thiserror-anyhow-designer
Design and review Rust error handling with thiserror 2 and anyhow 1: library vs application split, typed error enums with #[from], #[source], #[error(transparent)] and #[non_exhaustive], ErrorKind patterns, context via .context()/.with_context(), bail!/ensure!, source() chains, {:#} and {:?} reports, RUST_BACKTRACE/RUST_LIB_BACKTRACE, Box<dyn Error + Send + Sync>, main -> anyhow::Result or ExitCode, unwrap/expect/panic policy, Option to Result, collecting iterators of Results, axum IntoResponse mapping, retryable errors, tracing, async Send bounds, testing with matches! and downcast_ref, no_std core::error::Error, and snafu, eyre/color-eyre, miette, error-stack.
rust-lifetime-annotation-advisor
Decide when Rust lifetime annotations are needed and which ones: the three elision rules, structs holding references (struct Parser<'a>), owned vs borrowed (String, &str, Cow<'a, str>, Arc<str>), one vs several lifetimes, 'a: 'b bounds, T: 'static misconceptions, Box<dyn Trait + 'a> default object lifetimes, impl Trait + '_ and edition 2024 capture rules with use<..>, variance and PhantomData, HRTB for<'a> Fn(&'a T), GATs and lending iterators, async fn and tokio::spawn 'static, self-referential structs (ouroboros, self_cell, indices, arenas), Rc/Arc to escape lifetime soup. Ships std-only Rust checkers for redundant, missing, unused and over-restrictive lifetimes.
rust-trait-design-reviewer
Review and design Rust traits and generic APIs: generics vs impl Trait vs dyn Trait (static vs dynamic dispatch), dyn compatibility / object safety (generic methods, Self returns, where Self: Sized, error[E0038] not dyn compatible / cannot be made into an object), associated types vs generic parameters, blanket impls, coherence and orphan rule (E0117, E0119, E0210), newtype, extension, sealed and marker traits, supertraits and trait upcasting (1.86), async fn in traits (1.75) and the Send bound problem (trait_variant, async-trait), RPITIT, GATs, From/Into/TryFrom/AsRef/Borrow, std trait checklist (Debug, Clone, PartialEq, Hash, Default, Display, Error, Send/Sync, serde feature), #[must_use], #[non_exhaustive], semver hazards, Deref abuse, builders, typestate, PhantomData, Box<dyn Error + Send + Sync>, Fn traits, mockall.
rust-unsafe-block-auditor
Audit unsafe Rust for soundness: inventory every unsafe block, unsafe fn, unsafe impl/trait, extern block and unsafe attribute, check // SAFETY: comments and # Safety docs, and classify each as sound, needs-justification or likely UB, proposing safe alternatives first. Covers the five unsafe superpowers, aliasing (Stacked/Tree Borrows), invalid values, MaybeUninit vs mem::zeroed/uninitialized, transmute, slice::from_raw_parts, Vec::set_len/from_raw_parts, get_unchecked, CString::as_ptr dangling, unsafe impl Send/Sync, Pin projection, FFI (extern "C", repr(C), C-unwind, Box::into_raw/from_raw), strict provenance, static mut and &raw, edition 2024 (unsafe extern, #[unsafe(no_mangle)], unsafe_op_in_unsafe_fn, env::set_var), Miri, sanitizers, cargo-fuzz, loom, cargo-geiger and clippy safety lints.