在VS2010,代码分析和SuppressMessage FxCop的行为 [英] FxCop behavior in VS2010, Code Analysis, and SuppressMessage

查看:302
本文介绍了在VS2010,代码分析和SuppressMessage FxCop的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个类:

  [System.Diagnostics.CodeAnalysis.SuppressMessage(Microsoft.Naming CA1709:IdentifiersShouldBeCasedCorrectly的MessageId =木卫一)] 
公共无效ParaQueFalleCalidadCodigoUnoIo_ReglaCA1709()
{

}

公共无效ParaQueFalleCalidadCodigoDosIo_ReglaCA1709()
{

}

我使用自定义的规则集文件<强> CustomRules.ruleset

 <规则集名称=RulesNetToolsVersion =10.0> 
< RuleHintPaths>
<路径> C:\Fxcop10.0\Rules< /路径和GT;
< / RuleHintPaths>
<规则AnalyzerId =Microsoft.Analyzers.ManagedCodeAnalysisRuleNamespace =Microsoft.Rules.Managed>

<规则编号=CA1709行动=警告/>

< /规则与GT;
< /规则集>

当我运行内置的代码分析工具VS2010,我得到这样的警告:




CA1709:Microsoft.Naming:通过改变正确的木卫一的成员名字
'_Default.ParaQueFalleCalidadCodigoDosIo_ReglaCA1709()的外壳是
为IO。




现在,我可以使用同样的规则集文件的 CustomRules.ruleset FxCopCmd.exe 的:




FxCopCmd.exe / GAC /d:\"C:\CompanyFramework\4.0。 0.0/f:\"D:\TFS\Tests\WebApplication1\bin\WebApplication1.dll/o:\"resultsFxCop.xml/ruleset:\"=CustomRules.ruleset/ v




我得到的 2的错误(FixCategory断裂和级别的错误)



< BLOCKQUOTE>

CA1709 - 通过改变它正确的木卫一的成员名字
'_Default.ParaQueFalleCalidadCodigoUnoIo_ReglaCA1709()'的外壳
为IO



CA1709 - 通过改变它
为IO正确木卫一的成员名字
'_Default.ParaQueFalleCalidadCodigoDosIo_ReglaCA1709()'套管




 <消息ID =木卫一的TypeName =IdentifiersShouldBeCasedCorrectly类别=Microsoft.NamingCheckId =CA1709 状态=活动创建=10 2013年2月5日:24:01ZFixCategory =断> 
<邮品名称=会员确定性=85级=错误路径=D:\TFS\Tests\WebApplication1文件=Default.aspx.cs行=21 方式>通过将其更改为'IO'<正确木卫一的成员名字'_Default.ParaQueFalleCalidadCodigoUnoIo_ReglaCA1709()'的外壳; /发行>
< /信息>

<消息ID =木卫一的TypeName =IdentifiersShouldBeCasedCorrectly类别=Microsoft.NamingCheckId =CA1709状态=活动创建=2013年2月5日10:24: 01ZFixCategory =断>
<邮品名称=会员确定性=85级=错误路径=D:\TFS\Tests\WebApplication1文件=Default.aspx.cs行=26 方式>通过将其更改为'IO'<正确木卫一的成员名字'_Default.ParaQueFalleCalidadCodigoDosIo_ReglaCA1709()'的外壳; /发行>
< /信息>

在resultsFxcop.xml,我所看到的CA1709:IdentifiersShouldBeCasedCorrectly规则:

 <规则类型名=IdentifiersShouldBeCasedCorrectly类别=Microsoft.NamingCheckId =CA1709> 
<名称>标识符应正确<套管; /名称>
<说明与GT;类型,命名空间和...省略了< /说明>
<分辨率名称=会员方式> /分辨率>在成员名{0}改变为'{2}'< {1}更正的套管;
<所有者/>
< URL> HTTP://msdn.microsoft.com/library/ms182240(VS.100)的.aspx< / URL>
<电子邮件和GT; [无]< /电子邮件与GT;
< MessageLevel确定性=85>错误< / MessageLevel>
<文件名=namingrules.dll版本=10.0.0.0/>
< /规则>



MessageLevel为CA1709的规则:




 < MessageLevel确定性=85>错误< / MessageLevel> 




两个问题:




  • 我得到的错误,但CA1709规则操作使用FxCopcmd.exe警告

  • SuppressMessage被忽略



现在,我修改 CustomRules.ruleset 并执行我再次FxCopcmd.exe

 <规则标识=CA1709行动=无/> 



我得到的没有错误



我修改 CustomRules.ruleset 并执行我再次FxCopcmd.exe

 < ;规则编号=CA1709行动=忽略/> 



我得到同样的 2的错误



我需要使用FxCopCmd.exe和一个自定义规则集。




  • 是否SuppressMessage工程FxCopcmd.exe?

  • 为什么我如果动作警告错误,使用Fxcopcmd.exe?

  • 这是什么MessageLevel误差CA1709规则是什么意思?比规则操作警告更优先?



有什么建议?



更新



http://social.msdn.microsoft.com/Forums/en/vstscode/thread/3f8931da-9a4d-47a6-b331-8b6b07aea8d6



http://social.msdn.microsoft.com/forums/en-US/vstscode/thread/3cb6c50c-7095-4551-a4e3-a3cbc7cb85be



有关默认的FxCop规则,有修改的消息级别没有简单的方法,



MessageLevel 的是的重要性消息,例如:如果你有数以千计的邮件,这很可能是开始解决关键(惊叹号)的错误首先是一个好主意。



确定性的是规则写入分配给每个规则的数目,它是一个消息导致代码变化的可能性。这个数字是基于领域专家和放大器反馈建立起来的; 。客户如何在规则中使用的启发式能够避免误报



修复类别的:这表示如果修订为违规如果代码以前出货将是一个二进制的重大更改。例如你在它一个拼写错误,你已经运到客户库。您现在就开始在其上运行的FxCop,看到了拼写错误。 FxCop的会告诉你,这是一个重大更改。如果您修复拼写错误和船舶资料库的新版本的客户,他们无法在不改变和重新编译他们的代码使用库。所以,你可能要忽略的FxCop违反了这个API。在另一方面,如果你从来没有发货,这将是完全罚款来解决的FxCop冲突。


解决方案

请问SuppressMessage工程FxCopcmd.exe?




是的。你需要为你的SuppressMessage属性包含在您的装配与 CODE_ANALYSIS 为了定义编译符号编译。一旦他们在那里,的FxCop引擎会识别它们,无论用于运行分析机制。




为什么我得到的错误如果动作是警告,使用Fxcopcmd.exe?




写入总是FxCop的生产报告中的问题的水平使用由指定的级别规则作者。当您从Visual Studio中运行时,Visual Studio集成插件覆盖此水平在规则集中指定的。当您运行fxcopcmd.exe,一个配置规则作为警告VS错误之间的唯一区别是,一个错误级别的违反规则的检测会导致fxcopcmd.exe返回一个非零退出代码,从而使您能够打破自动化的构建。



如果您希望生成报告时fxcopcmd.exe使用级替代,你可能要考虑在的 http://visualstudio.uservoice.com/


I have a class like this one:

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Io")]
    public void ParaQueFalleCalidadCodigoUnoIo_ReglaCA1709()
    {

    }

    public void ParaQueFalleCalidadCodigoDosIo_ReglaCA1709()
    {

    }

I use a custom Ruleset file CustomRules.ruleset

<RuleSet Name="RulesNet" ToolsVersion="10.0">
  <RuleHintPaths>
    <Path>C:\Fxcop10.0\Rules</Path>
  </RuleHintPaths>
  <Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">

    <Rule Id="CA1709" Action="Warning" />

  </Rules>
</RuleSet>

When I run VS2010 built in Code Analysis tool, I get this warning:

CA1709 : Microsoft.Naming : Correct the casing of 'Io' in member name '_Default.ParaQueFalleCalidadCodigoDosIo_ReglaCA1709()' by changing it to 'IO'.

Now, I can use this same rule set file CustomRules.ruleset in FxCopCmd.exe:

FxCopCmd.exe /gac /d:"C:\CompanyFramework\4.0.0.0" /f:"D:\TFS\Tests\WebApplication1\bin\WebApplication1.dll" /o:"resultsFxCop.xml" /ruleset:"=CustomRules.ruleset" /v

I get 2 errors (FixCategory Breaking, and Level Error)

CA1709 - Correct the casing of 'Io' in member name '_Default.ParaQueFalleCalidadCodigoUnoIo_ReglaCA1709()' by changing it to 'IO'.

CA1709 - Correct the casing of 'Io' in member name '_Default.ParaQueFalleCalidadCodigoDosIo_ReglaCA1709()' by changing it to 'IO'.

  <Message Id="Io" TypeName="IdentifiersShouldBeCasedCorrectly" Category="Microsoft.Naming" CheckId="CA1709" Status="Active" Created="2013-02-05 10:24:01Z" FixCategory="Breaking">
             <Issue Name="Member" Certainty="85" Level="Error" Path="D:\TFS\Tests\WebApplication1" File="Default.aspx.cs" Line="21">Correct the casing of 'Io' in member name '_Default.ParaQueFalleCalidadCodigoUnoIo_ReglaCA1709()' by changing it to 'IO'.</Issue>
  </Message>

  <Message Id="Io" TypeName="IdentifiersShouldBeCasedCorrectly" Category="Microsoft.Naming" CheckId="CA1709" Status="Active" Created="2013-02-05 10:24:01Z" FixCategory="Breaking">
             <Issue Name="Member" Certainty="85" Level="Error" Path="D:\TFS\Tests\WebApplication1" File="Default.aspx.cs" Line="26">Correct the casing of 'Io' in member name '_Default.ParaQueFalleCalidadCodigoDosIo_ReglaCA1709()' by changing it to 'IO'.</Issue>
  </Message>

In resultsFxcop.xml, I have seen CA1709: IdentifiersShouldBeCasedCorrectly rule:

 <Rule TypeName="IdentifiersShouldBeCasedCorrectly" Category="Microsoft.Naming" CheckId="CA1709">
   <Name>Identifiers should be cased correctly</Name>
   <Description>Type, namespace, and... OMITED.</Description>
   <Resolution Name="Member">Correct the casing of '{0}' in member name {1} by changing it to '{2}'.</Resolution>
   <Owner />
   <Url>http://msdn.microsoft.com/library/ms182240(VS.100).aspx</Url>
   <Email>[none]</Email>
   <MessageLevel Certainty="85">Error</MessageLevel>
   <File Name="namingrules.dll" Version="10.0.0.0" />
  </Rule>

MessageLevel for CA1709 rule:

   <MessageLevel Certainty="85">Error</MessageLevel>

Two issues:

  • I get Errors but CA1709 rule Action is Warning
  • SuppressMessage is ignored using FxCopcmd.exe

Now, I modify CustomRules.ruleset and I execute FxCopcmd.exe again

<Rule Id="CA1709" Action="None" />

I get NO errors.

I modify CustomRules.ruleset and I execute FxCopcmd.exe again

<Rule Id="CA1709" Action="Ignore" />

I get the same 2 errors.

I need use FxCopCmd.exe and a custom ruleset.

  • Does SuppressMessage works for FxCopcmd.exe?
  • Why do I get errors if Action is Warning, using Fxcopcmd.exe?
  • What does MessageLevel Error for CA1709 rule mean? More priority than Rule Action "Warning"?

Any suggestions?

Update

http://social.msdn.microsoft.com/Forums/en/vstscode/thread/3f8931da-9a4d-47a6-b331-8b6b07aea8d6

http://social.msdn.microsoft.com/forums/en-US/vstscode/thread/3cb6c50c-7095-4551-a4e3-a3cbc7cb85be

For the default FxCop rules, there is no easy way to modify the message level,

MessageLevel is the importance of the message, e.g. if you had thousands of messages, it probably would be a good idea to start addressing the Critical (the exclamation mark) Errors first.

Certainty is the number the Rule writer assigns to each rule, it is the likelihood that a message leads to a code change. This number is built up based on feedback from domain experts & customers and how well the heuristic used in the rule is able to avoid false positives.

Fix Category: This indicates if the fix for a violation would be a binary breaking change if the code has previously shipped. e.g you have a library with a misspelling in it that you have already shipped to customers. You now start running FxCop on it and see the misspelling. FxCop will tell you this is a breaking change. If you fix the misspelling and ship a new version of your library to customers, they can't use the library without changing and recompiling their code. So you probably want to ignore the FxCop violation on this API. On the other hand, if you never shipped, it would be totally fine to fix the FxCop violation.

解决方案

Does SuppressMessage works for FxCopcmd.exe?

Yes. You will need to compile with the CODE_ANALYSIS compilation symbol defined in order for your SuppressMessage attributes to be included in your assembly. Once they're in there, the FxCop engine will recognize them, regardless of the mechanism used to run the analysis.

Why I get errors if Action is Warning, using Fxcopcmd.exe?

The issue level written to the FxCop-produced report always uses the level specified by the rule author. When you run from within Visual Studio, the Visual Studio integration plug-in overrides this level with the one specified in the ruleset. When you run fxcopcmd.exe, the only difference between a configuring a rule as a warning vs an error is that detection of an error-level rule violation will cause fxcopcmd.exe to return a non-zero exit code, thereby allowing you to break an automated build.

If you would prefer that fxcopcmd.exe use your level overrides when generating its report, you may want to consider making a suggestion at http://visualstudio.uservoice.com/.

这篇关于在VS2010,代码分析和SuppressMessage FxCop的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆