Search code examples
fxcopstylecopsuppress-warningsservice-reference

Stylecop Suppress errors for Reference.cs file


I have just asked a similar question regarding auto generated code from Service References and Xml summary errors. An answer was posted suggesting that I could add the Service Reference into its own project and then it can avoid Xml summary warnings and Stylecop errors completely.

But I realised that I was able to get around the Xml problem by setting the Service Reference to Internal and am now stuck on the StyleCop errors.

I am using FxCop version 10.0.

In the properties of my project, in the Code Analysis tab, I have checked the option for

Suppress results from generated code (managed only)

Also, from looking at the generated code in the Reference.cs file, I see that the code is decorated with the following attribute:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]

..which Stylecop should then skip, right? Although this is not working, I am still getting StyleCop errors for the generated code.

Edit: The following header can also be found in the generated code:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18033
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

So why isn't it being ignored?

I also found this question and tried using the following in my StyleCop setings:

<CollectionProperty Name="GeneratedFileFilters">
    <Value>Reference\.cs$</Value>
 </CollectionProperty>

But this doesn't work either, I have been struggling with the regex but tried to add the following values to the above example to get Stylecop to ignore my generated code.

Reference.cs
\Reference\.cs
\Reference\.cs$
Reference\.cs$

Does anyone know how to suppress all Stylecop errors for a Reference.cs file?

Edit2: The errors being displayed for the Reference.cs file:

Error 1 CA1812 : Microsoft.Performance : ... is an internal class that is apparently never instantiated. If so, remove the code from the assembly. If this class is intended to contain only static methods, consider adding a private constructor to prevent the compiler from generating a default constructor.

Error 2 CA2239 : Microsoft.Usage : Add a 'private void OnDeserialized(StreamingContext)' method to type ... and attribute it with the System.Runtime.Serialization.OnDeserializedAttribute.

Error 4 CA1811 : Microsoft.Performance : ... appears to have no upstream public or protected callers.

I have 27 of these 3 types of errors.

Edit 3: I have Style/Fx cop setup to run on build. I have added CODE_ANALYSIS in the Conditional compilation symbols in the build tab and

"$(ProgramFiles)\Microsoft FxCop 10.0\FxCopCmd.exe" /file:"$(TargetPath)" /ruleid:-"Microsoft.Design#CA1006" /ruleid:-"Microsoft.Design#CA1020" /console /cul:en-GB /q /assemblyCompareMode:StrongNameIgnoringVersion

... in the Post-build event command line in the Build Events tab.


Solution

  • The CAxxxx errors are from FxCop, not StyleCop. If they are appearing for generated code, and you want to keep the generated code without changing the generator, simply add project-level exclusions (e.g.: in GlobalSuppressions.cs).

    Following question edit #3: Since you're running fxcopcmd.exe "manually", your C# project setting for ignoring generated code isn't being used at all. You'll need to add the /ignoregeneratedcode switch to your fxcopcmd.exe command line to get this to be applied.