Configuration

Even if we will check individual lints in the next section, we will now see how we can configure the whole Clippy execution. Clippy will read the clippy.toml file at the same level as the Cargo.toml file and act accordingly.

Some lints have configuration parameters. For instance, the cyclomatic complexity lint will alert you whenever a function has more than 25 branches. As we saw in Chapter 1Common Performance Pitfalls, this is bad practice, since it will make the optimizations of the code much harder for the compiler, creating less performant code.

However, you can change the threshold that creates the warning. 25 is a fair amount of branches, but depending on your product, you would prefer not to have more than 20 branches or to be able to have up to 30, for example. The setting that changes this behavior in the clippy.toml is the cyclomatic-complexity-threshold:

cyclomatic-complexity-threshold = 30

Clippy will also warn you, for example, when it finds names that could be the name of a structure or enumeration in the documentation without the proper (`) characters showing that they are code. This can have false positives in cases such as your software being called MyCompanyInc, for example, where Clippy will think it's a struct or an enum. There is a configuration parameter for this case too. You can check all of them in the Clippy wiki at https://rust-lang-nursery.github.io/rust-clippy/master/.

If we want to add Clippy lints to our project, when we compile without Clippy, Rust will warn us that those lints are unknown. Of course, those have been defined by Clippy, but the compiler does not know this. Clippy sets a cargo-clippy feature by default, and when configuring the lints, we can use it to remove the unknown lints warning:

#![cfg_attr(feature = "cargo-clippy", forbid(deprecated))]

This way, when we run cargo clippy, the lint will be taken into account, but when running cargo check, it won't.

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

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