📜 ⬆️ ⬇️

Roslyn Analyzers. How to write code quickly and accurately

Roslyn, the .NET compiler platform, helps detect errors even before running code. One example is the Roslyn spell checker, which is built into Visual Studio. Let's say you create a static method and randomly write the word static as statc. You will be able to see this spelling error even before the launch of your code, because Roslyn will issue a warning in your code when entering text even before the completion of the string. In other words, you do not need to build the code to find out that you made a mistake.

image

Roslyn analyzers can also display automatic correction of the code using the Visual Studio light bulb icon, which allows you to immediately fix the code.
')
image

But what if you could catch more bugs?


Let me introduce you to the Roslyn analyzer packages. These analyzer collections provide more detailed analysis, but do not come with the default tools of Visual Studio. To learn more about our favorite Roslyn analyzers, visit the Roslyn analyzers repository on GitHub . This repository includes the FxCop rules, which are still applicable to modern software development, but are now targeted for our modern Roslyn based code analysis platform. Let's continue and install this package to increase the productivity and speed of writing excellent code!

Installing FxCop analyzers:

  1. I assume that you are using Visual Studio 2017 version 15.8 or newer. If so, select the latest version of Microsoft.CodeAnalysis.FxCopAnalyzers .
  2. Install the package in Visual Studio using the Package Manager UI .

image

After installing the package, you can simply configure the analyzer diagnostics in Solution Explorer. The analyzer node will appear under the References or Dependencies node in Solution Explorer. If you first deploy analyzers, and then one of the analyzer assemblies, you can view all available diagnostics in the assembly.

image

You can view diagnostic properties, including items such as Description and Default severity, in the Properties window. To view the properties, right-click the rule and select Properties, or select the rule, and then press Alt + Enter .

image

The icons next to each type of diagnostics in Solution Explorer correspond to the icons that appear in the rule set when it is opened in the editor:


image

You can then set severity options in Solution Explorer. First of all, to do this, you need to deploy Dependencies > Analyzers in Solution Explorer, and then deploy the assembly containing the rule for which you want to set severity parameters. After that, right-click on the rule, select Set Rule Set Severity and select one of the severity parameters in the pop-up menu.

image

If you specify Warning, you will receive warnings in your code for this particular set of rules.

image

Now that you understand how analyzers work, you can improve performance and write excellent code faster!

FAQ:


Q : I encountered the following warning when working in Visual Studio: “Run Code Analysis has been deprecated in favor of FxCop analyzers, which run during build. Refer to aka.ms/fxcopanalyzers to migrate to FxCop analyzers ”. What does it mean?
A : FxCop is a Code Analysis tool that Roslyn preceded almost a decade. Just as we moved our compiler (for example, by introducing Roslyn), we also transferred our code analysis technology to the Roslyn platform. Since it works on the .NET Compiler Platform, it can issue warnings in your code as you type. In other words, there is no need to assemble the code to learn about the error.

Q : What is the difference between legacy FxCop and FxCop analyzers?
A : FxCop analyzers analyze source code in real time and at compile time, while legacy FxCop is a static code analysis and analyzes binary files after the build is completed. For more information, see Roslyn analyzers vs. static code analysis and FxCop analyzers FAQ .

Q : Can I write my own analyzers?
A : Of course! Documentation for writing analyzers is here .

Q : Should I use Roslyn analyzers or .editorconfig for code style?
A : Roslyn analyzers and .editorconfig files work hand in hand. When you define code styles in an .editorconfig file or on a text editor settings page, you actually configure the Roslyn analyzers built into Visual Studio.

Q : Do analyzers work in continuous integration (CI) assemblies?
A : Yes, analyzers installed as NuGet packages can be used in CI builds.

Q : Where can I request more analyzers or report errors?
A : You can request more analyzers and report bugs in the Roslyn repository on GitHub.

Source: https://habr.com/ru/post/459982/


All Articles