Skip to content
v0.1.1latest release34 skills2 categories4 sub-categories79 tested scripts157 reference docsContact us

elision_check.rs

Helper script for rust-lifetime-annotation-advisor · 142 lines

Programming / Rustv1.0.0Updated OCT 21 20267 references6 scripts
scripts/elision_check.rs
//! Find lifetime annotations that elision makes unnecessary, and print the elided form.
//!   LT001 needless-lifetime       fn first<'a>(s: &'a str) -> &'a str   ->  fn first(s: &str) -> &str
//!   LT002 elidable-impl-lifetime  impl<'a> Display for Token<'a>        ->  impl Display for Token<'_>
//!   LT005 unused-fn-lifetime      fn count<'x>(s: &str) -> usize        ->  fn count(s: &str) -> usize
//!
//! Build: rustc -O --edition 2021 scripts/elision_check.rs -o /tmp/elision_check
//! Usage: elision_check <FILES|DIRS|-> [--json] [--strict] [--select IDS] [--ignore IDS]

mod cli;
mod rsparse;

use cli::{Finding, Rule};
use rsparse::{count_lifetime, declared_lifetimes, elided_refs, items, lifetimes_in, one_line, strip_fn_sugar, word_positions, FnSig, Src};

const RULES: &[Rule] = &[
    Rule { id: "LT001", name: "needless-lifetime", severity: "low",
           why: "the elision rules already give this signature; named lifetimes add noise",
           fix: "delete the lifetime parameter and its uses (printed signature)" },
    Rule { id: "LT002", name: "elidable-impl-lifetime", severity: "low",
           why: "the impl's lifetime is used once in the header and never in the body",
           fix: "impl Trait for Type<'_> (impl-header elision, Rust 2018+)" },
    Rule { id: "LT005", name: "unused-fn-lifetime", severity: "medium",
           why: "a lifetime declared on a fn but used nowhere (clippy: extra_unused_lifetimes)",
           fix: "remove it from the generic list" },
];

const USAGE: &str = "usage: elision_check <FILES|DIRS|-> [options]

Report lifetime annotations that Rust's elision rules make unnecessary (LT001, LT002)
and fn lifetimes that are declared but never used (LT005), with the corrected
signature. Reads source text only; never compiles anything.";

/// Generic list without the given lifetimes; "" when nothing is left.
fn without(generics: &str, drop: &[String]) -> String {
    let kept: Vec<String> = rsparse::split_top(generics)
        .into_iter()
        .filter(|p| !drop.iter().any(|d| p == d || p.starts_with(&format!("{d}:"))))
        .collect();
    if kept.is_empty() { String::new() } else { format!("<{}>", kept.join(", ")) }
}

/// `&'a T` -> `&T`; other `'a` -> `'_`.
fn elide(s: &str, lts: &[String]) -> String {
    let mut out = s.to_string();
    for lt in lts {
        out = out.replace(&format!("&{lt} "), "&");
        for i in word_positions(&out.clone(), &lt[1..]).into_iter().rev() {
            if i > 0 && out.as_bytes()[i - 1] == b'\'' {
                out.replace_range(i - 1..i + lt.len() - 1, "'_");
            }
        }
    }
    out
}

/// Uses of `lt` outside its own declaration in the generic list (bounds like `'b: 'a`, `T: 'a`).
fn used_in_bounds(f: &FnSig, lt: &str) -> bool {
    count_lifetime(&f.generics, lt) > 1 || count_lifetime(&f.where_, lt) > 0
}

fn check_fn(f: &FnSig, src: &Src, file: &str, out: &mut Vec<Finding>) {
    let decl = declared_lifetimes(&f.generics);
    if decl.is_empty() {
        return;
    }
    let inputs = strip_fn_sugar(&f.params);
    let output = strip_fn_sugar(&f.ret);
    let line = src.line_of(f.off);
    let sig = |g: &str, p: &str, r: &str| {
        let ret = if r.is_empty() { String::new() } else { format!(" -> {r}") };
        one_line(&format!("fn {}{g}({p}){ret}", f.name))
    };

    let unused: Vec<String> = decl
        .iter()
        .filter(|lt| count_lifetime(&f.params, lt) + count_lifetime(&f.ret, lt) + count_lifetime(&f.body, lt) == 0 && !used_in_bounds(f, lt))
        .cloned()
        .collect();
    for lt in &unused {
        out.push(Finding { file: file.into(), line, id: "LT005", msg: format!("`{lt}` is declared on fn {} but never used", f.name),
                           fix: sig(&without(&f.generics, &unused), &f.params, &f.ret) });
    }
    if !unused.is_empty() || decl.iter().any(|lt| used_in_bounds(f, lt) || count_lifetime(&f.body, lt) > 0) {
        return;
    }

    // Every declared lifetime must be written exactly once among the inputs.
    if decl.iter().any(|lt| count_lifetime(&inputs, lt) != 1) {
        return;
    }
    let in_output: Vec<&String> = decl.iter().filter(|lt| count_lifetime(&output, lt) > 0).collect();
    let needless = match in_output.as_slice() {
        [] => true, // rule 1: each elided input gets its own lifetime anyway
        [lt] => {
            let receiver = inputs.trim_start();
            let by_ref_only = output.matches(&format!("&{lt}")).count() == count_lifetime(&output, lt);
            let rule3 = receiver.starts_with(&format!("&{lt} self")) || receiver.starts_with(&format!("&{lt} mut self"));
            let input_lifetimes = elided_refs(&inputs) + lifetimes_in(&inputs).len();
            by_ref_only && (rule3 || input_lifetimes == 1) // rule 3, or rule 2 (one input lifetime)
        }
        _ => false,
    };
    if needless {
        let names: Vec<String> = decl.iter().map(|l| format!("`{l}`")).collect();
        out.push(Finding { file: file.into(), line, id: "LT001",
                           msg: format!("{} on fn {} can be elided", names.join(", "), f.name),
                           fix: sig(&without(&f.generics, &decl), &elide(&f.params, &decl), &elide(&f.ret, &decl)) });
    }
}

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 (fns, _, impls) = items(&src);
        let mut found = Vec::new();
        for f in &fns {
            check_fn(f, &src, &input.name, &mut found);
        }
        for b in &impls {
            let decl = declared_lifetimes(&b.generics);
            let elidable: Vec<String> = decl
                .iter()
                .filter(|lt| count_lifetime(&b.header, lt) == 1 && count_lifetime(&b.generics, lt) == 1
                    && count_lifetime(&b.where_, lt) == 0 && count_lifetime(&b.body, lt) == 0)
                .cloned()
                .collect();
            if !elidable.is_empty() {
                let names: Vec<String> = elidable.iter().map(|l| format!("`{l}`")).collect();
                found.push(Finding { file: input.name.clone(), line: src.line_of(b.off), id: "LT002",
                                     msg: format!("{} is only used once in `impl {}`", names.join(", "), one_line(&b.header)),
                                     fix: one_line(&format!("impl{} {}", without(&b.generics, &elidable), elide(&b.header, &elidable))) });
            }
        }
        let rules_on = |id: &str| RULES.iter().any(|r| r.id == id && opts.enabled(r));
        findings.extend(found.into_iter().filter(|f| rules_on(f.id) && !src.noqa(f.line, f.id)));
    }
    cli::finish(&opts, RULES, findings, inputs.len(), "elision")
}
More in Rust

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-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.