Debian 上进行 Rust 代码审查的实操指南
一 环境准备与基础工具
rustup component add rustfmtrustup component add clippycargo build、cargo test、cargo bench。二 本地预检与提交前检查
cargo fmt 自动格式化;在 CI 中用 cargo fmt -- --check 作为门禁,拒绝未格式化提交。cargo clippy 发现风格、复杂度、性能与正确性问题;在 CI 中用 cargo clippy -- -D warnings 将警告视为错误,必要时加上 --all-targets --all-features 做全量检查。cargo test,确保新增/修改代码有对应测试,并覆盖正常路径与错误路径。cargo bench 建立基准并在 CI 中监控回归。三 代码审查清单要点
Box<dyn Error>),以免丢失错误类型信息。四 审查流程与 Git 工作流建议
五 Debian 上的 CI 最小示例
.github/workflows/rust-ci.yml):name: Rust CI
on: [push, pull_request]
jobs:
build-test-clippy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- name: Cache cargo dependencies
uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Check formatting
run: cargo fmt -- --check
- name: Run Clippy
run: cargo clippy --all-targets --all-features -- -D warnings
- name: Run tests
run: cargo test --all-targets --all-features
- name: Run benchmarks (informational)
run: cargo bench -- --no-run
fmt --check 与 clippy -D warnings 设为门禁,未通过则阻断合并。