| name | unsafe-checker |
| description | Unsafe Rust code reviewer. Activates when encountering unsafe blocks, FFI code, raw pointers, transmute, MaybeUninit, or when reviewing code for memory safety. Use explicitly with /unsafe-checker to audit unsafe code. |
Unsafe Rust Checker
SAFETY Comment Requirement
Every unsafe block MUST have a // SAFETY: comment explaining why the invariants are upheld:
unsafe { &*ptr }
Unsafe Review Checklist
For each unsafe block, verify:
Deprecated → Modern Replacements
| Deprecated | Use Instead | Why |
|---|
mem::uninitialized() | MaybeUninit<T> | UB for any type with invalid values |
mem::zeroed() (non-zero types) | MaybeUninit<T> | UB for NonZero*, references, etc. |
transmute between integer sizes | as cast or try_into() | Safer, clearer intent |
transmute for &T ↔ &U | bytemuck::cast_ref | Verified at compile time |
slice::from_raw_parts unchecked | Validate length + alignment first | Prevents buffer overflows |
FFI Guidelines
Crate Recommendations
| Task | Crate |
|---|
| C header → Rust bindings | bindgen |
| Rust → C header | cbindgen |
| Python bindings | PyO3 |
| Node.js bindings | napi-rs |
| WebAssembly | wasm-bindgen |
FFI Function Checklist
Common FFI Patterns
#[no_mangle]
pub extern "C" fn my_func(ptr: *const u8, len: usize) -> i32 {
let result = std::panic::catch_unwind(|| {
let slice = unsafe { std::slice::from_raw_parts(ptr, len) };
process(slice)
});
match result {
Ok(Ok(())) => 0,
Ok(Err(_)) => -1,
Err(_) => -2,
}
}
Red Flags in Unsafe Code
transmute without size/alignment validation
- Raw pointer arithmetic without bounds checking
unsafe impl Send/Sync without justification
unsafe blocks with no SAFETY comment
- Casting function pointers
union types without clear invariant documentation