Jun 02 2009

Microsoft FxCop doesn’t like Microsoft generated code!

The other day I thought it might be nice to “do the right thing” and give my code a run against Microsoft’s FxCop.

I ran it right out of the box - I didn’t bother making my own rules or changing the defaults. I was bored. Anyway, here’s one of the results that actually made be chuckle once I read it carefully:

Warning, Certainty 90, for DoNotInitializeUnnecessarily
{

Target : #.ctor() (IntrospectionTargetMember)
Location : <735>> (String)
Resolution : “‘MyDocument.New()’ initializes field ‘MyDocument.disposedValue’
of type ‘Boolean’ to false. Remove this initialization
because it will be done automatically by the runtime.”

Help : (String)
Category : Microsoft.Performance (String)
CheckId : CA1805 (String)
RuleFile : Performance Rules (String)
Info : “Do not make initializations that have already been
done by the runtime.”
Created : 3/6/2009 6:58:21 PM (DateTime)
LastSeen : 3/6/2009 8:36:25 PM (DateTime)
Status : Active (MessageStatus)
Fix Category : NonBreaking (FixCategories)

}

The reason for the chuckle was that the code which triggered this violation of the rule was written by the Microsoft IDE! My role in this infraction was really quite simple: I typed “Implements IDisposable” and hit Enter. The IDE was “nice” enough to plugin the rest for me:

[vb language=".net"]

‘ To detect redundant calls
Private disposedValue As Boolean = False

‘ IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
Array.Clear(mDocumentContent, 0, mDocumentContent.Length)
End If
End If
Me.disposedValue = True
End Sub
[/vb]

It’s bad enough that the IDE writes code for me without prompting, but maybe the FxCop team should talk to the IDE team to avoid such embarrassing nuisances (I had dozens of similar warnings to weed through) in the future.

According to the Code Analysis Team, this is the new default for FxCop 1.36.

Here’s how to avoid it:

Using an FxCop project:

  1. Open your FxCop project in FxCop
  2. Choose Project -> Options -> Spelling & Analysis
  3. Check Suppress analysis results against generated code
  4. Click OK

OR, if you prefer the command-line:

  1. Pass the /ignoregeneratedcode switch, for example:

FxCopCmd.exe /file:MyAssembly.dll /out:AnalysisResults.xml /ignoregeneratedcode

Blog Traffic Exchange Related Posts Blog Traffic Exchange Related Websites
  • Establishing Remodel Cost: Home Remodeling Critical First Step Nothing ruins a fantastic remodel more quickly than exceeding the project budget.  Why is this phenomenon so common in residential remodeling?  Simple:  Many homeowners struggle to establish a detailed budget at the appropriate time in the course of the project. Why is establishing remodel cost one of your most important......
  • Windows Mobile, iPhone, Android - Marketplace Comparison Detailed comparison between Windows Mobile Marketplace, Apple's iPhone AppStore and Android Market from developer point of view. By: Predrag Tomasevic (Click for bigger image) Introduction The article you are about to read is my compilation of thoughts on three currently most popular and talked about mobile platforms, sparked by whole......
  • The Complete Home Improvement Perspective There are lots of people these days that consider themselves good handy people.  These people are fond of bragging about their complete home improvement skills and all of the projects they have managed to put together.  If you are listening to some of these people, you may just be tempted......
  • Can Google challenge Microsoft's dominance in 2010? In 2009, Google Inc. and Microsoft Corp. were fighting for each other's market share and revenue stream by introducing new products and updated versions of existing ones. Various analysts predict that the year 2010 will see more action between the two giants as they compete in the fields of Internet......
  • Generate PDF from HTML with C# in ASP.NET PDF Duo .Net is a converting component for use in ASP.NET (VB, C# etc.) and enables toconvert HTML to PDF. The main class HtmlToPdf provides several methods and properties to enable multi-purpose customization of the resulting PDF. Main functions allow to convert HTML represented as a File, Page from Url......

Leave a Reply