Ben Chuanlong Du's Blog

It is never too late to learn.

Tips on Rust Clippy

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

unused_variables unused_imports dead_code

General Tips

  1. Clippy does not support filtering by specific lint directly. However, it can be achieved via the old rustc flags hack. For example, you can use the following command to fix only clippy::collapsible_else_if lints.

    :::bash cargo clippy --fix -- \ -A clippy::all -W clippy::collapsible_else_if

    If there are other non-Clippy lint warnings, you can filter out them manually. For example, if you code still have unfixed unused_variables and dead_code lints, you can filter them out by adding more -A options.

    :::bash cargo clippy --fix -- \ -A unused_variables -A dead_code \ -A clippy::all -W clippy::collapsible_else_if

    Lint Levels and Lint Groups are important lint concepts which help you better understand the above trick.

  2. Not all lints can be automatically fixed by Clippy. If you have a large number of a specific kind of lint which is not automatically fixable by Clippy, you can write a Python script to help you fix those issues automatically instead of fixing them manually.

Configuration

Adding configuration to a lint

#[allow(clippy::wrong_self_convention)]
#[allow(clippy::all)]

Place the following at the beginning of a Rust source file, if you want to disable all Clippy lints in the file.

#![allow(clippy::all)]

References

Comments