Unused lints

Let's face it, we sometimes forget to remove a dependency we are no longer using, or we forget that the write() method returns the number of bytes written. This usually is not a big deal. The first will simply make our compilation slower, while the second, in most cases, will simply not change our code.

But since we do not want to have unused dependencies, or we don't want to forget that we might not have written the whole buffer to a file, that's where the next lints come to help us. Let's start with the first one, the unused_extern_crates lint. This lint will mark the external crates that are not being used in our code. This can be useful to remove dependencies we are no longer using, so I usually configure it to warn while I'm starting the development and change it to forbid once my crates go to production or the dependencies are not changing in every commit.

The second lint you should know about is the unused_results lint. By default, the compiler will warn about unused results for Result<T, E> return values. That is an important detail because it could be that an I/O operation failed, for example, and you should act accordingly. There are other cases, though, where the Rust compiler won't warn, but that can be almost as dangerous as the previous ones. The Write and Read traits, for example, will return the number of bytes written and read, respectively, and you should probably be aware of that number.

This lint will make sure you always take into account any return value, except for the empty tuple (). This can sometimes be annoying, but you can explicitly discard a result by using the underscore binding, which is shown as follows:

#![warn(unused_results)]

fn main() {
let _ = write_hello();
}

fn write_hello() -> usize {
unimplemented!()
}

There are also a couple of lints that will make your code much more readable: unused_qualifications and unused_import_braces. The first will detect places where you are using extra qualifications for some elements:

#![warn(unused_qualifications)]

#[derive(Debug)]
enum Test {
A,
B,
}

fn main() {
use Test::*;

println!("{:?}", Test::A);
println!("{:?}", B);
}

This example of code will warn us that in the first println!() we do not need to use the Test:: qualification, as we are already importing all values inside the Test enumeration. The second println!() will not warn us since we are not specifying any extra qualifications. This will make the code more readable, potentially reducing errors.

The second lint, the unused_import_braces lint, will check for places where we are importing only one element with import braces:

#![warn(unused_import_braces)]

#[derive(Debug)]
enum Test {
A,
B,
}

#[derive(Debug)]
enum Test2 {
C,
D,
}

fn main() {
use Test::{A, B};
use Test2::{C};

println!("{:?}, {:?}, {:?}", A, B, C);
}

Even though the Rust formatter will automatically remove the braces around the C variant of the Test2 import, if we do not use the formatter, this is an interesting lint that will alert that we do not require those braces and that removing them we will make the code cleaner.

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

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