//! Check how error types are declared and exposed.
//! ERR004 box-dyn-error-not-send Box<dyn Error> without Send + Sync (fn main is exempt)
//! ERR005 anyhow-in-lib-api pub fn in library code returning anyhow::Result / anyhow::Error
//! ERR008 string-error-type Result<T, String>, Result<T, &str>, Result<T, ()>
//! ERR009 source-printed-twice #[error("..{0}")] on a #[from]/#[source] field (chain prints it twice)
//! ERR010 missing-non-exhaustive pub error enum in library code without #[non_exhaustive]
//!
//! Build: rustc -O --edition 2021 scripts/error_types.rs -o /tmp/error_types
//! Usage: error_types <FILES|DIRS|-> [--lib|--app] [--include-tests] [--json] [--strict]
mod cli;
mod rsparse;
mod scope;
use cli::{Finding, Rule};
use rsparse::{attrs_before, base_name, is_pub, items, one_line, split_top, word_positions, Src, TypeDef};
use scope::Scope;
const RULES: &[Rule] = &[
Rule { id: "ERR004", name: "box-dyn-error-not-send", severity: "medium",
why: "Box<dyn Error> without Send + Sync cannot cross threads or tokio::spawn and does not convert into anyhow::Error",
fix: "Box<dyn std::error::Error + Send + Sync + 'static>, or a concrete error type" },
Rule { id: "ERR005", name: "anyhow-in-lib-api", severity: "high",
why: "anyhow::Error in a library API forces callers to downcast to handle specific failures",
fix: "a thiserror enum for the public API; keep anyhow for binaries and internal glue" },
Rule { id: "ERR008", name: "string-error-type", severity: "medium",
why: "String/&str/() errors cannot be matched, carry no source and do not implement Error",
fix: "a typed error (thiserror enum), or anyhow::Error in application code" },
Rule { id: "ERR009", name: "source-printed-twice", severity: "medium",
why: "Display must not include the source if source() returns it: reports print the cause twice",
fix: "describe only this layer in #[error(..)]; the reporter walks source() for the cause" },
Rule { id: "ERR010", name: "missing-non-exhaustive", severity: "info",
why: "adding a variant to an exhaustive public error enum is a breaking change",
fix: "#[non_exhaustive] on public library error enums" },
];
const USAGE: &str = "usage: error_types <FILES|DIRS|-> [options]
Report Box<dyn Error> without Send + Sync (ERR004), anyhow in public library signatures
(ERR005), String/&str/() as error types (ERR008), thiserror messages that print their own
source (ERR009) and exhaustive public library error enums (ERR010).";
/// Error type of `Result<T, E>` in `ret` (None for one-argument aliases such as `Result<T>`).
fn result_error(ret: &str) -> Option<String> {
let i = word_positions(ret, "Result").into_iter().find(|&i| ret[i + 6..].trim_start().starts_with('<'))?;
let open = i + 6 + (ret[i + 6..].len() - ret[i + 6..].trim_start().len());
let inner = &ret[open + 1..rsparse::close_of(ret, open) - 1];
split_top(inner).get(1).cloned()
}
/// ERR009: thiserror variants whose message names their own source field.
fn twice_printed(t: &TypeDef, src: &Src) -> Vec<(usize, String)> {
let mut out = Vec::new();
let mut pos = 0;
for v in split_top(&t.body) {
let off = t.body_off + t.body[pos..].find(v.as_str()).map_or(0, |n| pos + n);
pos = off - t.body_off + v.len();
let orig = &src.orig[off..off + v.len()];
let Some(e) = orig.find("#[error(") else { continue };
let msg_end = orig[e..].find(")]").map_or(orig.len(), |n| e + n);
let msg = &orig[e + 8..msg_end];
if msg.trim() == "transparent" {
continue;
}
// Fields after the variant name: (tuple) or {named}.
let code_v = &src.code[off..off + v.len()];
let fields_at = code_v.rfind(|c| c == '(' || c == '{').filter(|&i| !code_v[..i].trim_end().ends_with("error"));
let Some(fa) = fields_at.or_else(|| code_v.find(['(', '{']).filter(|&i| i > msg_end - e)) else { continue };
let named = code_v.as_bytes()[fa] == b'{';
let variant = code_v[..fa].rsplit([']', ' ', '\n']).find(|w| !w.trim().is_empty()).unwrap_or("").trim().to_string();
let body = &code_v[fa + 1..rsparse::close_of(code_v, fa) - 1];
for (i, field) in split_top(body).iter().enumerate() {
let is_source = field.contains("#[from]") || field.contains("#[source]")
|| (named && field.trim_start().starts_with("source") && field.contains(':'));
if !is_source {
continue;
}
let name = if named {
field.rsplit(']').next().unwrap_or(field).split(':').next().unwrap_or("").trim().to_string()
} else {
i.to_string()
};
if msg.contains(&format!("{{{name}}}")) || msg.contains(&format!("{{{name}:")) {
out.push((off, format!("{variant}: message {} prints `{{{name}}}`, which is also the error's source()", one_line(msg))));
}
}
}
out
}
fn main() {
let usage = format!("{USAGE}\n\n{}", scope::SCOPE_HELP);
let opts = cli::parse(&usage, RULES, &[], scope::SWITCHES);
let inputs = opts.inputs();
let mut findings = Vec::new();
for input in &inputs {
let src = Src::new(input.text.clone());
let (fns, types, impls) = items(&src);
let scope = Scope::new(input, &src, &fns, &opts);
let mut found = Vec::new();
let mut push = |off: usize, id: &'static str, msg: String, fix: &str| {
if !scope.skip(off) {
found.push(Finding { file: input.name.clone(), line: src.line_of(off), id, msg, fix: fix.into() });
}
};
let anyhow_result = src.code.contains("use anyhow::Result") || src.code.contains("use anyhow::{") && src.code.contains("Result");
for f in &fns {
let sig = format!("{} {}", f.params, f.ret);
for i in word_positions(&sig, "dyn") {
let obj = one_line(sig[i..].split(['>', ')', ',']).next().unwrap_or(""));
if base_name(obj.trim_start_matches("dyn ").split('+').next().unwrap_or("")) == "Error"
&& sig[..i].trim_end().ends_with("Box<") && !obj.contains("Send") && f.name != "main" {
push(f.off, "ERR004", format!("fn {} uses `Box<{obj}>` without Send + Sync", f.name),
"Box<dyn std::error::Error + Send + Sync + 'static>");
}
}
let in_trait_impl = f.impl_idx.is_some_and(|i| impls[i].trait_and_self().0.is_some());
if !scope.app && is_pub(&src.code, f.off) && !in_trait_impl
&& (f.ret.contains("anyhow::") || anyhow_result && result_error(&f.ret).is_none() && f.ret.contains("Result<")) {
push(f.off, "ERR005", format!("pub fn {} returns anyhow in library code: `{}`", f.name, one_line(&f.ret)),
"return a thiserror enum; convert to anyhow in the binary");
}
if let Some(e) = result_error(&f.ret) {
let e = e.replace(' ', "");
if ["String", "&str", "&'staticstr", "()"].contains(&e.as_str()) {
push(f.off, "ERR008", format!("fn {} returns `{}`: the error is a {}", f.name, one_line(&f.ret), if e == "()" { "unit" } else { "string" }),
"a thiserror enum (library) or anyhow::Result (application)");
}
}
}
for t in types.iter().filter(|t| t.kind == "enum") {
let attrs = attrs_before(&src.code, t.off);
let is_error = t.name.ends_with("Error") || attrs.contains("Error");
if !is_error {
continue;
}
for (off, msg) in twice_printed(t, &src) {
push(off, "ERR009", format!("{}::{msg}", t.name), "drop the placeholder: #[error(\"failed to ..\")]");
}
if !scope.app && is_pub(&src.code, t.off) && !attrs.contains("non_exhaustive") {
push(t.off, "ERR010", format!("pub enum {} can never gain a variant without a major release", t.name), "#[non_exhaustive]");
}
}
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)));
}
cli::finish(&opts, RULES, findings, inputs.len(), "error type")
}
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-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-tokio-async-service-builder
Build and review production async Rust services on Tokio 1.x and axum 0.8: runtime choice (#[tokio::main] multi_thread vs current_thread, Builder), axum routing with /{id} paths, State, extractors, IntoResponse error types, axum::serve with_graceful_shutdown, tower/tower-http middleware (TraceLayer, TimeoutLayer, CorsLayer, ConcurrencyLimitLayer, RequestBodyLimitLayer), Arc<AppState>, std vs tokio Mutex, JoinSet, TaskTracker, CancellationToken, select! cancel safety, bounded mpsc, oneshot, broadcast, watch, actors, Semaphore, timeouts, retries, spawn_blocking, sqlx PgPool, reqwest Client reuse, tracing, tests with start_paused and oneshot, Dockerfile. Fixes 'future cannot be sent between threads safely', E0373, Rc or MutexGuard held across await, 'Cannot start a runtime from within a runtime', 'Path segments must not start with :'. Ships std-only Rust scanners and a compiling axum 0.8 scaffold.
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.