DEV Community

Eakam
Eakam

Posted on • Edited on

2

Adding formatter and linter to rost_gen

This week, I added a formatter and linter to rost_gen (a static site generator) to improve the readability of the code and fix common issues automatically. I chose to use rustfmt for the formatter and rust-clippy for the linter.

Setting these up was quite easy as they were already installed when I installed rust using rustup. I took some time to look through the configuration options for rustfmt and found that some of the options (the non-stable ones) were only available in the nightly toolchain. So, I had to install the nightly toolchain:

rustup toolchain install nightly

This automatically installed rustfmt and clippy on the nightly toolchain as well.

Then I added a rustfmt.toml configuration file for rustfmt, and added my desired options in it. I did not change any configuration options for clippy.

I ran rustfmt and discovered quite a lot of spacing changes as expected since I had changed the number of tab spaces from 4 to 2. Though I got a lot of unexpected warnings when I ran clippy. There were over a dozen warnings, most of them being unnecessary returns. I did not know that return was not needed in rust if it is the last statement that will be run in a function. The rest were about several minor things such as using .unwrap_or_else(|_| panic~()) instead of .expect(&format!()).

Next, I wanted to add a configuration file to Visual Studio Code so rustfmt and clippy would run automatically when a file is saved. I installed the rust-analyzer extension and added a settings.json file under the .vscode directory in my project. Here, I added the following:

{
  "rust-analyzer.checkOnSave.command": "clippy",
  "rust-analyzer.rustfmt.extraArgs": [
    "+nightly"
  ],
  "editor.formatOnSave": true,
  "[rust]": {
    "editor.defaultFormatter": "rust-lang.rust-analyzer"
  },
}
Enter fullscreen mode Exit fullscreen mode

I was able to autocomplete most of these with the help of this post. However, I needed to look up to find a way to run the nightly version of rustfmt as most of my configurations needed the nightly toolchain. I found that it could be done by adding the extraArgs property (line 2 above).

I also added an extensions.json file and added rust-analyzer to the recommendations so vscode would prompt if it was not installed.

👋 Kindness is contagious

Please leave your appreciation by commenting on this post!

It takes one minute and is worth it for your career.

Get started

Thank you!

Top comments (0)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay