How it works...

In this recipe, you learned the methods by which you can use the Clippy tool to catch the different lints in the code and get suggestions to improve them.

When we use Clippy as an optional dependency, we set the optional flag as true when mentioning Clippy as a dependency in the Cargo.toml file. In the feature section, we have a default set of optional packages. Most of the time, developers will want to use these packages, but they are strictly optional. In addition to manipulations in the configuration file, we have to enable the #[cfg_attr] attribute that generally works with #[cfg_attr(condition, attribute)], where it allows us to compile the Rust script main.rs if the condition is true; that is, if the condition is true, it's equivalent to #[attribute], and if the condition is false, it does not proceed.

The cargo clippy command is the easiest way to run the Clippy tool as it does not require any manipulations in the configuration or code.

In the third way, we add the clippy dependency in the Cargo.toml file without mentioning it as a feature and loading it as a compiler plugin. We do syntax extension by calling #![feature(plugin)] and #![plugin(clippy)].

rustc can load compiler plugins, which are user-provided libraries that extend the compiler's behavior with new syntax extensions, lint checks, and so on. There are around 200 lints and this is a growing number configured with the rust-clippy tool that helps developers write better Rust code.

A good place to use the tool would be to have the cargo clippy command in the CI script processes for the project before building the projects, similar to the rustfmt tool.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.249.198