Skills rust-code-review
Reviews Rust code for ownership, borrowing, lifetime, error handling, trait design, unsafe usage, and common mistakes. Use when reviewing .rs files, checking borrow checker issues, error handling patterns, or trait implementations. Covers Rust 2024 edition patterns and modern idioms.
install
source · Clone the upstream repo
git clone https://github.com/openclaw/skills
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/openclaw/skills "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/anderskev/rust-code-review" ~/.claude/skills/openclaw-skills-rust-code-review && rm -rf "$T"
OpenClaw · Install into ~/.openclaw/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/openclaw/skills "$T" && mkdir -p ~/.openclaw/skills && cp -r "$T/skills/anderskev/rust-code-review" ~/.openclaw/skills/openclaw-skills-rust-code-review && rm -rf "$T"
manifest:
skills/anderskev/rust-code-review/SKILL.mdsource content
Rust Code Review
Review Workflow
Follow this sequence to avoid false positives and catch edition-specific issues:
- Check
— Note the Rust edition (2018, 2021, 2024) and MSRV if set. Edition 2024 introduces breaking changes to unsafe semantics, RPIT lifetime capture, temporary scoping, andCargo.toml
type fallback. This determines which patterns apply. Check workspace structure if present.! - Check dependencies — Note key crates (thiserror vs anyhow, tokio features, serde features). These inform which patterns are expected.
- Scan changed files — Read full functions, not just diffs. Many Rust bugs hide in ownership flow across a function.
- Check each category — Work through the checklist below, loading references as needed.
- Verify before reporting — Load beagle-rust:review-verification-protocol before submitting findings.
Output Format
Report findings as:
[FILE:LINE] ISSUE_TITLE Severity: Critical | Major | Minor | Informational Description of the issue and why it matters.
Quick Reference
| Issue Type | Reference |
|---|---|
| Ownership transfers, borrowing, lifetimes, clone traps, iterators | references/ownership-borrowing.md |
| Lifetime variance, covariance/invariance, memory regions | references/lifetime-variance.md |
| Result/Option handling, thiserror, anyhow, error context, Error trait | references/error-handling.md |
| Async pitfalls, Send/Sync bounds, runtime blocking | references/async-concurrency.md |
| Send/Sync semantics, atomics, memory ordering, lock patterns | references/concurrency-primitives.md |
| Type layout, alignment, repr, PhantomData, generics vs dyn Trait | references/types-layout.md |
| Unsafe code, API design, derive patterns, clippy patterns | references/common-mistakes.md |
| Safety contracts, raw pointers, MaybeUninit, soundness, Miri | references/unsafe-deep.md |
For development guidance on performance, pointer types, type state, clippy config, iterators, generics, and documentation, use the
skill.beagle-rust:rust-best-practices
Review Checklist
Ownership and Borrowing
- No unnecessary
to silence the borrow checker (hiding design issues).clone() - No
inside loops — prefer.clone()
or.cloned()
on iterators.copied() - No cloning to avoid lifetime annotations (take ownership explicitly or restructure)
- References have appropriate lifetimes (not overly broad
when shorter lifetime works)'static - Edition 2024: RPIT (
) captures all in-scope lifetimes by default; use-> impl Trait
for precise capture control+ use<'a> -
preferred over&str
,String
over&[T]
in function parametersVec<T> -
orimpl AsRef<T>
used for flexible API parametersInto<T> - No dangling references or use-after-move
- Interior mutability (
,Cell
,RefCell
) used only when shared mutation is genuinely neededMutex - Small types (≤24 bytes) derive
and are passed by valueCopy -
used when ownership is ambiguousCow<'_, T> - Iterator chains preferred over index-based loops for collection transforms
- No premature
— pass iterators directly when the consumer accepts them.collect() -
preferred over.sum()
for summation (compiler optimizes better).fold() -
variants used when fallbacks involve allocation_or_else - Edition 2024:
temporaries drop at end of theif let
— code relying on temporaries living through the else branch needs restructuringif let - Edition 2024:
implementsBox<[T]>
— prefer direct iteration overIntoIterator
firstinto_vec()
Error Handling
-
used for recoverable errors, notResult<T, E>
/panic!
/unwrapexpect - Error types provide context (thiserror with
or manual#[error("...")]
)Display -
operator used with proper?
implementations orFrom.map_err() -
/unwrap()
only in tests, examples, or provably-safe contextsexpect() - Error variants are specific enough to be actionable by callers
-
used in applications,anyhow
in libraries (or clear rationale for alternatives)thiserror -
variants used when fallbacks involve allocation (_or_else
,ok_or_else
)unwrap_or_else -
used for early returns on failure (let-else
)let Ok(x) = expr else { return ... } -
used for error logging,inspect_err
for error transformationmap_err
Traits and Types
- Traits are minimal and cohesive (single responsibility)
-
macros appropriate for the type (derive
,Clone
,Debug
used correctly)PartialEq - Newtypes used to prevent primitive obsession (e.g.,
not barestruct UserId(Uuid)
)Uuid -
/From
implementations are lossless and infallible;Into
for fallible conversionsTryFrom - Sealed traits used when external implementations shouldn't be allowed
- Default implementations provided where they make sense
-
bounds verified for types shared across threadsSend + Sync -
used on public traits to provide clear error messages when users forget to implement them#[diagnostic::on_unimplemented]
Unsafe Code
-
blocks have safety comments explaining invariantsunsafe -
is minimal — only the truly unsafe operation is inside the blockunsafe - Safety invariants are documented and upheld by surrounding safe code
- No undefined behavior (null pointer deref, data races, invalid memory access)
-
trait implementations justify why the contract is upheldunsafe - Edition 2024:
bodies use explicitunsafe fn
blocks around unsafe ops (unsafe {}
is deny)unsafe_op_in_unsafe_fn - Edition 2024:
blocks written asextern "C" {}unsafe extern "C" {} - Edition 2024:
and#[no_mangle]
written as#[export_name]
and#[unsafe(no_mangle)]#[unsafe(export_name)]
Naming and Style
- Types are
, functions/methodsPascalCase
, constantssnake_caseSCREAMING_SNAKE_CASE - Modules use
snake_case -
,is_
,has_
prefixes for boolean-returning methodscan_ - Builder pattern methods take and return
(notself
) for chaining&mut self - Public items have doc comments (
)/// -
on functions where ignoring the return value is likely a bug#[must_use] - Imports ordered: std → external crates → workspace → crate/super
-
preferred over#[expect(clippy::...)]
for lint suppression#[allow(...)]
Performance
Detailed guidance:
skill (references/performance.md)beagle-rust:rust-best-practices
- No unnecessary allocations in hot paths (prefer
over&str
,String
over&[T]
)Vec<T> -
type is specified or inferablecollect() - Iterators preferred over indexed loops for collection transforms
-
used when size is knownVec::with_capacity() - No redundant
/.to_string()
chains.to_owned() - No intermediate
when passing iterators directly works.collect() -
preferred over.sum()
for summation.fold() - Static dispatch (
) used over dynamic (impl Trait
) unless flexibility requireddyn Trait
Clippy Configuration
Detailed guidance:
skill (references/clippy-config.md)beagle-rust:rust-best-practices
- Workspace-level lints configured in
(Cargo.toml
or[workspace.lints.clippy]
)[lints.clippy] -
used over#[expect(clippy::lint)]
— warns when suppression becomes stale#[allow(...)] - Justification comment present when suppressing any lint
- Key lints enforced:
,redundant_clone
,large_enum_variant
,needless_collect
groupperf -
passescargo clippy --all-targets --all-features -- -D warnings - Doc lints enabled for library crates (
,missing_docs
)broken_intra_doc_links
Type State Pattern
Detailed guidance:
skill (references/type-state-pattern.md)beagle-rust:rust-best-practices
-
used for zero-cost compile-time state machines (not runtime enums/booleans)PhantomData<State> - State transitions consume
and return new state type (prevents reuse of old state)self - Only applicable methods available per state (invalid operations are compile errors)
- Pattern used where it adds safety value (builders with required fields, connection states, workflows)
- Not overused for trivial state (simple enums are fine when runtime flexibility needed)
Severity Calibration
Critical (Block Merge)
code with unsound invariants or undefined behaviorunsafe- Use-after-free or dangling reference patterns
on user input or external data in production codeunwrap()- Data races (concurrent mutation without synchronization)
- Memory leaks via circular
without weak referencesArc<Mutex<...>>
Major (Should Fix)
- Errors returned without context (bare
equivalent)return err
masking ownership design issues in hot paths.clone()- Missing
/Send
bounds on types used across threadsSync
for recoverable errors in library codepanic!- Overly broad
lifetimes hiding API design issues'static
Minor (Consider Fixing)
- Missing doc comments on public items
parameter whereString
or&str
would workimpl AsRef<str>- Derive macros missing for types that should have them
- Unused feature flags in
Cargo.toml - Suboptimal iterator chains (multiple allocations where one suffices)
Informational (Note Only)
- Suggestions to introduce newtypes for domain modeling
- Refactoring ideas for trait design
- Performance optimizations without measured impact
- Suggestions to add
or#[must_use]#[non_exhaustive]
When to Load References
- Reviewing ownership, borrows, lifetimes, clone traps → ownership-borrowing.md
- Reviewing lifetime variance, covariance/invariance, multiple lifetime params → lifetime-variance.md
- Reviewing Result/Option handling, error types, Error trait impls → error-handling.md
- Reviewing async code, tokio usage, task management → async-concurrency.md
- Reviewing Send/Sync, atomics, memory ordering, mutexes, lock patterns → concurrency-primitives.md
- Reviewing type layout, alignment, repr, PhantomData, generics vs dyn → types-layout.md
- Reviewing unsafe code, API design, derive macros, clippy patterns → common-mistakes.md
- Reviewing safety contracts, raw pointers, MaybeUninit, soundness → unsafe-deep.md
- Reviewing performance, pointer types, type state, generics, iterators, documentation →
skillbeagle-rust:rust-best-practices
Valid Patterns (Do NOT Flag)
These are acceptable Rust patterns — reporting them wastes developer time:
in tests — Clarity over performance in test code.clone()
in tests and examples — Acceptable where panicking on failure is intentionalunwrap()
in simple binaries — Not every application needs custom error typesBox<dyn Error>
fields in structs — Owned data in structs is correct;String
fields require lifetime parameters&str
during development — Common during iteration#[allow(dead_code)]
/todo!()
in new code — Valid placeholder during active developmentunimplemented!()
with clear message — Self-documenting and acceptable for invariants.expect("reason")
in test modules — Standard pattern foruse super::*
modules#[cfg(test)]- Type aliases for complex types —
is idiomatictype Result<T> = std::result::Result<T, MyError>
in return position — Zero-cost abstraction, standard patternimpl Trait- Turbofish syntax —
is idiomatic when type inference needs helpcollect::<Vec<_>>()
prefix for intentionally unused variables — Compiler convention_
with justification — Self-cleaning lint suppression#[expect(clippy::...)]
— Explicit Arc cloning is idiomatic and recommendedArc::clone(&arc)
for short critical sections in async — Tokio docs recommend thisstd::sync::Mutex
loops over iterators — When early exit or side effects are neededfor
in trait definitions — Stable since 1.75;async fn
crate only needed forasync-trait
or pre-1.75 MSRVdyn Trait
/LazyCell
from std — Stable since 1.80; replacesLazyLock
andonce_cell
for new codelazy_static
precise capture syntax — Edition 2024 syntax for controlling RPIT lifetime capture+ use<'a, T>
Context-Sensitive Rules
Only flag these issues when the specific conditions apply:
| Issue | Flag ONLY IF |
|---|---|
| Missing error context | Error crosses module boundary without context |
Unnecessary | In hot path or repeated call, not test/setup code |
| Missing doc comments | Item is and not in a module |
usage | In production code path, not test/example/provably-safe |
Missing | Type is actually shared across thread/task boundaries |
| Overly broad lifetime | A shorter lifetime would work AND the API is public |
Missing | Function returns a value that callers commonly ignore |
Stale suppression | Should be for self-cleaning lint management |
Missing derive | Type is ≤24 bytes with all-Copy fields and used frequently |
Edition 2024: type fallback | Match on or diverging expressions where fallback was assumed — now falls back to not |
Edition 2024: identifier | Code uses as an identifier — must be in edition 2024 (reserved keyword) |
Before Submitting Findings
Load and follow
beagle-rust:review-verification-protocol before reporting any issue.