FxCop custom rules do not have a name in VS2012 (but "official" rules have a name)

I am trying to migrate our custom FxCop rules (= Code Analysis) from Visual Studio 2010 to Visual Studio 2012.

While they work fine, with one exception: their name is empty in the results window ( View โ†’ Other windows โ†’ Code Analysis ), only the CheckId and Resolution tags CheckId , while the name is displayed for Microsoft rules:

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

What is strange is that the name is displayed in the rule editor:

The name is visible on both rules in the ruleset editor

What is wrong with my rules?

More details

This is how both rules appear in the output of MSBuild (there is something suspicious, but I donโ€™t understand why I have a different message):

 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 

Here is the rule declaration in the XML file for my rule:

 <?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> 

And here is the rule declaration in the XML rule for Microsoft.

 <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> 
+6
source share
1 answer

This is because the rule name (in contrast to its class type name) is not contained in the violation message in the FxCop report generated by the code run. Theoretically, Visual Studio could (and probably should) look up the name of the rule under the <Rules> node in the report before it starts looking elsewhere. However, this is not in VS 2012. Instead, it retrieves only the rule names from the rule information obtained from the downloaded rule collections.

The difficulty here is that for the purpose of displaying the user interface, Visual Studio does a completely separate search and loading of rule assemblies than is done in code analysis runs. The latter is accomplished by running fxcopcmd.exe with a command line generated using the MSBuild properties. The first uses completely separate logic built into Visual Studio plugins for code analysis, and does not take into account the rules at the project level or ruleset files. (This seems to be done using the Microsoft.VisualStudio.CodeAnalysis.ManagedRuleProvider class in the CodeAnalysis.dll assembly, if you are interested in learning more about gory.)

So ... You have two options if you want to see the names of the rules:

  • Place your rule assembly in the <VS install directory>\Team Tools\Static Analysis Tools\FxCop\Rules folder so that it gets into Visual Studio.

  • Add the registry HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\11.0\Code Analysis\FxCopRulePath node (create a new key named Code Analysis with a space instead of the existing CodeAnalysis ) with a value similar to the one below to allow both your rules and the built-in rules for displaying their names: %FxCopDir%\Rules;C:\Foo\Bar\YourRulesFolder

+6
source

Source: https://habr.com/ru/post/950569/


All Articles