- Mastering Rust
- Rahul Sharma Vesa Kaihlavirta
- 325字
- 2021-07-02 13:35:22
Linting code with clippy
Linting is a practice that helps maintain the quality of your library and have it adhere to standard coding idioms and practices. The de facto linting tool in the Rust ecosystem is clippy. Clippy provides us with a bunch of lints (about 291 at the time of writing this book) to ensure high quality Rust code. In this section, we'll install clippy and try it out on our libawesome library, add some dummy code to it, and see what suggestions we get from clippy. There are various ways of using clippy on your project, but we will use the cargo clippy subcommand way as that's simple. Clippy can perform analysis on code because it's a compiler plugin and has access to a lot of the compiler's internal APIs.
To use clippy, we need to install it by running rustup component add clippy. If you don't have it already, rustup will install it for you. Now, to demonstrate how clippy can point out bad style in our code, we have put some dummy statements within an if condition inside our pow function in the myexponent crate, as follows:
// myexponent/src/lib.rs
fn pow(base: i64, exponent: usize) -> i64 {
/////////////////// Dummy code for clippy demo
let x = true;
if x == true {
}
///////////////////
let mut res = 1;
...
}
With those lines added, by running cargo clippy in our myexponent directory, we get the following output:

Great! Clippy has found a common code style that is redundant, that is, checking for a Boolean value that is either true or false. Alternatively, we could have written the preceding if condition directly as if x {} . There are many more checks that clippy does, and some of them even point out potential mistakes in your code, such as https://rust-lang-nursery.github.io/rust-clippy/master/index.html#absurd_extreme_comparisons. To see all the available lints and various ways of configuring clippy, head over to https://github.com/rust-lang/rust-clippy.
- Microsoft Application Virtualization Cookbook
- AngularJS Web Application Development Blueprints
- 機器人Python青少年編程開發實例
- Processing互動編程藝術
- 老“碼”識途
- 精通Python自然語言處理
- C程序設計實踐教程
- Windows Phone 7.5:Building Location-aware Applications
- 領域驅動設計:軟件核心復雜性應對之道(修訂版)
- 常用工具軟件立體化教程(微課版)
- OpenStack Networking Essentials
- 跟戴銘學iOS編程:理順核心知識點
- Photoshop智能手機APP界面設計
- 例解Python:Python編程快速入門踐行指南
- 產品架構評估原理與方法