1# Clippy 2 3[/badge.svg?branch=auto&event=push)](https://github.com/rust-lang/rust-clippy/actions?query=workflow%3A%22Clippy+Test+(bors)%22+event%3Apush+branch%3Aauto) 4[](#license) 5 6A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code. 7 8[There are over 700 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html) 9 10Lints are divided into categories, each with a default [lint level](https://doc.rust-lang.org/rustc/lints/levels.html). 11You can choose how much Clippy is supposed to ~~annoy~~ help you by changing the lint level by category. 12 13| Category | Description | Default level | 14|-----------------------|-------------------------------------------------------------------------------------|---------------| 15| `clippy::all` | all lints that are on by default (correctness, suspicious, style, complexity, perf) | **warn/deny** | 16| `clippy::correctness` | code that is outright wrong or useless | **deny** | 17| `clippy::suspicious` | code that is most likely wrong or useless | **warn** | 18| `clippy::style` | code that should be written in a more idiomatic way | **warn** | 19| `clippy::complexity` | code that does something simple but in a complex way | **warn** | 20| `clippy::perf` | code that can be written to run faster | **warn** | 21| `clippy::pedantic` | lints which are rather strict or have occasional false positives | allow | 22| `clippy::restriction` | lints which prevent the use of language and library features[^restrict] | allow | 23| `clippy::nursery` | new lints that are still under development | allow | 24| `clippy::cargo` | lints for the cargo manifest | allow | 25 26More to come, please [file an issue](https://github.com/rust-lang/rust-clippy/issues) if you have ideas! 27 28The `restriction` category should, *emphatically*, not be enabled as a whole. The contained 29lints may lint against perfectly reasonable code, may not have an alternative suggestion, 30and may contradict any other lints (including other categories). Lints should be considered 31on a case-by-case basis before enabling. 32 33[^restrict]: Some use cases for `restriction` lints include: 34 - Strict coding styles (e.g. [`clippy::else_if_without_else`]). 35 - Additional restrictions on CI (e.g. [`clippy::todo`]). 36 - Preventing panicking in certain functions (e.g. [`clippy::unwrap_used`]). 37 - Running a lint only on a subset of code (e.g. `#[forbid(clippy::float_arithmetic)]` on a module). 38 39[`clippy::else_if_without_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#else_if_without_else 40[`clippy::todo`]: https://rust-lang.github.io/rust-clippy/master/index.html#todo 41[`clippy::unwrap_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used 42 43--- 44 45Table of contents: 46 47* [Usage instructions](#usage) 48* [Configuration](#configuration) 49* [Contributing](#contributing) 50* [License](#license) 51 52## Usage 53 54Below are instructions on how to use Clippy as a cargo subcommand, 55in projects that do not use cargo, or in Travis CI. 56 57### As a cargo subcommand (`cargo clippy`) 58 59One way to use Clippy is by installing Clippy through rustup as a cargo 60subcommand. 61 62#### Step 1: Install Rustup 63 64You can install [Rustup](https://rustup.rs/) on supported platforms. This will help 65us install Clippy and its dependencies. 66 67If you already have Rustup installed, update to ensure you have the latest 68Rustup and compiler: 69 70```terminal 71rustup update 72``` 73 74#### Step 2: Install Clippy 75 76Once you have rustup and the latest stable release (at least Rust 1.29) installed, run the following command: 77 78```terminal 79rustup component add clippy 80``` 81 82If it says that it can't find the `clippy` component, please run `rustup self update`. 83 84#### Step 3: Run Clippy 85 86Now you can run Clippy by invoking the following command: 87 88```terminal 89cargo clippy 90``` 91 92#### Automatically applying Clippy suggestions 93 94Clippy can automatically apply some lint suggestions, just like the compiler. Note that `--fix` implies 95`--all-targets`, so it can fix as much code as it can. 96 97```terminal 98cargo clippy --fix 99``` 100 101#### Workspaces 102 103All the usual workspace options should work with Clippy. For example the following command 104will run Clippy on the `example` crate: 105 106```terminal 107cargo clippy -p example 108``` 109 110As with `cargo check`, this includes dependencies that are members of the workspace, like path dependencies. 111If you want to run Clippy **only** on the given crate, use the `--no-deps` option like this: 112 113```terminal 114cargo clippy -p example -- --no-deps 115``` 116 117### Using `clippy-driver` 118 119Clippy can also be used in projects that do not use cargo. To do so, run `clippy-driver` 120with the same arguments you use for `rustc`. For example: 121 122```terminal 123clippy-driver --edition 2018 -Cpanic=abort foo.rs 124``` 125 126Note that `clippy-driver` is designed for running Clippy only and should not be used as a general 127replacement for `rustc`. `clippy-driver` may produce artifacts that are not optimized as expected, 128for example. 129 130### Travis CI 131 132You can add Clippy to Travis CI in the same way you use it locally: 133 134```yaml 135language: rust 136rust: 137 - stable 138 - beta 139before_script: 140 - rustup component add clippy 141script: 142 - cargo clippy 143 # if you want the build job to fail when encountering warnings, use 144 - cargo clippy -- -D warnings 145 # in order to also check tests and non-default crate features, use 146 - cargo clippy --all-targets --all-features -- -D warnings 147 - cargo test 148 # etc. 149``` 150 151Note that adding `-D warnings` will cause your build to fail if **any** warnings are found in your code. 152That includes warnings found by rustc (e.g. `dead_code`, etc.). If you want to avoid this and only cause 153an error for Clippy warnings, use `#![deny(clippy::all)]` in your code or `-D clippy::all` on the command 154line. (You can swap `clippy::all` with the specific lint category you are targeting.) 155 156## Configuration 157 158### Allowing/denying lints 159 160You can add options to your code to `allow`/`warn`/`deny` Clippy lints: 161 162* the whole set of `Warn` lints using the `clippy` lint group (`#![deny(clippy::all)]`). 163 Note that `rustc` has additional [lint groups](https://doc.rust-lang.org/rustc/lints/groups.html). 164 165* all lints using both the `clippy` and `clippy::pedantic` lint groups (`#![deny(clippy::all)]`, 166 `#![deny(clippy::pedantic)]`). Note that `clippy::pedantic` contains some very aggressive 167 lints prone to false positives. 168 169* only some lints (`#![deny(clippy::single_match, clippy::box_vec)]`, etc.) 170 171* `allow`/`warn`/`deny` can be limited to a single function or module using `#[allow(...)]`, etc. 172 173Note: `allow` means to suppress the lint for your code. With `warn` the lint 174will only emit a warning, while with `deny` the lint will emit an error, when 175triggering for your code. An error causes Clippy to exit with an error code, so 176is useful in scripts like CI/CD. 177 178If you do not want to include your lint levels in your code, you can globally 179enable/disable lints by passing extra flags to Clippy during the run: 180 181To allow `lint_name`, run 182 183```terminal 184cargo clippy -- -A clippy::lint_name 185``` 186 187And to warn on `lint_name`, run 188 189```terminal 190cargo clippy -- -W clippy::lint_name 191``` 192 193This also works with lint groups. For example, you 194can run Clippy with warnings for all lints enabled: 195 196```terminal 197cargo clippy -- -W clippy::pedantic 198``` 199 200If you care only about a single lint, you can allow all others and then explicitly warn on 201the lint(s) you are interested in: 202 203```terminal 204cargo clippy -- -A clippy::all -W clippy::useless_format -W clippy::... 205``` 206 207### Configure the behavior of some lints 208 209Some lints can be configured in a TOML file named `clippy.toml` or `.clippy.toml`. It contains a basic `variable = 210value` mapping e.g. 211 212```toml 213avoid-breaking-exported-api = false 214disallowed-names = ["toto", "tata", "titi"] 215``` 216 217The [table of configurations](https://doc.rust-lang.org/nightly/clippy/lint_configuration.html) 218contains all config values, their default, and a list of lints they affect. 219Each [configurable lint](https://rust-lang.github.io/rust-clippy/master/index.html#Configuration) 220, also contains information about these values. 221 222For configurations that are a list type with default values such as 223[disallowed-names](https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_names), 224you can use the unique value `".."` to extend the default values instead of replacing them. 225 226```toml 227# default of disallowed-names is ["foo", "baz", "quux"] 228disallowed-names = ["bar", ".."] # -> ["bar", "foo", "baz", "quux"] 229``` 230 231> **Note** 232> 233> `clippy.toml` or `.clippy.toml` cannot be used to allow/deny lints. 234 235To deactivate the “for further information visit *lint-link*” message you can 236define the `CLIPPY_DISABLE_DOCS_LINKS` environment variable. 237 238### Specifying the minimum supported Rust version 239 240Projects that intend to support old versions of Rust can disable lints pertaining to newer features by 241specifying the minimum supported Rust version (MSRV) in the Clippy configuration file. 242 243```toml 244msrv = "1.30.0" 245``` 246 247Alternatively, the [`rust-version` field](https://doc.rust-lang.org/cargo/reference/manifest.html#the-rust-version-field) 248in the `Cargo.toml` can be used. 249 250```toml 251# Cargo.toml 252rust-version = "1.30" 253``` 254 255The MSRV can also be specified as an attribute, like below. 256 257```rust,ignore 258#![feature(custom_inner_attributes)] 259#![clippy::msrv = "1.30.0"] 260 261fn main() { 262 ... 263} 264``` 265 266You can also omit the patch version when specifying the MSRV, so `msrv = 1.30` 267is equivalent to `msrv = 1.30.0`. 268 269Note: `custom_inner_attributes` is an unstable feature, so it has to be enabled explicitly. 270 271Lints that recognize this configuration option can be found [here](https://rust-lang.github.io/rust-clippy/master/index.html#msrv) 272 273## Contributing 274 275If you want to contribute to Clippy, you can find more information in [CONTRIBUTING.md](https://github.com/rust-lang/rust-clippy/blob/master/CONTRIBUTING.md). 276 277## License 278 279<!-- REUSE-IgnoreStart --> 280 281Copyright 2014-2024 The Rust Project Developers 282 283Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or 284[https://www.apache.org/licenses/LICENSE-2.0](https://www.apache.org/licenses/LICENSE-2.0)> or the MIT license 285<LICENSE-MIT or [https://opensource.org/licenses/MIT](https://opensource.org/licenses/MIT)>, at your 286option. Files in the project may not be 287copied, modified, or distributed except according to those terms. 288 289<!-- REUSE-IgnoreEnd --> 290