open-code-review icon indicating copy to clipboard operation
open-code-review copied to clipboard

feat(rules): add built-in Rust and Cargo.toml review rules

Open MuoDoo opened this issue 5 months ago • 0 comments

Problem Statement

当前内置 review rules 没有针对 Rust 源码和 Cargo manifest 的专项规则。

.rs 和 .toml 文件虽然属于可评审文件类型,但 Rust 代码目前会落到通用默认规则,难以稳定覆盖 Rust 特有风险,例如:

  • 可恢复错误中不应直接使用 unwrap() / expect() / panic!
  • ownership、lifetime、borrow、clone 误用
  • unsafe 边界和安全不变量缺失
  • sync lock 跨 .await、任务取消和并发共享状态问题
  • Cargo.toml 中 edition、MSRV、resolver、feature flags、依赖版本和发布 metadata 问题

这会导致 Rust 项目的评审质量不如 Java、TypeScript、Kotlin、C/C++ 等已有专项规则覆盖的语言。

Proposed Solution

新增 Rust 和 Cargo.toml 的内置评审规则:

  1. 为 **/*.rs 增加 Rust 专项 rule,覆盖:

    • typo / naming
    • ownership and lifetime correctness
    • error handling and panics,尤其是生产代码或 library 代码中尽量避免可恢复错误直接 unwrap() / expect()
    • unsafe code boundaries
    • concurrency and shared state
    • async and cancellation safety
    • collections, iterators, and performance
    • type and API design
    • security-sensitive code
  2. 为 **/cargo.toml 增加 Cargo manifest 专项 rule,覆盖:

    • dependency version hygiene
    • git dependency reproducibility
    • edition / rust-version / resolver
    • feature flags
    • package metadata and release packaging
  3. 补充默认规则匹配测试,确保:

    • src/lib.rs 命中 Rust rule
    • crates/service/src/main.rs 命中 Rust rule
    • crates/service/Cargo.toml 命中 Cargo manifest rule

Alternatives Considered

  • 继续使用 default rule:实现成本最低,但无法稳定提示 Rust 特有问题。
  • 只添加 Rust 源码规则,不添加 Cargo.toml 规则:可以覆盖代码问题,但会遗漏 Rust 项目中常见的 manifest 配置风险。
  • 在用户项目中通过 .opencodereview/rule.json 自定义:适合局部定制,但通用 Rust 支持更适合作为内置默认能力。

Affected Area

Review Rules

Additional Context

已搜索现有 issues,未发现 Rust / Cargo.toml / unwrap 相关的重复 issue。

相关但不重复的 issue:

  • #35:讨论高优先级 rule 与低优先级 rule 的合并机制
  • #47:支持 HarmonyOS .ets 文件,属于另一个语言支持场景

MuoDoo avatar Jun 08 '26 07:06 MuoDoo