ITADN
rust-lang/rust-clippy
rust-lang/rust-clippy · 文件 下载 ZIP
文件最后提交记录最后更新时间
README.md
以下内容由 AI 翻译,如有问题请点此提交 issue 反馈

Clippy

License: MIT OR Apache-2.0

一组用于捕获常见错误并改进 Rust 代码的 lints。

本 crate 包含超过 800 个 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较为严格或偶尔出现误报的 lintallow
clippy::restriction阻止使用语言和库功能的 lint1allow
clippy::nursery仍在开发中的新 lintallow
clippy::cargo针对 cargo manifest 的 lintallow

更多内容即将推出,如果您有想法,请提交 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:

  • 使用 clippy lint 组(#![deny(clippy::all)])来启用整套 Warn lints。 请注意,rustc 包含额外的 lint 组

  • 使用 clippyclippy::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

  1. restriction lint 的一些使用场景包括: