- Notifications
You must be signed in to change notification settings - Fork1.9k
A bunch of lints to catch common mistakes and improve your Rust code. Book:https://doc.rust-lang.org/clippy/
License
Apache-2.0, MIT licenses found
Licenses found
rust-lang/rust-clippy
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A collection of lints to catch common mistakes and improve yourRust code.
There are over 800 lints included in this crate!
Lints are divided into categories, each with a defaultlint level.You can choose how much Clippy is supposed toannoy help you by changing the lint level by category.
| 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 features1 | allow |
clippy::nursery | new lints that are still under development | allow |
clippy::cargo | lints for the cargo manifest | allow |
More to come, pleasefile an issue if you have ideas!
Therestriction category should,emphatically, not be enabled as a whole. The containedlints may lint against perfectly reasonable code, may not have an alternative suggestion,and may contradict any other lints (including other categories). Lints should be consideredon a case-by-case basis before enabling.
Table of contents:
Below are instructions on how to use Clippy as a cargo subcommand,in projects that do not use cargo, or in Travis CI.
One way to use Clippy is by installing Clippy through rustup as a cargosubcommand.
You can installRustup on supported platforms. This will helpus install Clippy and its dependencies.
If you already have Rustup installed, update to ensure you have the latestRustup and compiler:
rustup updateOnce you have rustup and the latest stable release (at least Rust 1.29) installed, run the following command:
rustup component add clippyIf it says that it can't find theclippy component, please runrustup self update.
Now you can run Clippy by invoking the following command:
cargo clippyClippy can automatically apply some lint suggestions, just like the compiler. Note that--fix implies--all-targets, so it can fix as much code as it can.
cargo clippy --fixAll the usual workspace options should work with Clippy. For example the following commandwill run Clippy on theexample crate:
cargo clippy -p exampleAs withcargo check, this includes dependencies that are members of the workspace, like path dependencies.If you want to run Clippyonly on the given crate, use the--no-deps option like this:
cargo clippy -p example -- --no-depsClippy can also be used in projects that do not use cargo. To do so, runclippy-driverwith the same arguments you use forrustc. For example:
clippy-driver --edition 2018 -Cpanic=abort foo.rsNote thatclippy-driver is designed for running Clippy only and should not be used as a generalreplacement forrustc.clippy-driver may produce artifacts that are not optimized as expected,for example.
You can add Clippy to Travis CI in the same way you use it locally:
language:rustrust: -stable -betabefore_script: -rustup component add clippyscript: -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.
Note that adding-D warnings will cause your build to fail ifany warnings are found in your code.That includes warnings found by rustc (e.g.dead_code, etc.). If you want to avoid this and only causean error for Clippy warnings, use#![deny(clippy::all)] in your code or-D clippy::all on the commandline. (You can swapclippy::all with the specific lint category you are targeting.)
You can add options to your code toallow/warn/deny Clippy lints:
the whole set of
Warnlints using theclippylint group (#![deny(clippy::all)]).Note thatrustchas additionallint groups.all lints using both the
clippyandclippy::pedanticlint groups (#![deny(clippy::all)],#![deny(clippy::pedantic)]). Note thatclippy::pedanticcontains some very aggressivelints prone to false positives.only some lints (
#![deny(clippy::single_match, clippy::box_vec)], etc.)allow/warn/denycan be limited to a single function or module using#[allow(...)], etc.
Note:allow means to suppress the lint for your code. Withwarn the lintwill only emit a warning, while withdeny the lint will emit an error, whentriggering for your code. An error causes Clippy to exit with an error code, sois useful in scripts like CI/CD.
If you do not want to include your lint levels in your code, you can globallyenable/disable lints by passing extra flags to Clippy during the run:
To allowlint_name, run
cargo clippy -- -A clippy::lint_nameAnd to warn onlint_name, run
cargo clippy -- -W clippy::lint_nameThis also works with lint groups. For example, youcan run Clippy with warnings for all lints enabled:
cargo clippy -- -W clippy::pedanticIf you care only about a single lint, you can allow all others and then explicitly warn onthe lint(s) you are interested in:
cargo clippy -- -A clippy::all -W clippy::useless_format -W clippy::...Some lints can be configured in a TOML file namedclippy.toml or.clippy.toml. It contains a basicvariable = value mapping e.g.
avoid-breaking-exported-api =falsedisallowed-names = ["toto","tata","titi"]
Thetable of configurationscontains all config values, their default, and a list of lints they affect.Eachconfigurable lint, also contains information about these values.
For configurations that are a list type with default values such asdisallowed-names,you can use the unique value".." to extend the default values instead of replacing them.
# default of disallowed-names is ["foo", "baz", "quux"]disallowed-names = ["bar",".."]# -> ["bar", "foo", "baz", "quux"]
Note
clippy.tomlor.clippy.tomlcannot be used to allow/deny lints.
To deactivate the “for further information visitlint-link” message you candefine theCLIPPY_DISABLE_DOCS_LINKS environment variable.
Projects that intend to support old versions of Rust can disable lints pertaining to newer features byspecifying the minimum supported Rust version (MSRV) in the Clippy configuration file.
msrv ="1.30.0"
Alternatively, therust-version fieldin theCargo.toml can be used.
# Cargo.tomlrust-version ="1.30"
The MSRV can also be specified as an attribute, like below.
#![feature(custom_inner_attributes)]#![clippy::msrv ="1.30.0"]fnmain(){ ...}
You can also omit the patch version when specifying the MSRV, somsrv = 1.30is equivalent tomsrv = 1.30.0.
Note:custom_inner_attributes is an unstable feature, so it has to be enabled explicitly.
Lints that recognize this configuration option can be foundhere
If you want to contribute to Clippy, you can find more information inCONTRIBUTING.md.
Copyright 2014-2025 The Rust Project Developers
Licensed under the Apache License, Version 2.0 <LICENSE-APACHE orhttps://www.apache.org/licenses/LICENSE-2.0> or the MIT license<LICENSE-MIT orhttps://opensource.org/licenses/MIT>, at youroption. Files in the project may not becopied, modified, or distributed except according to those terms.
Footnotes
Some use cases for
restrictionlints include:- Strict coding styles (e.g.
clippy::else_if_without_else). - Additional restrictions on CI (e.g.
clippy::todo). - Preventing panicking in certain functions (e.g.
clippy::unwrap_used). - Running a lint only on a subset of code (e.g.
#[forbid(clippy::float_arithmetic)]on a module).
- Strict coding styles (e.g.
About
A bunch of lints to catch common mistakes and improve your Rust code. Book:https://doc.rust-lang.org/clippy/
Topics
Resources
License
Apache-2.0, MIT licenses found
Licenses found
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors1,084
Uh oh!
There was an error while loading.Please reload this page.