源代码分析器概述Overview of source code analyzers

.NET Compiler Platform(“Roslyn”)代码分析器检查 C# 或 Visual Basic 代码的样式、质量和可维护性、设计及其他问题。.NET Compiler Platform ("Roslyn") code analyzers inspect your C# or Visual Basic code for style, quality and maintainability, design, and other issues.

  • 某些分析器内置于 Visual Studio 中。Some analyzers are built in to Visual Studio. 对于这些分析器,诊断 ID 或代码的格式为 IDExxxx,如 IDE0067。The diagnostic ID, or code, for these analyzers is of the format IDExxxx, for example, IDE0067. 大多数这些内置分析器会检查代码样式,并且你可以在文本编辑器选项页上或在 EditorConfig 文件中配置首选项。Most of these built-in analyzers inspect code style, and you can configure preferences on the text editor options page or in an EditorConfig file. 一小部分内置分析器会检查代码质量。A handful of built-in analyzers look at code quality.

  • 可以将其他分析器作为 NuGet 包或 Visual Studio 扩展进行安装。You can install additional analyzers as a NuGet package or a Visual Studio extension. 例如:For example:

如果分析器发现规则冲突,将在代码编辑器(违规代码下方有波浪线)和“错误列表”窗口中报告 。If rule violations are found by an analyzer, they're reported in the code editor (as a squiggle under the offending code) and in the Error List window.

许多分析器规则或诊断都有一个或多个相关的代码修复程序,可以应用它们来纠正问题 。Many analyzer rules, or diagnostics, have one or more associated code fixes that you can apply to correct the problem. Visual Studio 中内置的每个分析器诊断都有关联的代码修复。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.

分析器冲突和快速操作代码修复

源代码分析与传统分析Source code analysis versus legacy analysis

Roslyn 分析器进行的源分析会替换针对托管代码的传统分析Source analysis by Roslyn analyzers replaces legacy analysis for managed code. 许多传统代码分析规则都已被重新编写为 Roslyn 代码分析器。Many of the legacy analysis rules have already been rewritten as Roslyn code analyzers. 对于更新的项目模板(如 .NET Core 和 .NET Standard 项目),传统分析甚至不可用。For newer project templates such as .NET Core and .NET Standard projects, legacy analysis is not even available.

与传统的分析规则冲突一样,源代码分析冲突会出现在 Visual Studio 的“错误列表”窗口中。Like legacy analysis rule violations, source code analysis violations appear in the Error List window in Visual Studio. 此外,源代码分析冲突也会在代码编辑器中显示,表现为违规代码下有波浪线 。In addition, source code analysis violations also show up in the code editor as squiggles under the offending code. 规则的严重性设置决定波浪线的颜色。The color of the squiggle depends on the severity setting of the rule. 以下图像显示了三个冲突—一个为红色,一个为绿色,一个为灰色:The following image shows three violations—one red, one green, and one gray:

Visual Studio 中代码编辑器中的波浪线

代码分析器在生成时检查代码,比如传统分析(如果启用),但还可在你键入时保持运行状态。Code analyzers inspect code at build time, like legacy analysis if it's enabled, but also live as you type. 如果启用完整的解决方案分析,代码分析器还提供未在编辑器中打开的代码文件的设计时分析。If you enable full solution analysis, code analyzers also provide design-time analysis of code files that aren't open in the editor.

Tip

仅当分析器作为 NuGet 包安装时,才会显示来自代码分析器的生成时错误和警告。Build-time errors and warnings from code analyzers are shown only if the analyzers are installed as a NuGet package. 内置分析器(例如 IDE0067 和 IDE0068)不会在生成期间运行。The built-in analyzers (for example, IDE0067 and IDE0068) never run during build.

Roslyn 代码分析器不仅会报告传统分析也会报告的相同类型的问题,而且还可以用于轻松修复文件或项目中一次冲突或所有。Not only do Roslyn code analyzers report the same types of problems that legacy 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. 代码修复是特定于 IDE 的;在 Visual Studio 中,它们以快速操作的方式实现。Code fixes are IDE-specific; in Visual Studio, they're implemented as Quick Actions. 并非所有分析器诊断都有相关联的代码修复。Not all analyzer diagnostics have an associated code fix.

Note

“分析” > “运行代码分析”菜单选项仅适用于传统分析 。The Analyze > Run Code Analysis menu option applies only to legacy analysis.

若在“错误列表”中要区分来自代码分析器和传统分析的冲突,请查看“工具”列 。To differentiate between violations from code analyzers and legacy analysis in the Error List, look at the Tool column. 如果“工具”值与“解决方案资源管理器”中的某个分析器程序集(例如 Microsoft.CodeQuality.Analyzers)匹配,则该冲突来自代码分析器 。If the Tool value matches one of the analyzer assemblies in Solution Explorer, for example Microsoft.CodeQuality.Analyzers, the violation comes from a code analyzer. 否则,冲突源自传统分析。Otherwise, the violation originates from legacy analysis.

错误列表中的工具列

Tip

项目文件中的 RunCodeAnalysis MSBuild 属性仅适用于传统分析 。The RunCodeAnalysis MSBuild property in a project file applies only to legacy analysis. 如果安装分析器,请在项目文件中将 RunCodeAnalysis 设置为 false,以防止生成后运行传统分析 。If you install analyzers, set RunCodeAnalysis to false in your project file, to prevent legacy analysis from running after build.

<RunCodeAnalysis>false</RunCodeAnalysis>

NuGet 包与 VSIX 扩展NuGet package versus VSIX extension

可以通过 NuGet 包为每个项目安装 Roslyn 代码分析器。Roslyn code analyzers can be installed per-project via a NuGet package. 有些分析器还可用作 Visual Studio 扩展,在这种情况下,它们适用于在 Visual Studio 中打开的任何解决方案。Some are also available as a Visual Studio extension, in which case they apply to any solution you open in Visual Studio. 这两种安装分析器方法之间存在一些关键行为差异。There are some key behavior differences between these two methods of installing analyzers.

范围Scope

如果将分析器安装为 Visual Studio 扩展,则它们将在解决方案级别应用于 Visual Studio 的所有实例。If you install analyzers as a Visual Studio extension, they apply at the solution level and to all instances of Visual Studio. 如果将分析器安装为 NuGet 包(这是首选方法),它们仅适用于安装了 NuGet 软件包的项目。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. 在团队环境中,作为 NuGet 包安装的分析器适用于处理该项目的所有开发人员 。In team environments, analyzers installed as NuGet packages are in scope for all developers that work on that project.

生成错误Build errors

要在生成时强制执行规则,包括通过命令行执行或作为持续集成 (CI) 生成的一部分来执行,请将分析器安装为 NuGet 包。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.

以下图像显示了生成包含分析器规则冲突的项目时的命令行生成输出:The following image shows the command-line build output from building a project that contains an analyzer rule violation:

带规则冲突的 MSBuild 输出

规则严重性Rule severity

无法配置作为 Visual Studio 扩展安装的分析器的规则严重性。You cannot configure the severity of rules from analyzers that were installed as a Visual Studio extension. 若要配置规则严重性,则应将分析器安装为 NuGet 包。To configure rule severity, install the analyzers as a NuGet package.

后续步骤Next steps

请参阅See also