gpt4 book ai didi

visual-studio-2012 - FxCop 自定义规则在 VS2012 中没有名称(但 "official"规则有名称)

转载 作者:行者123 更新时间:2023-12-04 15:00:00 25 4
gpt4 key购买 nike

我正在尝试将我们的自定义 FxCop(= 代码分析)规则从 Visual Studio 2010 迁移到 Visual Studio 2012。

到目前为止,他们工作正常,除了一件事:他们的名字在结果窗口中是空白的( View -> Other windows -> Code Analysis ),只有 CheckIdResolution出现,而名称显示为 Microsoft 的规则:

The name isn't visible on the right of the CheckId on the custom rule, but is visible on the Microsoft rule.

奇怪的是,该名称在规则集编辑器中可见:

The name is visible on both rules in the ruleset editor

我的规则有什么问题?

细节

以下是 MSBuild 的输出中显示这两条规则的方式(那里有些可疑,但我不明白为什么我有不同的消息):

6>c:\Users\Me\MySolution\MyProject\Program.cs(15): warning : CA1804 : Microsoft.Performance : 'Foo<T>.SomeMethod()' declares a variable, 'z', of type 'int', which is never used or is only assigned to. Use this variable or remove it.
6>MSBUILD : warning : CF1001 : CustomRules.ThreadSafety : The public Public Field "Tests.Program.Foo" must be preceded of readonly

这是我的规则的 XML 文件中的规则声明:
<?xml version="1.0" encoding="utf-8" ?>
<Rules FriendlyName="CustomRules">
<Rule TypeName="PublicFieldsMustBeReadonly" Category="CustomRules.ThreadSafety" CheckId="CF1001">
<Name>Public Fields must be readonly or must be replaced with a Getter/Setter Method.</Name>
<Description>Public Fields must be readonly or must be replaced with a Getter/Setter Method.</Description>
<GroupOwner>MyCompany</GroupOwner>
<DevOwner>Me</DevOwner>
<Owner>Me</Owner>
<Url>http://example.com</Url>
<Resolution>The public Public Field "{0}" must be preceded of readonly</Resolution>
<Email>my@email.com</Email>
<MessageLevel Certainty="100">Warning</MessageLevel>
<FixCategories>Breaking</FixCategories>
</Rule>
</Rules>

这是 Microsoft 规则的 XML 中的规则声明。
<Rules FriendlyName="Performance Rules">
<Rule TypeName="RemoveUnusedLocals" Category="Microsoft.Performance" CheckId="CA1804">
<Name>
Remove unused locals
</Name>
<Description>
Remove locals that are not used or are only assigned to in method implementations.
</Description>
<Url>
@ms182278(VS.100).aspx
</Url>
<Resolution>
{0} declares a variable, {1}, of type {2}, which is never used or is only assigned to. Use this variable or remove it.
</Resolution>
<Email />
<MessageLevel Certainty="95">
Warning
</MessageLevel>
<FixCategories>
NonBreaking
</FixCategories>
<Owner />
</Rule>
</Rules>

最佳答案

发生这种情况是因为规则名称(与其类类型名称相反)未包含在代码分析运行生成的 FxCop 报告中的违规消息中。理论上,Visual Studio 可以(并且可能应该)在报告中的 <Rules> 节点下查找规则名称,然后再开始查找其他地方。但是,它在 VS 2012 中没有。相反,它仅从从加载的规则程序集中获取的规则信息中获取规则名称。

这里的棘手之处在于,出于 UI 显示的目的,Visual Studio 对规则程序集的搜索和加载与在代码分析运行中所做的完全不同。后者是通过使用从 MSBuild 属性生成的命令行运行 fxcopcmd.exe 来执行的。前者使用嵌入在 Visual Studio 插件中的完全独立的代码分析逻辑,并且不考虑项目级别的规则位置或规则集文件。 (这似乎是通过 Microsoft.VisualStudio.CodeAnalysis.ManagedRuleProvider 程序集中的 CodeAnalysis.dll 类完成的,如果您对血腥细节感到好奇。)

所以...如果您想查看规则名称,您基本上有两种选择:

  • 将您的规则程序集放在 <VS install directory>\Team Tools\Static Analysis Tools\FxCop\Rules 文件夹中,以便 Visual Studio 获取它。
  • 添加一个 HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\11.0\Code Analysis\FxCopRulePath 注册表节点(创建一个名为 Code Analysis 的新键,带有空格,而不是使用现有的 CodeAnalysis ),其值如下所示,以允许您的规则和内置规则显示他们的名字:%FxCopDir%\Rules;C:\Foo\Bar\YourRulesFolder
  • 关于visual-studio-2012 - FxCop 自定义规则在 VS2012 中没有名称(但 "official"规则有名称),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17931969/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com