Clippy
一组用于捕获常见错误并改进 Rust 代码的 lints。
Lints 被划分为不同的类别,每个类别都有一个默认的 lint level。
你可以通过更改每个类别的 lint level 来选择 Clippy 应该在多大程度上 打扰 帮助你。
| 类别 | 描述 | 默认级别 |
|---|---|---|
clippy::all | 所有默认开启的 lint(correctness、suspicious、style、complexity、perf) | warn/deny |
clippy::correctness | 明显错误或无用的代码 | deny |
clippy::suspicious | 很可能错误或无用的代码 | warn |
clippy::style | 应该以更惯用的方式编写的代码 | warn |
clippy::complexity | 做了简单事情但方式复杂的代码 | warn |
clippy::perf | 可以编写得运行更快的代码 | warn |
clippy::pedantic | 较为严格或偶尔出现误报的 lint | allow |
clippy::restriction | 阻止使用语言和库功能的 lint1 | allow |
clippy::nursery | 仍在开发中的新 lint | allow |
clippy::cargo | 针对 cargo manifest 的 lint | allow |
更多内容即将推出,如果您有想法,请提交 issue!
restriction 类别不应强烈地整体启用。其中包含的
lint 可能会针对完全合理的代码进行 lint,可能没有替代建议,
并且可能与其他 lint(包括其他类别)相矛盾。在启用之前,应
逐个考虑这些 lint。
目录:
使用
以下是如何使用 Clippy 作为 cargo 子命令的说明, 适用于不使用 cargo 的项目,或在 Travis CI 中使用。
作为 cargo 子命令(cargo clippy)
使用 Clippy 的一种方法是通过 rustup 安装 Clippy 作为 cargo 子命令。
步骤 1:安装 Rustup
您可以在支持的平台上安装 Rustup。这将帮助 我们安装 Clippy 及其依赖项。
如果您已经安装了 Rustup,请更新以确保拥有最新的 Rustup 和编译器:
rustup update
步骤 2:安装 Clippy
一旦你已安装 rustup 和最新稳定版(至少 Rust 1.29),请运行以下命令:
rustup component add clippy
如果提示找不到 clippy 组件,请运行 rustup self update。
步骤 3:运行 Clippy
现在您可以通过调用以下命令来运行 Clippy:
cargo clippy
自动应用 Clippy 建议
Clippy 可以自动应用一些 lint 建议,就像编译器一样。请注意,--fix 意味着
--all-targets,因此它可以修复尽可能多的代码。
cargo clippy --fix
工作区
所有常规的工作区选项都应适用于 Clippy。例如,以下命令
将在 example crate 上运行 Clippy:
cargo clippy -p example
与 cargo check 一样,这包括工作区的成员依赖项,例如路径依赖项。
如果你只想对给定的 crate 运行 Clippy,请像这样使用 --no-deps 选项:
cargo clippy -p example -- --no-deps
使用 clippy-driver
Clippy 也可以用于不使用 cargo 的项目。为此,请使用与 rustc 相同的参数运行 clippy-driver。例如:
clippy-driver --edition 2018 -Cpanic=abort foo.rs
请注意,clippy-driver 专为仅运行 Clippy 而设计,不应作为 rustc 的通用
替代品。例如,clippy-driver 可能会生成未按预期优化的产物。
Travis CI
你可以像在本地使用一样,将 Clippy 添加到 Travis CI 中:
language: rust
rust:
- stable
- beta
before_script:
- rustup component add clippy
script:
- cargo clippy
# if you want the build job to fail when encountering warnings, use
- CARGO_BUILD_WARNINGS=deny cargo clippy
# in order to also check tests and non-default crate features, use
- CARGO_BUILD_WARNINGS=deny cargo clippy --all-targets --all-features
- cargo test
# etc.
请注意,设置 CARGO_BUILD_WARNINGS=deny 会导致如果代码中发现任何警告,您的构建将失败。
这包括由 rustc 发现的警告(例如 dead_code 等)。如果您想避免这种情况,并仅针对 Clippy 警告
引发错误,请在代码中使用 #![deny(clippy::all)] 或在命令
行中使用 -D clippy::all。(您可以将 clippy::all 替换为您针对的具体 lint 类别。)
另外请注意,在 Cargo 1.97 之前,拒绝所有警告的通常方法是使用 -D warnings:
cargo clippy -- -D warnings
然而,CARGO_BUILD_WARNINGS 具有不使构建缓存失效的优势,
因此今后应优先采用。
配置
允许/拒绝 lints
你可以在代码中添加选项,以 allow/warn/deny Clippy lints:
-
使用
clippylint 组(#![deny(clippy::all)])来启用整套Warnlints。 请注意,rustc包含额外的 lint 组。 -
使用
clippy和clippy::pedantic两个 lint 组(#![deny(clippy::all)],#![deny(clippy::pedantic)])来启用所有 lints。请注意,clippy::pedantic包含一些非常激进且 容易产生误报的 lints。 -
仅启用部分 lints(
#![deny(clippy::single_match, clippy::box_vec)]等) -
allow/warn/deny可以使用#[allow(...)]等限制在单个函数或模块中。
注意:allow 表示抑制你代码中的 lint。使用 warn 时,lint
仅会发出警告,而使用 deny 时,lint 会发出错误,当
在你的代码中触发时。错误会导致 Clippy 以错误代码退出,因此
在 CI/CD 等脚本中非常有用。
如果你不想在代码中包含你的 lint 级别,你可以在运行期间通过向 Clippy 传递额外标志来全局 启用/禁用 lints:
要允许 lint_name,请运行
cargo clippy -- -A clippy::lint_name
并且要在 lint_name 上发出警告,请运行
cargo clippy -- -W clippy::lint_name
这也适用于 lint 组。例如,你可以运行 Clippy,并启用所有 lint 的警告:
cargo clippy -- -W clippy::pedantic
如果你只关心单个 lint,你可以允许所有其他 lint,然后显式地对你感兴趣的 lint 发出警告:
cargo clippy -- -A clippy::all -W clippy::useless_format -W clippy::...
配置某些 lint 的行为
某些 lint 可以在名为 clippy.toml 或 .clippy.toml 的 TOML 文件中配置。它包含基本的 variable = value 映射,例如
avoid-breaking-exported-api = false
disallowed-names = ["toto", "tata", "titi"]
配置表 包含所有配置值、其默认值,以及它们影响的 lint 列表。 每个可配置 lint 也包含关于这些值的信息。
对于具有默认值的列表类型配置,例如
disallowed-names,
你可以使用唯一值 ".." 来扩展默认值,而不是替换它们。
# default of disallowed-names is ["foo", "baz", "quux"]
disallowed-names = ["bar", ".."] # -> ["bar", "foo", "baz", "quux"]
注意
clippy.toml或.clippy.toml不能用于允许/拒绝 lints。
若要停用“for further information visit lint-link”消息,您可以
定义 CLIPPY_DISABLE_DOCS_LINKS 环境变量。
指定最低支持的 Rust 版本
旨在支持旧版本 Rust 的项目可以通过在 Clippy 配置文件中 指定最低支持的 Rust 版本(MSRV)来禁用与较新特性相关的 lints。
msrv = "1.30.0"
或者,可以使用 Cargo.toml 中的 rust-version 字段
# Cargo.toml
rust-version = "1.30"
MSRV 也可以像下面这样指定为属性。
#![feature(custom_inner_attributes)]
#![clippy::msrv = "1.30.0"]
fn main() {
...
}
在指定 MSRV 时,您也可以省略补丁版本,因此 msrv = 1.30
等同于 msrv = 1.30.0。
注意:custom_inner_attributes 是一个不稳定特性,因此必须显式启用。
识别此配置选项的 Lint 可以在此处
贡献
如果您想为 Clippy 做出贡献,可以在 CONTRIBUTING.md 中找到更多信息。
许可证
Copyright (c) The Rust Project Contributors
根据 Apache License, Version 2.0 <LICENSE-APACHE 或 https://www.apache.org/licenses/LICENSE-2.0> 或 MIT 许可证 <LICENSE-MIT or https://opensource.org/licenses/MIT>,由您 选择。项目中的文件不得 复制、修改或分发,除非根据这些条款。
Footnotes
-
restrictionlint 的一些使用场景包括:- 严格的编码风格(例如
clippy::else_if_without_else)。 - CI 上的额外限制(例如
clippy::todo)。 - 防止某些函数中发生 panic(例如
clippy::unwrap_used)。 - 仅在代码子集上运行 lint(例如在模块上使用
#[forbid(clippy::float_arithmetic)])。
- 严格的编码风格(例如