Compiler Lints and Clippy

The Rust compiler produces fantastic error messages, as well as helpful built-in lints. Clippy provides even more lints, organized into groups that can be enabled per-project.

  1. #[deny(clippy::cast_possible_truncation)]
  2. fn main() {
  3.     let x = 3;
  4.     while (x < 70000) {
  5.         x *= 2;
  6.     }
  7.     println!("X probably fits in a u16, right? {}", x as u16);
  8. }

This slide should take about 3 minutes.

Run the code sample and examine the error message. There are also lints visible here, but those will not be shown once the code compiles. Switch to the Playground site to show those lints.

After resolving the lints, run clippy on the playground site to show clippy warnings. Clippy has extensive documentation of its lints, and adds new lints (including default-deny lints) all the time.

Note that errors or warnings with help: ... can be fixed with cargo fix or via your editor.