//! Find values alive across `.await` that make futures !Send or hold locks too long.
//! TK006 sync-guard-across-await std MutexGuard / RwLock guard / RefCell borrow alive at a later .await
//! TK007 async-mutex-not-needed tokio::sync::Mutex guard never held across an .await (std Mutex is cheaper)
//! TK008 rc-in-async Rc inside async code (!Send: cannot be spawned on the multi-thread runtime)
//!
//! Build: rustc -O --edition 2021 scripts/await_holding.rs -o /tmp/await_holding
//! Usage: await_holding <FILES|DIRS|-> [--json] [--strict] [--select IDS] [--ignore IDS]
mod async_ctx;
mod cli;
mod rsparse;
use async_ctx::{fn_at, scope_end, AsyncCtx};
use cli::{Finding, Rule};
use rsparse::{items, word_positions, Src};
const RULES: &[Rule] = &[
Rule { id: "TK006", name: "sync-guard-across-await", severity: "high",
why: "a std guard alive at .await makes the future !Send (spawn fails) and blocks other tasks on the lock",
fix: "scope the guard: let v = { let g = m.lock().unwrap(); g.get(..).cloned() }; then .await; or drop(g) first" },
Rule { id: "TK007", name: "async-mutex-not-needed", severity: "low",
why: "tokio::sync::Mutex is slower than std::sync::Mutex and only needed when the guard lives across .await",
fix: "std::sync::Mutex (or parking_lot) for short critical sections without .await" },
Rule { id: "TK008", name: "rc-in-async", severity: "medium",
why: "Rc is !Send: a future holding it across .await cannot go to tokio::spawn on the multi-thread runtime",
fix: "Arc (and Mutex/atomics for mutation), or keep the Rc out of the awaited section" },
];
const USAGE: &str = "usage: await_holding <FILES|DIRS|-> [options]
Report std lock guards and RefCell borrows that are still alive at a later .await (TK006),
tokio Mutex guards that never live across an .await (TK007) and Rc used in async code (TK008).
A guard is a `let` binding of .lock()/.read()/.write()/.borrow()/.borrow_mut(); it lives until
drop(name) or the end of its block.";
/// Offset of the `;` that ends the statement starting at `i` (ignoring `;` nested in blocks).
fn statement_end(code: &str, i: usize) -> usize {
let mut depth = 0i32;
for (j, c) in code[i..].char_indices() {
match c {
'(' | '[' | '{' => depth += 1,
')' | ']' | '}' => depth -= 1,
';' if depth == 0 => return i + j,
_ => {}
}
if depth < 0 {
return i + j;
}
}
code.len()
}
/// (guard name, offset of the let, is it a tokio guard?) for `let [mut] g = ..lock()..;`
fn guards(code: &str, from: usize, to: usize) -> Vec<(String, usize, bool)> {
let mut out = Vec::new();
for i in word_positions(&code[from..to], "let").into_iter().map(|i| from + i) {
let stmt = &code[i..statement_end(code, i).min(to)];
let Some((lhs, rhs)) = stmt.split_once('=') else { continue };
let name = lhs.trim_start_matches("let").trim().trim_start_matches("mut ").trim();
if !name.chars().all(|c| c.is_alphanumeric() || c == '_') || name.is_empty() {
continue;
}
let rhs = rhs.replace([' ', '\n'], "");
let tokio = [".lock().await", ".read().await", ".write().await"].iter().any(|p| rhs.ends_with(p));
let std = [".lock().unwrap()", ".read().unwrap()", ".write().unwrap()", ".borrow()", ".borrow_mut()"]
.iter()
.any(|p| rhs.ends_with(p))
|| [".lock().expect(", ".read().expect(", ".write().expect("].iter().any(|p| rhs.contains(p) && rhs.ends_with(')'));
if tokio || std {
out.push((name.to_string(), i, tokio));
}
}
out
}
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 ctx = AsyncCtx::new(&src, &fns);
let mut found = Vec::new();
let mut seen = Vec::new();
for (name, off, tokio) in guards(code, 0, code.len()) {
let Some(span) = ctx.innermost(off).filter(|_| ctx.contains(off)) else { continue };
if seen.contains(&off) {
continue;
}
seen.push(off);
// The guard lives until drop(name) or the end of its block (whichever comes first).
let end = scope_end(code, off).min(span.1);
let dropped = code[off..end].find(&format!("drop({name})")).map(|n| off + n);
let alive_until = dropped.unwrap_or(end);
let stmt_end = statement_end(code, off);
let await_at = code[stmt_end..alive_until].find(".await").map(|n| stmt_end + n);
let place = fn_at(&src, &fns, off).map_or(String::new(), |f| format!(" in fn {}", f.name));
match (tokio, await_at) {
(false, Some(a)) => found.push(Finding {
file: input.name.clone(), line: src.line_of(off), id: "TK006",
msg: format!("std guard `{name}`{place} is still alive at the .await on line {}", src.line_of(a)),
fix: String::new() }),
(true, None) => found.push(Finding {
file: input.name.clone(), line: src.line_of(off), id: "TK007",
msg: format!("tokio Mutex guard `{name}`{place} is never held across an .await"),
fix: String::new() }),
_ => {}
}
}
let mut rc_fns = Vec::new();
for (off, _) in code.match_indices("Rc::") {
let word_start = off == 0 || !code.as_bytes()[off - 1].is_ascii_alphanumeric();
let f = fn_at(&src, &fns, off).map(|f| f.name.clone()).unwrap_or_default();
if word_start && ctx.contains(off) && !rc_fns.contains(&f) {
found.push(Finding { file: input.name.clone(), line: src.line_of(off), id: "TK008",
msg: format!("`Rc` in async fn {f}: the future is !Send"), fix: String::new() });
rc_fns.push(f);
}
}
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(), "await-holding")
}
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.