关于 clippy-advanced
为 Rust 开发者提供 Clippy 配置指导,涵盖 lint 类别、自定义规则、禁用项及 IDE 集成,并说明 CI 检查方式。适用于建立和维护项目代码规范,不用于格式化、构建或测试。
主要功能
- 01配置 lint 类别与级别
- 02设置 clippy.toml 阈值和禁用项
- 03使用 Cargo 工作区 lint 配置
- 04配置 CI 检查与输出格式
- 05管理 lint 抑制规则
适用场景
- 01为 Rust 项目制定代码规范
- 02在 CI 中将 lint 警告视为错误
- 03按项目需要启用 pedantic 检查
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ laurigates/ claude-plugins --skill clippy-advanced安装后在 Claude Code 中输入/clippy-advanced确认已加载。
附带文件REFERENCE.md
详细介绍
clippy-advanced - 高级 Clippy 配置
用于全面 Rust lint 检查的高级 Clippy 配置,包括自定义规则、lint 类别、禁用方法和 IDE 集成。
何时使用此技能
| 在以下情况下使用此技能…… | 在以下情况下改用其他工具…… |
|---|---|
| 配置 Clippy lint 规则 | 格式化代码(使用 rustfmt) |
| 为 Rust 设置 CI lint 检查 | 构建/编译(使用 cargo build) |
| 自定义 clippy.toml | 运行测试(使用 cargo test) |
| 强制执行代码标准 | 管理依赖项(使用 cargo add) |
安装
# Clippy is included with rustup
rustup component add clippy
# Verify installation
cargo clippy --version
# Update clippy with rust toolchain
rustup update
基本用法
# Run clippy on current project
cargo clippy
# Run on all targets (lib, bins, tests, examples, benches)
cargo clippy --all-targets
# Run with all features enabled
cargo clippy --all-features
# Run on workspace
cargo clippy --workspace --all-targets --all-features
# Show detailed lint explanations
cargo clippy -- -W clippy::all -A clippy::pedantic
# Treat warnings as errors
cargo clippy -- -D warnings
Lint 类别
| 类别 | 用途 | 默认值 |
|---|---|---|
clippy::correctness | 可能存在的错误 | Deny |
clippy::complexity | 过于复杂的代码 | Warn |
clippy::perf | 性能问题 | Warn |
clippy::style | 代码风格 | Warn |
clippy::suspicious | 看起来有问题的代码 | Warn |
clippy::pedantic | 主观性较强的风格规则 | Off |
clippy::restriction | 选择性启用的约束 | Off |
clippy::nursery | 实验性规则 | Off |
clippy::cargo | Cargo.toml 问题 | Off |
推荐的 Cargo.toml 配置
[workspace.lints.clippy]
# Deny correctness issues (likely bugs)
correctness = "deny"
complexity = "warn"
perf = "warn"
style = "warn"
suspicious = "warn"
# Enable pedantic but allow some noisy lints
pedantic = "warn"
must_use_candidate = "allow"
missing_errors_doc = "allow"
# Enable some restriction lints selectively
clone_on_ref_ptr = "warn"
dbg_macro = "warn"
print_stdout = "warn"
todo = "warn"
unimplemented = "warn"
unwrap_used = "warn"
# Enable nursery lints (experimental)
use_self = "warn"
clippy.toml 必要设置
在项目根目录创建 clippy.toml,用于设置阈值和禁用项:
cognitive-complexity-threshold = 15
too-many-lines-threshold = 100
too-many-arguments-threshold = 5
disallowed-methods = [
{ path = "std::env::var", reason = "Use std::env::var_os for better Unicode handling" },
{ path = "std::process::exit", reason = "Use Result propagation instead" },
]
disallowed-names = ["foo", "bar", "baz"]
抑制 Lint
// Function-level
#[allow(clippy::too_many_arguments)]
fn complex_function(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) {}
// Module-level (src/lib.rs)
#![warn(clippy::all)]
#![warn(clippy::pedantic)]
#![deny(clippy::unwrap_used)]
#![allow(clippy::module_name_repetitions)]
// Inline
#[allow(clippy::cast_possible_truncation)]
let x = value as u8;
Agentic 优化
| 场景 | 命令 |
|---|---|
| CI 严格模式 | cargo clippy --workspace --all-targets -- -D warnings |
| JSON 输出 | cargo clippy --message-format=json |
| 精简错误信息 | cargo clippy --message-format=short |
| 快速检查 | cargo clippy --message-format=short -- -D warnings |
| Pedantic 检查 | cargo clippy -- -W clippy::pedantic -D clippy::correctness |
快速参考
命令行标志
| 标志 | 描述 |
|---|---|
--all-targets | 检查库、二进制文件、测试、示例和基准测试 |
--all-features | 启用所有功能 |
--workspace | 检查整个工作区 |
--message-format=json | 为工具提供 JSON 输出 |
--message-format=short | 精简错误格式 |
-- -D warnings | 将警告视为错误 |
-- -W clippy::pedantic | 启用 pedantic lint |
-- -A clippy::lint_name | 放行指定的 lint |
Lint 级别
| 级别 | 属性 | 效果 |
|---|---|---|
| Allow | #[allow(...)] | 抑制 lint |
| Warn | #[warn(...)] | 显示警告 |
| Deny | #[deny(...)] | 编译错误 |
参考资料
详细的配置示例、CI 集成、IDE 设置和最佳实践,请参阅 REFERENCE.md。
评论
支持 Markdown