|
| 1 | +--- |
| 2 | +title: "Roslyn analyzers in Visual Studio | Microsoft Docs" |
| 3 | +ms.date: 03/26/2018 |
| 4 | +ms.technology: vs-ide-code-analysis |
| 5 | +ms.topic: conceptual |
| 6 | +helpviewer_keywords: |
| 7 | + - "code analysis, managed code" |
| 8 | + - "analyzers" |
| 9 | + - "Roslyn analyzers" |
| 10 | +author: "gewarren" |
| 11 | +ms.author: "gewarren" |
| 12 | +manager: ghogen |
| 13 | +ms.workload: |
| 14 | + - "dotnet" |
| 15 | +--- |
| 16 | +# Overview of .NET Compiler Platform analyzers |
| 17 | + |
| 18 | +Visual Studio 2017 includes a built-in set of .NET Compiler Platform analyzers that analyze your C# or Visual Basic code as you type. You can install additional analyzers as a Visual Studio extension, or on a per-project basis as a NuGet package. Analyzers look at code style, code quality and maintainability, code design, and other issues. |
| 19 | + |
| 20 | +If rule violations are found by an analyzer, they are reported both in the code editor as a *squiggly* under the offending code, and in the **Error List**. |
| 21 | + |
| 22 | +Many analyzer rules, or *diagnostics*, have one or more associated *code fixes* that you can apply to correct the problem. The analyzer diagnostics that are built into Visual Studio each have an associated code fix. Code fixes are shown in the light bulb icon menu, along with other types of *Quick Actions*. For information about these code fixes, see [Common Quick Actions](../ide/common-quick-actions.md). |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +## Roslyn analyzers vs. static code analysis |
| 27 | + |
| 28 | +.NET Compiler Platform ("Roslyn") analyzers will eventually replace [static code analysis](../code-quality/code-analysis-for-managed-code-overview.md) for managed code. Many of the static code analysis rules have already been rewritten as Roslyn analyzer diagnostics. |
| 29 | + |
| 30 | +Like static code analysis rule violations, Roslyn analyzer violations appear in the **Error List**. In addition, Roslyn analyzer violations also show up in the code editor as *squigglies* under the offending code. The color of the squiggly depends on the [severity setting](../code-quality/use-roslyn-analyzers.md#rule-severity) of the rule. The following screenshot shows three violations—one red, one green, and one gray: |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +Roslyn analyzers analyze code at build time, like static code analysis if it's enabled, but also live as you type! Roslyn analyzers can also provide design-time analysis of code files that aren't open in the editor if you enable [full solution analysis](../code-quality/how-to-enable-and-disable-full-solution-analysis-for-managed-code.md#to-toggle-full-solution-analysis). |
| 35 | + |
| 36 | +> [!NOTE] |
| 37 | +> Build-time errors and warnings from Roslyn analyzers are shown only if the analyzers are installed as a NuGet package. |
| 38 | +
|
| 39 | +Not only do Roslyn analyzers report the same types of problems that static code analysis does, but they make it easy for you to fix one, or all, occurrences of the violation in your file or project. These actions are called *code fixes*. Code fixes are IDE-specific; in Visual Studio, they are implemented as [Quick Actions](../ide/quick-actions.md). Not all analyzer diagnostics have an associated code fix. |
| 40 | + |
| 41 | +> [!NOTE] |
| 42 | +> The menu option **Analyze** > **Run Code Analysis** applies only to static code analysis. Additionally, on a project's **Code Analysis** property page, the **Enable Code Analysis on Build** and **Suppress results from generated code** checkboxes apply only to static code analysis. They have no effect on Roslyn analyzers. |
| 43 | +
|
| 44 | +To differentiate between violations from Roslyn analyzers and static code analysis in the **Error List**, look at the **Tool** column. If the Tool value matches one of the analyzer assemblies in **Solution Explorer**, for example **Microsoft.CodeQuality.Analyzers**, the violation comes from a Roslyn analyzer. Otherwise, the violation originates from static code analysis. |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +## NuGet package vs. extension |
| 49 | + |
| 50 | +.NET Compiler Platform analyzers can be installed per-project via a NuGet package, or Visual Studio-wide as a Visual Studio extension. There are some key behavior differences between these two methods of [installing analyzers](../code-quality/install-roslyn-analyzers.md). |
| 51 | + |
| 52 | +### Scope |
| 53 | + |
| 54 | +If you install analyzers as a Visual Studio extension, they apply at the solution level, to all instances of Visual Studio. If you install the analyzers as a NuGet package, which is the preferred method, they apply only to the project where the NuGet package was installed. In team environments, analyzers installed as NuGet packages are in scope for *all developers* that work on that project. |
| 55 | + |
| 56 | +### Build errors |
| 57 | + |
| 58 | +To have rules enforced at build time, including through the command line or as part of a continuous integration (CI) build, install the analyzers as a NuGet package. Analyzer warnings and errors don't show up in the build report if you install the analyzers as an extension. |
| 59 | + |
| 60 | +The following screenshot shows the command-line build output from building a project that contains an analyzer rule violation: |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +### Rule severity |
| 65 | + |
| 66 | +You cannot set the severity of rules from analyzers that were installed as a Visual Studio extension. To configure [rule severity](../code-quality/use-roslyn-analyzers.md#rule-severity), install the analyzers as a NuGet package. |
| 67 | + |
| 68 | +## Next steps |
| 69 | + |
| 70 | +- [Install Roslyn analyzers in Visual Studio](../code-quality/install-roslyn-analyzers.md) |
| 71 | +- [Use Roslyn analyzers in Visual Studio](../code-quality/use-roslyn-analyzers.md) |
| 72 | + |
| 73 | +## See also |
| 74 | + |
| 75 | +- [Quick Actions in Visual Studio](../ide/quick-actions.md) |
| 76 | +- [Write your own Roslyn analyzer](../extensibility/getting-started-with-roslyn-analyzers.md) |
| 77 | +- [.NET Compiler Platform SDK](/dotnet/csharp/roslyn-sdk/) |
0 commit comments