Redigera

Dela via


Code analysis using .NET compiler platform (Roslyn) analyzers

.NET compiler platform (Roslyn) analyzers inspect your C# or Visual Basic code for style, quality, maintainability, design, and other issues. This inspection or analysis happens during design time in all open files.

Analyzers are divided into the following groups:

Severity levels of analyzers

Each Roslyn analyzer rule, or diagnostic, has a default severity and suppression state that you can customize for your project.

Severity levels include: Error, Warning, Suggestion, Silent, None, and Default. For detailed information and behavior, see Configure severity levels.

Rule violations

If an analyzer finds any analyzer rule violations, it reports them in the Error List window and in the code editor.

The following screenshot shows rule violations reported in the Error List window. The analyzer violations reported in the error list match the severity level setting of the rule:

Screenshot that shows analyzer violations in the Error List window.

The analyzer rule violations also appear in the code editor as squiggle lines under the offending code. For example, the following screenshot shows three violations: one error (red squiggle line), one warning (green squiggle line), and one suggestion (three gray dots):

Screenshot that shows error, warning, and suggestion marks in the code editor.

Code fixes

Many diagnostics have one or more associated code fixes that you can apply to correct the rule violation. Code fixes are shown in the light bulb icon menu along with other types of Quick Actions. For more information about code fixes, see Common Quick Actions.

Screenshot that shows an analyzer violation and Quick Action code fix in the code editor.

Configure analyzer severity levels

You can configure the severity of analyzer rules in an EditorConfig file or from the light bulb menu.

You can also configure analyzers to inspect code at build time and as you type. You can configure the scope of live code analysis to execute for the current document only, all open documents, or the entire solution. For more information, see Configure live code analysis for .NET.

Tip

Build-time errors and warnings from code analyzers are shown only if the analyzers are installed as a NuGet package. The built-in analyzers (for example, IDE0067 and IDE0068) never run during build.

Install external code analyzers

Visual Studio includes a core set of Roslyn analyzers, which are always active. If you need more code analyzers, you can install external analyzers for every project via a NuGet package. Some analyzers are also available as a Visual Studio .vsix extension, in which case they apply to any solution you open in Visual Studio. For information about installing these two different types of analyzers, see Install code analyzers.

Scope

If you install an analyzer as a NuGet package, the preferred method, it applies only to the project where the NuGet package is installed. Otherwise, if you install an analyzer as a Visual Studio extension, it applies at the solution level and to all instances of Visual Studio. In team environments, an analyzer you install as a NuGet package is in scope for all developers that work on that project.

Note

First-party analyzers ship within the .NET SDK. It's preferable that you enable these analyzers from the .NET SDK instead of installing them as a Microsoft.CodeAnalysis.NetAnalyzers NuGet package. Enabling the analyzers from the .NET SDK ensures that you automatically get analyzer bug fixes and new analyzers as soon as you update the SDK. For more information about analyzers, see Enable or install first-party .NET analyzers.

Build errors

To enforce the rules at build time, by using either a command-line or continuous integration (CI) builds, choose one of the following options:

  • Create a .NET 5.0 or later project, which enables code analysis by default. To enable code analysis on projects that target earlier .NET versions, set the EnableNETAnalyzers property to true.

  • Install the analyzers as a NuGet package. If you install the analyzers as a Visual Studio extension, analyzer warnings and errors don't appear in the build report.

The following screenshot shows the command-line build output from building a project that contains an analyzer rule violation:

Screenshot that shows an MSBuild output with a rule violation in a Developer Command Prompt.

Rule severity

If you want to configure analyzer rule severity, you must install the analyzer as a NuGet package. You can't configure rule severity from analyzers that were installed as a Visual Studio extension.

Next steps