内置代码分析器与 NuGet 包

Built-in code analysers vs NuGet packages

刚换VS2019,正在摸索要不要用代码分析。在项目属性的“代码分析”选项卡中,有许多内置的 Microsoft 规则集,当我的代码违反其中一条规则时,我可以看到编辑器出现波浪线。我可以自定义这些规则集并“另存为”以创建我自己的规则集。

我还看到了代码分析器 NuGet 包,例如“Roslynator”和“StyleCop.Analyzers”。这些和内置的 MS 规则有什么区别?真的只是为了更全面的套路rules/more选择吗?

如果我想坚持使用内置的 MS 规则,有什么限制吗?例如。他们还会得到 运行 并在 TFS/Azure DevOps 构建期间得到报告吗?

What's the difference between legacy FxCop and FxCop analyzers?

Legacy FxCop runs post-build analysis on a compiled assembly. It runs as a separate executable called FxCopCmd.exe. FxCopCmd.exe loads the compiled assembly, runs code analysis, and then reports the results (or diagnostics).

FxCop analyzers are based on the .NET Compiler Platform ("Roslyn"). You install them as a NuGet package that's referenced by the project or solution. FxCop analyzers run source-code based analysis during compiler execution. FxCop analyzers are hosted within the compiler process, either csc.exe or vbc.exe, and run analysis when the project is built. Analyzer results are reported along with compiler results.

Note

You can also install FxCop analyzers as a Visual Studio extension. In this case, the analyzers execute as you type in the code editor, but they don't execute at build time. If you want to run FxCop analyzers as part of continuous integration (CI), install them as a NuGet package instead.

https://docs.microsoft.com/en-us/visualstudio/code-quality/fxcop-analyzers-faq?view=vs-2019

因此,内置的遗留 FxCop 和 NuGet 分析器仅在构建时 运行,而扩展分析器可以 运行 同时 JIT 编译器在您键入时执行。此外,您必须特别说明 运行 构建时遗留代码分析,而 NuGet 分析器将 运行 构建只是因为它们已安装。当您转到菜单选项“运行 代码分析”时,作为 NuGet 或扩展安装的分析器不会 运行。

至少,那是我从那个页面中得到的。

该页面底部附近有一个 link,可带您了解哪些代码分析规则已移至新分析器,包括现已弃用的规则。

https://docs.microsoft.com/en-us/visualstudio/code-quality/fxcop-rule-port-status?view=vs-2019

不同的分析器试图涵盖不同的编码风格和 Microsoft 在构建 FxCop 时没有涵盖的内容。爱丽丝,根据我刚刚对此所做的少量研究,有一个完整的兔子洞需要跟随,这将花费比我现在投入更多的时间。而且它似乎充满了许多神秘的知识和强迫症风格的代码挑剔,使仙境看起来很正常。但这只是我的意见。

对于这些和基本 Microsoft 规则中的各种规则,有很多个人和专业意见,因此有足够的空间来使用你想要的,禁用你不需要的。对于初学者,我建议一次只打开几条规则。这样,您就不会被比您可能拥有的代码行更多的警告和错误所淹没。好的,所以这可能有点夸张,但是有太多的规则确实是吹毛求疵的,尤其是在遗留代码上,它们真的不值得启用,因为您可能没有时间修复它全部。当您决定启用什么时,您还需要进行基础研究并使用“常识”。 (“我真的需要担心一个应用程序在 15 年多的时间里被移植到 4 种不同的语言并且有 10k 个文件的可变大写编码风格的一致性吗?”)这既是个人观点也是专业观点,所以不管是否遵循。

并且不要忘记相互矛盾的规则。处理起来很有趣......