• Stars
    star
    9,860
  • Rank 3,358 (Top 0.07 %)
  • Language
    Rust
  • License
    Other
  • Created over 9 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/

Clippy

Clippy Test License: MIT OR Apache-2.0

A collection of lints to catch common mistakes and improve your Rust code.

There are over 700 lints included in this crate!

Lints are divided into categories, each with a default lint level. You can choose how much Clippy is supposed to annoy 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, please file an issue if you have ideas!

The restriction category should, emphatically, not be enabled as a whole. The contained lints may lint against perfectly reasonable code, may not have an alternative suggestion, and may contradict any other lints (including other categories). Lints should be considered on a case-by-case basis before enabling.


Table of contents:

Usage

Below are instructions on how to use Clippy as a cargo subcommand, in projects that do not use cargo, or in Travis CI.

As a cargo subcommand (cargo clippy)

One way to use Clippy is by installing Clippy through rustup as a cargo subcommand.

Step 1: Install Rustup

You can install Rustup on supported platforms. This will help us install Clippy and its dependencies.

If you already have Rustup installed, update to ensure you have the latest Rustup and compiler:

rustup update

Step 2: Install Clippy

Once you have rustup and the latest stable release (at least Rust 1.29) installed, run the following command:

rustup component add clippy

If it says that it can't find the clippy component, please run rustup self update.

Step 3: Run Clippy

Now you can run Clippy by invoking the following command:

cargo clippy

Automatically applying Clippy suggestions

Clippy 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 --fix

Workspaces

All the usual workspace options should work with Clippy. For example the following command will run Clippy on the example crate:

cargo clippy -p example

As with cargo check, this includes dependencies that are members of the workspace, like path dependencies. If you want to run Clippy only on the given crate, use the --no-deps option like this:

cargo clippy -p example -- --no-deps

Using clippy-driver

Clippy can also be used in projects that do not use cargo. To do so, run clippy-driver with the same arguments you use for rustc. For example:

clippy-driver --edition 2018 -Cpanic=abort foo.rs

Note that clippy-driver is designed for running Clippy only and should not be used as a general replacement for rustc. clippy-driver may produce artifacts that are not optimized as expected, for example.

Travis CI

You can add Clippy to Travis CI in the same way you use it locally:

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.

Note that adding -D warnings will cause your build to fail if any 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 cause an error for Clippy warnings, use #![deny(clippy::all)] in your code or -D clippy::all on the command line. (You can swap clippy::all with the specific lint category you are targeting.)

Configuration

Allowing/denying lints

You can add options to your code to allow/warn/deny Clippy lints:

  • the whole set of Warn lints using the clippy lint group (#![deny(clippy::all)]). Note that rustc has additional lint groups.

  • all lints using both the clippy and clippy::pedantic lint groups (#![deny(clippy::all)], #![deny(clippy::pedantic)]). Note that clippy::pedantic contains some very aggressive lints prone to false positives.

  • only some lints (#![deny(clippy::single_match, clippy::box_vec)], etc.)

  • allow/warn/deny can be limited to a single function or module using #[allow(...)], etc.

Note: allow means to suppress the lint for your code. With warn the lint will only emit a warning, while with deny the lint will emit an error, when triggering for your code. An error causes clippy to exit with an error code, so is useful in scripts like CI/CD.

If you do not want to include your lint levels in your code, you can globally enable/disable lints by passing extra flags to Clippy during the run:

To allow lint_name, run

cargo clippy -- -A clippy::lint_name

And to warn on lint_name, run

cargo clippy -- -W clippy::lint_name

This also works with lint groups. For example, you can run Clippy with warnings for all lints enabled:

cargo clippy -- -W clippy::pedantic

If you care only about a single lint, you can allow all others and then explicitly warn on the lint(s) you are interested in:

cargo clippy -- -A clippy::all -W clippy::useless_format -W clippy::...

Configure the behavior of some lints

Some lints can be configured in a TOML file named clippy.toml or .clippy.toml. It contains a basic variable = value mapping e.g.

avoid-breaking-exported-api = false
disallowed-names = ["toto", "tata", "titi"]

The table of configurations contains all config values, their default, and a list of lints they affect. Each configurable lint , also contains information about these values.

For configurations that are a list type with default values such as disallowed-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.toml or .clippy.toml cannot be used to allow/deny lints.

To deactivate the โ€œfor further information visit lint-linkโ€ message you can define the CLIPPY_DISABLE_DOCS_LINKS environment variable.

Specifying the minimum supported Rust version

Projects that intend to support old versions of Rust can disable lints pertaining to newer features by specifying the minimum supported Rust version (MSRV) in the clippy configuration file.

msrv = "1.30.0"

Alternatively, the rust-version field in the Cargo.toml can be used.

# Cargo.toml
rust-version = "1.30"

The MSRV can also be specified as an attribute, like below.

#![feature(custom_inner_attributes)]
#![clippy::msrv = "1.30.0"]

fn main() {
  ...
}

You can also omit the patch version when specifying the MSRV, so msrv = 1.30 is equivalent to msrv = 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 found here

Contributing

If you want to contribute to Clippy, you can find more information in CONTRIBUTING.md.

License

Copyright 2014-2024 The Rust Project Developers

Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. Files in the project may not be copied, modified, or distributed except according to those terms.

Footnotes

  1. Some use cases for restriction lints include:

    โ†ฉ

More Repositories

1

rust

Empowering everyone to build reliable and efficient software.
Rust
85,122
star
2

rustlings

๐Ÿฆ€ Small exercises to get you used to reading and writing Rust code!
Rust
47,148
star
3

mdBook

Create book from markdown files. Like Gitbook but implemented in Rust
Rust
15,892
star
4

book

The Rust Programming Language
Rust
13,981
star
5

rust-analyzer

A Rust compiler front-end for IDEs
Rust
13,175
star
6

cargo

The Rust package manager
Rust
11,293
star
7

rust-by-example

Learn Rust with examples (Live code editor included)
6,109
star
8

rustup

The Rust toolchain installer
Rust
5,558
star
9

rustfmt

Format Rust code
Rust
5,524
star
10

rfcs

RFCs for changes to Rust
Markdown
5,390
star
11

futures-rs

Zero-cost asynchronous programming in Rust
Rust
5,119
star
12

rust-bindgen

Automatically generates Rust FFI bindings to C (and some C++) libraries.
Rust
3,830
star
13

rust.vim

Vim configuration for Rust.
Vim Script
3,628
star
14

miri

An interpreter for Rust's mid-level intermediate representation
Rust
3,581
star
15

rls

Repository for the Rust Language Server (aka RLS)
Rust
3,530
star
16

regex

An implementation of regular expressions for Rust. This implementation uses finite automata and guarantees linear time matching on all inputs.
Rust
3,083
star
17

crates.io

The Rust package registry
Rust
2,554
star
18

hashbrown

Rust port of Google's SwissTable hash map
Rust
2,194
star
19

log

Logging implementation for Rust
Rust
2,046
star
20

this-week-in-rust

Data for this-week-in-rust.org
HTML
1,826
star
21

libc

Raw bindings to platform APIs for Rust
Rust
1,786
star
22

chalk

An implementation and definition of the Rust trait system using a PROLOG-like logic solver
Rust
1,713
star
23

async-book

Asynchronous Programming in Rust
Shell
1,640
star
24

nomicon

The Dark Arts of Advanced and Unsafe Rust Programming
CSS
1,608
star
25

rustc-dev-guide

A guide to how rustc works and how to contribute to it.
HTML
1,522
star
26

git2-rs

libgit2 bindings for Rust
Rust
1,520
star
27

cc-rs

Rust library for build scripts to compile C/C++ code into a Rust library
Rust
1,491
star
28

vscode-rust

Rust extension for Visual Studio Code
TypeScript
1,388
star
29

rustc_codegen_cranelift

Cranelift based backend for rustc
Rust
1,348
star
30

polonius

Defines the Rust borrow checker.
Rust
1,207
star
31

api-guidelines

Rust API guidelines
1,186
star
32

reference

The Rust Reference
Rust
1,090
star
33

rust-playground

The Rust Playground
Rust
1,076
star
34

rust-mode

Emacs configuration for Rust
Emacs Lisp
1,009
star
35

docs.rs

crates.io documentation generator
Rust
891
star
36

rustfix

Automatically apply the suggestions made by rustc
Rust
857
star
37

rustc_codegen_gcc

libgccjit AOT codegen for rustc
Rust
850
star
38

portable-simd

The testing ground for the future of portable SIMD in Rust
Rust
775
star
39

datafrog

A lightweight Datalog engine in Rust
Rust
770
star
40

flate2-rs

DEFLATE, gzip, and zlib bindings for Rust
Rust
730
star
41

rust-semverver

Automatic checking for semantic versioning in library crates
Rust
637
star
42

socket2

Advanced configuration options for sockets.
Rust
633
star
43

unsafe-code-guidelines

Forum for discussion about what unsafe code can and can't do
618
star
44

crater

Run experiments across parts of the Rust ecosystem!
Rust
588
star
45

stdarch

Rust's standard library vendor-specific APIs and run-time feature detection
Rust
585
star
46

packed_simd

Portable Packed SIMD Vectors for Rust standard library
Rust
561
star
47

rustc-perf

Website for graphing performance of rustc
Rust
539
star
48

libm

A port of MUSL's libm to Rust.
Rust
481
star
49

cfg-if

A if/elif-like macro for Rust #[cfg] statements
Rust
479
star
50

crates.io-index

Registry index for crates.io
476
star
51

backtrace-rs

Backtraces in Rust
Rust
473
star
52

style-team

Home of the Rust style team
Rust
438
star
53

docker-rust

The official Docker images for Rust
Dockerfile
399
star
54

glob

Support for matching file paths against Unix shell style patterns.
Rust
381
star
55

rust-forge

Information useful to people contributing to Rust
Rust
380
star
56

compiler-team

A home for compiler team planning documents, meeting minutes, and other such things.
HTML
369
star
57

wg-async

Working group dedicated to improving the foundations of Async I/O in Rust
Rust
357
star
58

www.rust-lang.org

The home of the Rust website
Fluent
352
star
59

compiler-builtins

Porting `compiler-rt` intrinsics to Rust
Rust
328
star
60

measureme

Support crate for rustc's self-profiling feature
Rust
316
star
61

blog.rust-lang.org

The Rust Programming Language Blog
HTML
302
star
62

glacier

A big 'ol pile of ICE.
Rust
289
star
63

team

Rust teams structure
Rust
288
star
64

rustc-hash

Custom hash algorithm used by rustc (plus hashmap/set aliases): fast, deterministic, not secure
Rust
276
star
65

project-error-handling

Error handling project group
263
star
66

atom-ide-rust

Rust IDE support for Atom, powered by the Rust Analyzer or Rust Language Server
JavaScript
260
star
67

stacker

Manual segmented stacks for Rust
Rust
252
star
68

cmake-rs

Rust build dependency for running cmake
Rust
241
star
69

edition-guide

A guide to changes between various editions of Rust
233
star
70

getopts

The getopts repo maintained by the rust-lang project
Rust
229
star
71

a-mir-formality

a model of MIR and the Rust type/trait system
Rust
228
star
72

rust-roadmap-2017

Tracking Rust's roadmap
218
star
73

areweasyncyet.rs

Are we async yet?
Rust
209
star
74

rust-playpen

A web interface for running Rust code
Rust
208
star
75

annotate-snippets-rs

Library for snippet annotations
Rust
200
star
76

rustc-demangle

Rust symbol demangling
Rust
196
star
77

wg-allocators

Home of the Allocators working group: Paving a path for a standard set of allocator traits to be used in collections!
188
star
78

highfive

Github hooks to provide an encouraging atmosphere for new contributors
Python
184
star
79

lang-team

Home of the Rust lang team
JavaScript
184
star
80

rust-guidelines

This repository has moved
179
star
81

rustwide

Execute your code on the Rust ecosystem.
Rust
174
star
82

ferris-says

A Rust flavored implementation of `cowsay`
Rust
172
star
83

cargo-bisect-rustc

Bisects rustc, either nightlies or CI artifacts
Rust
171
star
84

homu

A bot that integrates with GitHub and your favorite continuous integration service
Python
164
star
85

rfcbot-rs

Coordinates asynchronous decision making on Rust repositories. Status of tracked issues and PRs can be viewed at https://rfcbot.rs.
Rust
157
star
86

triagebot

Automation/tooling for Rust spaces
Rust
155
star
87

prev.rust-lang.org

The previous Rust website. The current website's code is at https://github.com/rust-lang/www.rust-lang.org.
HTML
153
star
88

pkg-config-rs

Build library for invoking pkg-config for Rust
Rust
147
star
89

thanks

Celebrate Rust contributors.
Rust
143
star
90

rustc-reading-club

Rust Code Reading Clubs
141
star
91

gll

GLL parsing framework.
Rust
136
star
92

ena

An implementation of union-find / congruence-closure in Rust. Extracted from rustc for independent experimentation.
Rust
131
star
93

simpleinfra

Rust Infrastructure automation
HCL
128
star
94

wg-cargo-std-aware

Repo for working on "std aware cargo"
125
star
95

rust-memory-model

Collecting examples and information to help design a memory model for Rust.
Python
125
star
96

rust-artwork

Official artwork for the Rust project.
120
star
97

rustup-components-history

Rustup package status history
Rust
100
star
98

libs-team

The home of the library team
Rust
100
star
99

const-eval

home for proposals in and around compile-time function evaluation
CSS
98
star
100

wg-grammar

Where the work of WG-grammar, aiming to provide a canonical grammar for Rust, resides
Rust
98
star