DEV Community

TheoForger
TheoForger

Posted on

(not) Adding A Code Linter, and Why Rust is Awesome

This week we were tasked to add a code formatter, as well as a linter to our project, and then integrate them with the IDE. We also needed to create a contribution guideline, which covers the setup process for the project and the linter tools.

(not) Adding a Linter and a Code Formatter

To be honest, for the Mastermind project, I've been using rustfmt and clippy religiously from the very beginning.

In case you don't know: rustfmt is a Rust code formatter, and clippy is a Rust linter. They are pre-bundled within the cargo package manager, so no additional setup is required. They are, of course, both written in Rust and very well maintained, so there's really no reason to choose anything else.

RustRover, my IDE of choice, supports these tools out of the box. All I had to do was adding some custom arguments to cargo clippy:

--all-features --all-targets -- -W clippy::all
Enter fullscreen mode Exit fullscreen mode

Rust is Awesome!

This made me think: You don't really see many languages provide first-party support of a package/project manager, a code formatter and a linter. The fact that these are all included in the Rust toolchain is kinda amazing!

(not) Configuring These Tools

For rustfmt, the only thing I would change is to enable the fn_single_line option. However, this option is not stable yet, and I don't feel comfortable switching to the nightly channel just for the one option, so I decided to leave everything as default.

With clippy, things are more complicated. From their GitHub page, you can see there are several different categories:

Category Description Default level
clippy::all all lints that are on by default (correctness, suspicious, style, complexity, perf) warn/deny
clippy::correctness code that is outright wrong or useless deny
clippy::suspicious code that is most likely wrong or useless warn
clippy::style code that should be written in a more idiomatic way warn
clippy::complexity code that does something simple but in a complex way warn
clippy::perf code that can be written to run faster warn
clippy::pedantic lints which are rather strict or have occasional false positives allow
clippy::restriction lints which prevent the use of language and library features[^restrict] allow
clippy::nursery new lints that are still under development allow
clippy::cargo lints for the cargo manifest allow

I already use clippy::all since project creation. In the past, I've also tried enabling the clippy::pedantic group. It ended up giving me warnings on every single function that's not documented, among other false positives.

Not-so-helpful warnings from clippy::pedantic

However, I did go through each of them and find some useful tips. As I discussed in a previous post, some of these suggestions can really teach you good coding practices. Although you probably shouldn't leave this option on all the time.

Top comments (0)