rust-lang/rust-clippy
GitHub: rust-lang/rust-clippy
Rust 官方静态分析工具,通过超过 800 条 lint 规则捕捉代码错误、提升代码质量和性能。
Stars: 12933 | Forks: 1920
# Clippy
[](#license)
用于捕捉常见错误并改进您的 [Rust](https://github.com/rust-lang/rust) 代码的 Lint 集合。
[此 crate 中包含超过 800 个 Lint!](https://rust-lang.github.io/rust-clippy/master/index.html)
Lint 被分为不同类别,每个类别都有一个默认的 [lint 级别](https://doc.rust-lang.org/rustc/lints/levels.html)。
您可以通过按类别更改 lint 级别,来选择让 Clippy ~~ annoy ~~(打扰)帮助您的程度。
| Category | Description | Default level |
|-----------------------|-------------------------------------------------------------------------------------|---------------|
| `clippy::all` | all lints that are on by default (correctness, suspicious, style, complexity, perf) | **warn/deny** |
| `clippy::correctness` | code that is outright wrong or useless | **deny** |
| `clippy::suspicious` | code that is most likely wrong or useless | **warn** |
| `clippy::style` | code that should be written in a more idiomatic way | **warn** |
| `clippy::complexity` | code that does something simple but in a complex way | **warn** |
| `clippy::perf` | code that can be written to run faster | **warn** |
| `clippy::pedantic` | lints which are rather strict or have occasional false positives | allow |
| `clippy::restriction` | lints which prevent the use of language and library features[^restrict] | allow |
| `clippy::nursery` | new lints that are still under development | allow |
| `clippy::cargo` | lints for the cargo manifest | allow |
后续会有更多内容,如果您有想法,请[提交 issue](https://github.com/rust-lang/rust-clippy/issues)!
`restriction` 类别*绝对*不应作为一个整体被启用。其中包含的 Lint 可能会针对完全合理的代码进行警告,可能没有替代建议,并且可能与其他任何 Lint(包括其他类别)相冲突。在启用之前,应逐个考虑 Lint。
[^restrict]: `restriction` Lint 的一些用例包括:
- 严格的编码风格(例如 [`clippy::else_if_without_else`])。
- CI 上的额外限制(例如 [`clippy::todo`])。
- 防止在某些函数中发生 Panic(例如 [`clippy::unwrap_used`])。
- 仅在代码的子集上运行 Lint(例如在模块上使用 `#[forbid(clippy::float_arithmetic)]`)。
目录:
* [使用说明](#usage)
* [配置](#configuration)
* [贡献](#contributing)
* [许可证](#license)
## 使用说明
以下是如何将 Clippy 用作 cargo 子命令、在不使用 cargo 的项目中使用、或在 Travis CI 中使用的说明。
### 作为 cargo 子命令 (`cargo clippy`)
使用 Clippy 的一种方法是通过 rustup 将其作为 cargo 子命令安装。
#### 步骤 1:安装 Rustup
您可以在支持的平台上安装 [Rustup](https://rustup.rs/)。这将帮助我们安装 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
```
#### Workspaces
所有常用的 workspace 选项都应适用于 Clippy。例如,以下命令将在 `example` crate 上运行 Clippy:
```
cargo clippy -p example
```
与 `cargo check` 一样,这包括属于 workspace 成员的依赖项,例如路径依赖项。如果您**仅**想在给定的 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 clippy -- -D warnings
# in order to also check tests and non-default crate features, use
- cargo clippy --all-targets --all-features -- -D warnings
- cargo test
# etc.
```
请注意,添加 `-D warnings` 将导致如果您的代码中发现**任何**警告,构建就会失败。这包括 rustc 发现的警告(例如 `dead_code` 等)。如果您想避免这种情况,并且只针对 Clippy 警告导致错误,请在您的代码中使用 `#![deny(clippy::all)]` 或在命令行中使用 `-D clippy::all`。(您可以将 `clippy::all` 替换为您目标的特定 lint 类别。)
## 配置
### 允许/拒绝 Lint
您可以在代码中添加选项来 `allow`/`warn`/`deny` Clippy Lint:
* 使用 `clippy` lint 组(`#![deny(clippy::all)]`)覆盖所有 `Warn` 级别的 Lint 集合。请注意,`rustc` 有额外的 [lint 组](https://doc.rust-lang.org/rustc/lints/groups.html)。
* 同时使用 `clippy` 和 `clippy::pedantic` lint 组(`#![deny(clippy::all)]`,`#![deny(clippy::pedantic)]`)覆盖所有 Lint。请注意,`clippy::pedantic` 包含一些非常激进且容易出现误报的 Lint。
* 仅覆盖部分 Lint(`#![deny(clippy::single_match, clippy::box_vec)]` 等)。
* `allow`/`warn`/`deny` 可以通过 `#[allow(...)]` 等限制在单个函数或模块内。
注意:`allow` 意味着针对您的代码抑制该 lint。使用 `warn` 时,lint 在您的代码触发时只会发出警告,而使用 `deny` 时,lint 会发出错误。错误会导致 Clippy 以错误代码退出,这在 CI/CD 等脚本中非常有用。
如果您不想在代码中包含 lint 级别,可以在运行期间通过向 Clippy 传递额外的标志来全局启用/禁用 Lint:
要允许 `lint_name`,请运行
```
cargo clippy -- -A clippy::lint_name
```
要对 `lint_name` 发出警告,请运行
```
cargo clippy -- -W clippy::lint_name
```
这也适用于 lint 组。例如,您可以运行启用了所有 Lint 警告的 Clippy:
```
cargo clippy -- -W clippy::pedantic
```
如果您只关心单个 lint,您可以允许所有其他 lint,然后显式警告您感兴趣的 lint:
```
cargo clippy -- -A clippy::all -W clippy::useless_format -W clippy::...
```
### 配置某些 Lint 的行为
可以在名为 `clippy.toml` 或 `.clippy.toml` 的 TOML 文件中配置某些 Lint。它包含基本的 `variable = value` 映射,例如
```
avoid-breaking-exported-api = false
disallowed-names = ["toto", "tata", "titi"]
```
[配置表](https://doc.rust-lang.org/nightly/clippy/lint_configuration.html) 包含所有配置值、其默认值以及受其影响的 Lint 列表。每个[可配置 Lint](https://rust-lang.github.io/rust-clippy/master/index.html#Configuration) 也包含有关这些值的信息。
对于具有默认值的列表类型的配置,例如 [disallowed-names](https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_names),您可以使用特殊值 `".."` 来扩展默认值,而不是替换它们。
```
# disallowed-names 的 default 为 ["foo", "baz", "quux"]
disallowed-names = ["bar", ".."] # -> ["bar", "foo", "baz", "quux"]
```
要停用“有关更多信息,请访问 *lint-link*”消息,您可以定义 `CLIPPY_DISABLE_DOCS_LINKS` 环境变量。
### 指定最低支持的 Rust 版本
打算支持旧版本 Rust 的项目可以通过在 Clippy 配置文件中指定最低支持的 Rust 版本(MSRV)来禁用与较新功能相关的 Lint。
```
msrv = "1.30.0"
```
或者,可以使用 `Cargo.toml` 中的 [`rust-version` 字段](https://doc.rust-lang.org/cargo/reference/manifest.html#the-rust-version-field)。
```
# 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 可以在[这里](https://rust-lang.github.io/rust-clippy/master/index.html#msrv)找到。
## 贡献
如果您想为 Clippy 做贡献,可以在 [CONTRIBUTING.md](https://github.com/rust-lang/rust-clippy/blob/master/CONTRIBUTING.md) 中找到更多信息。
## 许可证
Copyright (c) The Rust Project Contributors
根据 Apache License, Version 2.0 或 MIT 许可证
授权,由您
选择。除非根据这些条款,否则不得复制、修改或分发项目中的文件。
标签:Bug检测, Clippy, Complexity, Correctness, DNS解析, IDE插件, Linter, LNA, pptx, Python安全, Rust, Style, 代码规范, 前端框架, 可视化界面, 威胁情报, 安全专业人员, 开发者工具, 开源项目, 性能优化, 检测绕过, 编程语言, 网络流量审计, 自动化审查, 通知系统, 通知系统, 错误基检测, 静态代码分析