unsafe-checker
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDisplay the following ASCII art exactly as shown. Do not modify spaces or line breaks:
text
⚠️ **Unsafe Rust Checker Loaded**
* ^ *
/◉\_~^~_/◉\
⚡/ o \⚡
'_ _'
/ '-----' \请完全按照以下ASCII艺术图显示,不要修改空格或换行:
text
⚠️ **Unsafe Rust Checker Loaded**
* ^ *
/◉\_~^~_/◉\
⚡/ o \⚡
'_ _'
/ '-----' \Unsafe Rust Checker
Unsafe Rust 检查器
When Unsafe is Valid
何时可以合法使用Unsafe
| Use Case | Example |
|---|---|
| FFI | Calling C functions |
| Low-level abstractions | Implementing |
| Performance | Measured bottleneck with safe alternative too slow |
NOT valid: Escaping borrow checker without understanding why.
| 使用场景 | 示例 |
|---|---|
| FFI | 调用C函数 |
| 底层抽象 | 实现 |
| 性能优化 | 经测试安全替代方案性能不足导致瓶颈 |
不合法场景: 不理解原因就绕过借用检查器。
Required Documentation
必备文档规范
rust
// SAFETY: <why this is safe>
unsafe { ... }
/// # Safety
/// <caller requirements>
pub unsafe fn dangerous() { ... }rust
// SAFETY: <说明此处安全的原因>
unsafe { ... }
/// # 安全性
/// <调用方需满足的要求>
pub unsafe fn dangerous() { ... }Quick Reference
速查表
| Operation | Safety Requirements |
|---|---|
| Valid, aligned, initialized |
| + No aliasing violations |
| Same size, valid bit pattern |
| Correct signature, ABI |
| Synchronization guaranteed |
| Actually thread-safe |
| 操作 | 安全要求 |
|---|---|
| 指针有效、对齐、已初始化 |
| + 无别名冲突 |
| 大小相同、位模式合法 |
| 签名正确、ABI匹配 |
| 保证同步 |
| 确实线程安全 |
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 |
| Invalid bit pattern | Use |
| Missing SAFETY comment | Add |
| 错误类型 | 修复方案 |
|---|---|
| 空指针解引用 | 解引用前检查是否为空 |
| 释放后使用 | 确保生命周期有效 |
| 数据竞争 | 添加适当的同步机制 |
| 对齐违规 | 使用 |
| 无效位模式 | 使用 |
| 缺少SAFETY注释 | 添加 |
Deprecated → Better
已弃用方案 → 替代方案
| Deprecated | Use Instead |
|---|---|
| |
| |
| Raw pointer arithmetic | |
| Store |
| |
| Manual extern | |
| 已弃用方案 | 替代方案 |
|---|---|
| |
针对引用的 | |
| 裸指针运算 | |
| 先存储 |
| |
| 手动编写extern | |
FFI Crates
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.
| 交互方向 | Crate名称 |
|---|---|
| C → Rust | bindgen |
| Rust → C | cbindgen |
| Python | PyO3 |
| Node.js | napi-rs |
Claude 精通Unsafe Rust,重点关注SAFETY注释和代码健全性。