.NET Core 静态代码分析工具

Static code analysis tool for .NET Core

我正在尝试为新的 .NET Core 寻找静态代码分析工具。有 ReSharper,但我认为 .NET Core 支持还没有真正存在。我不确定其他替代方案?

我通过 的搜索和其他调查得出的结论是,我们必须等到 Microsoft、Jetbrains 或其他公司的工具准备就绪。

在Resharper 2016.2(RC版现已推出)中,一些初步工作已经完成;但针对 post 2016.2 版本宣布了单元测试和代码分析。我渴望抢先体验版。

NDepend 和 Microsoft-Tooling 今天也缺乏支持。我希望在今年年底之前看到这个工具。

如何使用 .Net 核心分析器执行此操作...

今天我花了几个小时自己解决这个问题。我的回答并不权威,你的里程可能会有所不同。

第 1 步。将 nuget Microsoft.NetCore.Analysis 安装到您的 .Net Core 项目中。 (代码分析现在可以工作了)。

第 2 步:(几乎不可避免)配置规则

在 NetFx 应用程序中,您可以右键单击分析器,然后编辑您当前的规则集。但是,在 .Net Core 中,您必须手动执行此操作 (AFAIK)。

一个。在您的项目文件旁边创建一个文件 {Projectname}.ruleset

b。在您的项目中包含 *.ruleset 文件并将构建操作设置为“C# 分析器附加文件”(如果您使用其他语言,请自行翻译)。

c。编辑您的项目文件并包括:
<CodeAnalysisRuleSet>{ProjectName}.ruleset</CodeAnalysisRuleSet> 在您的项目文件中。

步骤 3+ 冲洗并重复每个项目...

(我把它放在 <TargetFramework>netcoreapp2.1\</TargetFramework> 下面,但它也适用于单独的构建目标环境)。

对于习惯在NetFx中这样做的人来说,<runcodeanalysis>true</runcodeanalysis>既不需要,也没有任何影响。

但是,你说,"I dont' have a ruleset to start from, how do I get started",我也没有。这是我的手册,使用 *.ruleset 文件的内容风险自负:

<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Default Code Analysis Rules for .Net Core projects" Description="Rules for {ProjectName}.csproj." ToolsVersion="15.0">
   <Rules AnalyzerId="Microsoft.NetCore.Analyzers" RuleNamespace="Microsoft.NetCore.Analyzers">    
    <!--CA1304: Specify CultureInfo -->
    <Rule Id="CA1304" Action="Warning" />
    <!--CA1305: Specify IFormat provider -->
    <Rule Id="CA1305" Action="Warning" />
    <!--CA1307: Specify StringComparison -->
    <Rule Id="CA1307" Action="Warning" />
    <!--CA1308: Normalize strings to uppercase -->
    <Rule Id="CA1308" Action="Warning" />
    <!--CA1401: P/Invokes should not be visible -->
    <Rule Id="CA1401" Action="Warning" />
    <!--CA1813: Avoid unsealed attributes -->
    <Rule Id="CA1813" Action="Warning" />
    <!--CA1816: Dispose methods should not call SuppressFinalize -->
    <Rule Id="CA1816" Action="Warning" />
    <!--CA1820: Test for empty strings using string length -->
    <Rule Id="CA1820" Action="Warning" />
    <!--CA1826: Do not use Enumerable methods on indexable collections. Instead use the collection directly -->
    <Rule Id="CA1826" Action="Warning" />
    <!--CA2002: Do not lock on objects with weak identity -->
    <Rule Id="CA2002" Action="Warning" />
    <!--CA2008: Do not create tasks without passing a TaskScheduler -->
    <Rule Id="CA2008" Action="Warning" />
    <!--CA2009: Do not call ToImmutableCollection on an ImmutableCollection -->
    <Rule Id="CA2009" Action="Warning" />
    <!--CA2101: Specify marshaling for P/Invoke string arguments -->
    <Rule Id="CA2101" Action="Warning" />
    <!--CA2208: Instantiate argument exceptions correctly -->
    <Rule Id="CA2208" Action="Warning" />
    <!--CA2216: Disposable types should declare finalizer -->
    <Rule Id="CA2216" Action="Warning" />
    <!--CA2241: Provide correct arguments to formatting methods -->
    <Rule Id="CA2241" Action="Warning" />
    <!--CA2242: Test for NaN correctly-->
    <Rule Id="CA2242" Action="Warning" />
    <!--CA2243: Attribute string literals should parse correctly -->
    <Rule Id="CA2243" Action="Warning" />
    <!--CA9999: Analyzer version mismatch -->
    <Rule Id="CA9999" Action="Warning" />
  </Rules>

  <Rules AnalyzerId="Microsoft.NetCore.CSharp.Analyzers" RuleNamespace="Microsoft.NetCore.CSharp.Analyzers">
    <!--CA1309: Use ordinal StringComparison -->
    <Rule Id="CA1309" Action="Warning" />
    <!--CA1414: Mark boolean PInvoke arguments with MarshalAs -->
    <Rule Id="CA1414" Action="Warning" />
    <!--CA1601: Do not use timers that prevent power state changes -->
    <Rule Id="CA1601" Action="Warning" />
    <!--CA1810: Initialize reference type static fields inline -->
    <Rule Id="CA1810" Action="Warning" />
    <!--CA1824: Mark assemblies with NeutralResourcesLanguageAttribute -->
    <Rule Id="CA1824" Action="Warning" />
    <!--CA1825: Avoid zero-length array allocations -->
    <Rule Id="CA1825" Action="Warning" />
    <!--CA2010: Always consume the value returned by methods marked with PreserveSigAttribute -->
    <Rule Id="CA2010" Action="Warning" />
    <!--CA2201: Do not raise reserved exception types -->
    <Rule Id="CA2201" Action="Warning" />
    <!--CA2205: Use managed equivalents of win32 api -->
    <Rule Id="CA2205" Action="Warning" />
    <!-- CA2207: Initialize value type static fields inline -->
    <Rule Id="CA2207" Action="Warning" />
    <!--CA2215: Dispose Methods Should Call Base Class Dispose -->
    <Rule Id="CA2215" Action="Warning" />
    <!--CA5350: Do Not Use Weak Cryptographic Algorithms (TripleDES, SHA-1, RIPEMD160)-->
    <Rule Id="CA5350" Action="Warning" />
    <!--CA5350: Do Not Use Broken Cryptographic Algorithms (MD5, DES, RC2)-->
    <Rule Id="CA5351" Action="Warning" />
  <Rules>
</RuleSet>