Answer#
Clippy checks code the compiler accepts. It knows hundreds of patterns, such as an index loop that an iterator would do better, or comparing len() == 0 instead of calling is_empty(). Every message names the lint and says how to fix it.
How to do it#
- Take this program. It compiles, but is not written the way Rust code usually is.
fn total(values: &Vec<i32>) -> i32 {
let mut sum = 0;
for i in 0..values.len() {
sum += values[i];
}
sum
}
fn main() {
let values = vec![1, 2, 3];
if values.len() == 0 {
println!("empty");
}
println!("{}", total(&values));
}- Run Clippy.
> cargo clippy
warning: writing `&Vec` instead of `&[_]` involves a new object where a slice will do
--> src\main.rs:1:18
|
1 | fn total(values: &Vec<i32>) -> i32 {
| ^^^^^^^^^ help: change this to: `&[i32]`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.93.0/index.html#ptr_arg
= note: `#[warn(clippy::ptr_arg)]` on by default
warning: the loop variable `i` is only used to index `values`
--> src\main.rs:3:14
|
3 | for i in 0..values.len() {
| ^^^^^^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.93.0/index.html#needless_range_loop
= note: `#[warn(clippy::needless_range_loop)]` on by default
help: consider using an iterator
|
3 - for i in 0..values.len() {
3 + for <item> in &values {
|
warning: length comparison to zero
--> src\main.rs:11:8
|
11 | if values.len() == 0 {
| ^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `values.is_empty()`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.93.0/index.html#len_zero
= note: `#[warn(clippy::len_zero)]` on by default
warning: `lint` (bin "lint") generated 3 warnings (run `cargo clippy --fix --bin "lint" -p lint` to apply 1 suggestion)- Apply the suggestions. Many can be applied automatically with
cargo clippy --fix, which edits the files, so commit your work first. - In CI, turn warnings into errors so they cannot be ignored:
> cargo clippy -- -D warnings
error: writing `&Vec` instead of `&[_]` involves a new object where a slice will do
--> src\main.rs:1:18
|
1 | fn total(values: &Vec<i32>) -> i32 {
| ^^^^^^^^^ help: change this to: `&[i32]`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.93.0/index.html#ptr_arg
= note: `-D clippy::ptr-arg` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::ptr_arg)]`
error: the loop variable `i` is only used to index `values`
--> src\main.rs:3:14
|
3 | for i in 0..values.len() {
| ^^^^^^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.93.0/index.html#needless_range_loop
= note: `-D clippy::needless-range-loop` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::needless_range_loop)]`
help: consider using an iterator
|
3 - for i in 0..values.len() {
3 + for <item> in &values {
|
error: length comparison to zero
--> src\main.rs:11:8
|
11 | if values.len() == 0 {
| ^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `values.is_empty()`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.93.0/index.html#len_zero
= note: `-D clippy::len-zero` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::len_zero)]`
error: could not compile `lint` (bin "lint") due to 3 previous errors
exit: 101Why it works#
Clippy is a compiler driver: it runs the normal compiler passes and adds its own lint passes on the result, which is why it needs a project that builds. Lints are grouped by how sure they are; the default set warns (clippy::all), and the stricter groups such as pedantic are off until you ask for them.
Common mistakes#
- Silencing a lint everywhere with
#![allow(clippy::all)]. Allow one lint, on one item, and say why in a comment:
#[allow(clippy::too_many_arguments)] // mirrors the C API one to one
fn open(a: u8, b: u8, c: u8, d: u8, e: u8, f: u8, g: u8, h: u8) {}- Treating each lint as an order. A few are matters of taste; the explanation link in each message says why the lint exists.
- Running Clippy on code that does not compile. Fix the compiler errors first.
In Ruststudio#
Run > Clippy Lints runs cargo clippy and shows the results in the Problems list, and Run > Cargo: Apply Clippy Fixes runs cargo clippy --fix with --allow-dirty --allow-staged. Because that changes files, commit or save your work before using it.