//! Find traits used as `dyn Trait` that are not dyn compatible (E0038), and public traits
//! with `async fn` whose futures callers cannot require to be `Send`.
//! TR001 dyn-incompatible-trait pub trait Storage { fn open(p: &str) -> Self; .. } + Box<dyn Storage>
//! TR008 async-fn-public-trait pub trait RemoteStore { async fn fetch(&self) -> T; }
//!
//! Build: rustc -O --edition 2021 scripts/dyn_compat.rs -o /tmp/dyn_compat
//! Usage: dyn_compat <FILES|DIRS|-> [--json] [--strict] [--select IDS] [--ignore IDS]
mod cli;
mod rsparse;
use cli::{Finding, Rule};
use rsparse::{attrs_before, dyn_uses, is_pub, items, split_top, traits, word_positions, FnSig, Src, TraitDef};
const RULES: &[Rule] = &[
Rule { id: "TR001", name: "dyn-incompatible-trait", severity: "high",
why: "the trait is used as `dyn Trait` but a method or item breaks dyn compatibility (E0038)",
fix: "add `where Self: Sized` to the offending methods, move them to an extension trait, or return Box<dyn ..> / boxed futures" },
Rule { id: "TR008", name: "async-fn-public-trait", severity: "medium",
why: "callers cannot require the future of a public trait's `async fn` to be Send (tokio::spawn needs it)",
fix: "#[trait_variant::make(SendX: Send)], or declare `fn f(..) -> impl Future<Output = T> + Send`" },
];
const USAGE: &str = "usage: dyn_compat <FILES|DIRS|-> [options]
For every local trait used as `dyn Trait` in the scanned files, list what breaks dyn
compatibility (E0038): Sized supertrait, associated consts, generic associated types,
methods without a receiver, generic methods, Self in parameters or return types,
async fn and -> impl Trait. Also flag `async fn` in public traits without a Send story.
Scan the whole crate: the `dyn` use may live in another file.";
/// Why one trait method is not dispatchable through `dyn`, or None.
fn method_problem(f: &FnSig) -> Option<String> {
if f.where_.replace(' ', "").contains("Self:Sized") {
return None; // explicitly opted out of the vtable
}
let params = split_top(&f.params);
let receiver = params.first().is_some_and(|p| {
let p = p.replace(' ', "");
p == "self" || p == "mutself" || p.starts_with("&self") || p.starts_with("&mutself") || p.starts_with("&'")
&& p.ends_with("self") || p.starts_with("self:")
});
let others = if receiver { ¶ms[1..] } else { ¶ms[..] };
let mentions_self = |s: &str| word_positions(s, "Self").iter().any(|&i| !s[i..].starts_with("Self::"));
if !receiver {
Some(format!("`{}` has no self receiver", f.name))
} else if f.generics.split(',').any(|g| !g.trim().is_empty() && !g.trim().starts_with('\'')) {
Some(format!("`{}` has type parameters", f.name))
} else if others.iter().any(|p| p.contains("impl ")) {
Some(format!("`{}` takes `impl Trait` (a hidden type parameter)", f.name))
} else if mentions_self(&f.ret) || others.iter().any(|p| mentions_self(p.split_once(':').map_or("", |x| x.1))) {
Some(format!("`{}` uses `Self` outside the receiver", f.name))
} else if f.ret.trim_start().starts_with("impl ") {
Some(format!("`{}` returns `impl Trait`", f.name))
} else {
None
}
}
fn check_trait(t: &TraitDef, fns: &[FnSig], src: &Src, used_as_dyn: bool, file: &str, out: &mut Vec<Finding>) {
let line = src.line_of(t.off);
let methods: Vec<&FnSig> = fns.iter().filter(|f| t.span.0 < f.off && f.off < t.span.1).collect();
let is_async = |f: &FnSig| src.code[..f.off].trim_end().ends_with("async");
if used_as_dyn {
let mut why = Vec::new();
if t.supers.split('+').any(|s| s.trim() == "Sized") {
why.push("it has a `Sized` supertrait".to_string());
}
if !word_positions(&t.body, "const").is_empty() {
why.push("it has associated consts".to_string());
}
if word_positions(&t.body, "type").iter().any(|&i| {
let rest = t.body[i + 4..].trim_start();
let name_end = rest.find(|c: char| !(c.is_alphanumeric() || c == '_')).unwrap_or(rest.len());
rest[name_end..].trim_start().starts_with('<')
}) {
why.push("it has a generic associated type".to_string());
}
for f in &methods {
if is_async(f) && !f.where_.replace(' ', "").contains("Self:Sized") {
why.push(format!("`{}` is an async fn", f.name));
} else if let Some(p) = method_problem(f) {
why.push(p);
}
}
if !why.is_empty() {
out.push(Finding { file: file.into(), line, id: "TR001",
msg: format!("trait {} is used as `dyn {}` but is not dyn compatible: {} (E0038)", t.name, t.name, why.join("; ")),
fix: "add `where Self: Sized` to constructors/generic helpers (they stay callable on concrete types), \
split them into an extension trait, or box: `-> Box<dyn Trait>`, `Pin<Box<dyn Future<..> + Send + '_>>`".into() });
}
}
let attrs = attrs_before(&src.code, t.off);
let has_send_story = attrs.contains("trait_variant") || attrs.contains("async_trait");
let async_methods: Vec<&str> = methods.iter().filter(|f| is_async(f)).map(|f| f.name.as_str()).collect();
if is_pub(&src.code, t.off) && !has_send_story && !async_methods.is_empty() {
out.push(Finding { file: file.into(), line, id: "TR008",
msg: format!("pub trait {} has async fn {}: callers cannot require the returned futures to be Send", t.name, async_methods.join(", ")),
fix: format!("#[trait_variant::make(Send{}: Send)] above the trait, or `fn {}(..) -> impl Future<Output = ..> + Send`",
t.name, async_methods[0]) });
}
}
fn main() {
let opts = cli::parse(USAGE, RULES, &[]);
let inputs = opts.inputs();
let parsed: Vec<Src> = inputs.iter().map(|i| Src::new(i.text.clone())).collect();
let dyn_names: Vec<String> = parsed.iter().flat_map(|s| dyn_uses(&s.code)).collect(); // crate-wide
let mut findings = Vec::new();
for (input, src) in inputs.iter().zip(&parsed) {
let (fns, _, _) = items(src);
let mut found = Vec::new();
for t in traits(src) {
check_trait(&t, &fns, src, dyn_names.contains(&t.name), &input.name, &mut found);
}
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(), "dyn-compatibility")
}
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-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-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.