unsafe-checker
Checks risky, low-level Rust code for hidden bugs that could crash a program or cause security problems.
Installation
Paste this into Claude Code, Cursor, or any agent that can run commands.
What this skill does
What it does:
This skill reviews the riskiest kind of Rust code, called unsafe code, which turns off some of the language's normal safety checks. This is sometimes needed for talking to code written in other languages like C, or for very high performance situations. The skill checks that risky code is actually necessary, properly explained with a comment describing why it's safe, and doesn't accidentally cause crashes, memory corruption, or other invisible bugs.
When to use it:
- Reviewing code that uses unsafe blocks, raw pointers, or connects Rust to another language
- Making sure risky code has a clear written explanation of why it's actually safe
- Checking for common mistakes like null pointer errors or using memory after it's been freed
- Learning what tools are recommended instead of outdated or risky techniques
This is a background reviewing tool, used automatically during code review of risky code, not a command typed by a person.
SKILL.mdShow the author's original SKILL.md
---
name: unsafe-checker
description: "CRITICAL: Use for unsafe Rust code review and FFI. Triggers on: unsafe, raw pointer, FFI, extern, transmute, *mut, *const, union, #[repr(C)], libc, std::ffi, MaybeUninit, NonNull, SAFETY comment, soundness, undefined behavior, UB, safe wrapper, memory layout, bindgen, cbindgen, CString, CStr, 安全抽象, 裸指针, 外部函数接口, 内存布局, 不安全代码, FFI 绑定, 未定义行为"
globs: ["**/*.rs"]
allowed-tools: ["Read", "Grep", "Glob"]
---
Display the following ASCII art exactly as shown. Do not modify spaces or line breaks:
```text
⚠️ **Unsafe Rust Checker Loaded**
* ^ *
/◉\_~^~_/◉\
⚡/ o \⚡
'_ _'
/ '-----' \
```
---
# Unsafe Rust Checker
## When Unsafe is Valid
| Use Case | Example |
|----------|---------|
| FFI | Calling C functions |
| Low-level abstractions | Implementing `Vec`, `Arc` |
| Performance | Measured bottleneck with safe alternative too slow |
**NOT valid:** Escaping borrow checker without understanding why.
## Required Documentation
```rust
// SAFETY: <why this is safe>
unsafe { ... }
/// # Safety
/// <caller requirements>
pub unsafe fn dangerous() { ... }
```
## Quick Reference
| Operation | Safety Requirements |
|-----------|---------------------|
| `*ptr` deref | Valid, aligned, initialized |
| `&*ptr` | + No aliasing violations |
| `transmute` | Same size, valid bit pattern |
| `extern "C"` | Correct signature, ABI |
| `static mut` | Synchronization guaranteed |
| `impl Send/Sync` | Actually thread-safe |
## Common Errors
| Error | Fix |
|-------|-----|
| Null pointer deref | Check for null before deref |
| Use after free | Ensure lifetime validity |
| Data race | Add proper synchronization |
| Alignment violation | Use `#[repr(C)]`, check alignment |
| Invalid bit pattern | Use `MaybeUninit` |
| Missing SAFETY comment | Add `// SAFETY:` |
## Deprecated → Better
| Deprecated | Use Instead |
|------------|-------------|
| `mem::uninitialized()` | `MaybeUninit<T>` |
| `mem::zeroed()` for refs | `MaybeUninit<T>` |
| Raw pointer arithmetic | `NonNull<T>`, `ptr::add` |
| `CString::new().unwrap().as_ptr()` | Store `CString` first |
| `static mut` | `AtomicT` or `Mutex` |
| Manual extern | `bindgen` |
## FFI Crates
| Direction | Crate |
|-----------|-------|
| C → Rust | bindgen |
| Rust → C | cbindgen |
| Python | PyO3 |
| Node.js | napi-rs |
Claude knows unsafe Rust. Focus on SAFETY comments and soundness.
Ships with 56 supporting files:
- AGENTS.md
- checklists/before-unsafe.md
- checklists/common-pitfalls.md
- checklists/review-unsafe.md
- examples/ffi-patterns.md
- examples/safe-abstraction.md
- metadata.json
- rules/_sections.md
- rules/_template.md
- rules/ffi-01-no-string-direct.md
- rules/ffi-02-read-ffi-docs.md
- rules/ffi-03-drop-for-c-ptr.md
- rules/ffi-04-panic-boundary.md
- rules/ffi-05-portable-types.md
- rules/ffi-06-string-abi.md
- rules/ffi-07-no-drop-external.md
- rules/ffi-08-error-handling.md
- rules/ffi-09-ref-not-ptr.md
- rules/ffi-10-thread-safety.md
- rules/ffi-11-packed-ub.md
- … and 36 more
Mirrored from the author's public source. Install counts from the open skills registry.