BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News C# Static Analysis Tool Roslynator.Analyzers Now Has over 500 Ways to Improve Code

C# Static Analysis Tool Roslynator.Analyzers Now Has over 500 Ways to Improve Code

This item in japanese

The new version 2.3.1 of the Roslynator.Analyzers package brings the number of analyzers, refactorings, and fixes to over 500. Roslynator uses the open-source Roslyn .NET Compiler Platform to perform static analysis on your C# code. This analysis drives your IDE to display hints and actions to improve your code.

Roslynator can be installed into Visual Studio 2017, Visual Studio 2019 and Visual Studio Code. The VSCode version runs on Linux, Mac and Windows. So that it can take advantage of the latest version of Roslyn, 3.0, Roslynator.Analyzers 2.3.1 will be the last version to support Visual Studio 2017.

The functionality can be seen with a simple "hello world" console application.

using System;
using System.Collections.Generic;
 
namespace HelloWorldApp
{
    class Program
    {
        static void Main(string[] args)
        {  
            var a = new List<string>() { "Hello","World"};
            var b = a.ToArray();
            for (var f = 1;f < b.Length;f++) {
                Console.WriteLine(value: b[f].ToString());
            }
            
        }
    }
}

Roslynator adds a lightbulb action item in the menu next to items that can be refactored and lists problems in the panel at the bottom of the screen. Wiggly lines highlight the issues mentioned in the problems panel. The system works as you type, for example, whilst typing the for statement the Roslynator analyzer highlights if the end condition does not resolve to a Boolean.

Using the configuration file or Visual Studio IDE you can configure which of the checks and fixes you wish to include in your project. The configuration file can be shared to enable the whole team to use the same settings.

The analysers are extensible so that you can write your own in order to handle common issues in your organisation, such as deprecated APIs or inclusion of security credentials in source code. The tool also supports a command-line variation so that it can be integrated into your build pipeline.

Like other static analysis tools, Roslynator does not require the software to be executed in order to ascertain the behaviour and features of the programme. It does this by reviewing the source code. These tools have been around since the 1970s when Stephen C. Johnson came up with the idea of Lint for his C programmes. They are even available for non-compiled languages such as Python and JavaScript.

The reason tools such as SonarAnalyzer, XUnit Analyzers, Code Cracker, FxCop and StyleCop can do this analysis relatively easily is due to the unique way that the Roslyn compiler has been created. As shown below, rather than being a black box, the compiler and tools are provided as a series of APIs and services. These tools can use the compiler APIs both to identify problem code and to generate replacements.

Image source: Roslyn Overview on github.

Not every popular code analysis tool uses Roslyn however. The Resharper tool, for example, has been around for longer, and uses proprietary technology from JetBrains. Resharper predates Roslyn by several years and their engine supports features not yet available in the Roslyn API, such as solution-wide error analysis, "big picture" code inspection, and support for a wider range of languages such as Razor. JetBrains has used the Roslyn technology in their other products such as the Rider IDE.

Rate this Article

Adoption
Style

BT