DEV Community

Cover image for Static Analysis with Roslynator and Dotnet-format
Amir Helali
Amir Helali

Posted on

Static Analysis with Roslynator and Dotnet-format

This week I used dotnet-format and Roslynator as a formatter and linter for my project. Also, since this is an open source project I finally made a CONTRIBUTING.md file for it, which explains all contribution guidelines and the set up process.

Dotnet-Format

dotnet-format is a code formatter for the .Net environment. I chose it because I used C# for my project. Setting it up was very easy, I installed it by running the command: dotnet tool install -g dotnet-format --version "8.0.446201" --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json. However, I had to use an older version since I had .NET 6 installed. After installation I simply ran it with the command dotnet format from the command line in my project's directory and by default it fixed the indentation and white-space formatting issues in my code. It can also be customized to follow formatting preferences by including a .editorconfig file in the project.

Roslynator

To choose a linter I had to do some research about which one to use, since I have never used one before. Finally, I decided to go with Roslynator because it has everything I needed. It has extensions for the IDEs I use, it has a command-line tool and it offers more than 200 analyzers for C# which is extremely useful. Like dotnet-format, setting it up was very easy, I installed the command line tool by running the command: dotnet tool install -g roslynator.dotnet.cli, then I used the command roslynator analyze where it gave me feedback on what I needed to fix, which I did. The feedback it provided can be found in this picture:
Image description. There is also the roslynator fix command that can fix the issues it finds. Also, to integrate the tool with my IDEs I used both extensions for Visual Studio and Visual Studio Code. I used the default configuration, but it can be customized by editing the .editorconfig file, the configuration guide can be found here.

What I learned

This was my first time using a formatter and linter. I can see how useful they can be specially if there is an open source project with many people working on it. They allow you to automate a set of preferences and standards that applies to everyone's work and that is extremely helpful in a collaborative environment. Also, after adding Roslynator, I realized that I could've just used that as my formatter instead of dotnet-format, but I added 2 new tools to my tool set so no regrets here.

Top comments (0)