Using Rust compiler lints

The Rust compiler, at the time of writing, has 70 lints. We will not check all 70, but we will take a look at the most relevant ones. Let's first start by learning how to configure a lint. We will take unused_imports as an example. The compiler will warn you for this lint by default. The compilation will continue, but it will show a warning in the command line, or in the editor if it's configured to show Rust compilation warnings.

We can change this behavior, and we can change it for each scope. The options are allow, warn, deny, and forbid the lint. If we allow the lint, no more warnings will appear. If we warn, compilation warnings will appear, and if we deny or forbid, the program won't compile if it finds something that triggers the lint. The difference between deny and forbid is that the former can be overridden down the line, while the latter can't. So we can have a module that denies one behavior, but in one particular function, we want to allow it.

This configuration can be applied at crate level, by putting  #![deny(unused_imports)], for example, at the top of the lib.rs or main.rs file. It can also be applied to any scope, even scopes you might create inside functions. If it has an exclamation mark (!) after the hash, it will affect the current scope; if not, it will affect the scope just next to it. Let's see what lints the Rust compiler gives us.

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

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