SPDisposeCheck How to suppress a false positive

21 Jan

Recently I asked a question on Twitter on how to suppress a false positive with the SPDispose check tool and Wictor Wilén pointed me to the right direction.
Several times I’m asked about the solution so I’m sharing it with you now. 

First of all you have to download the SPDisposeCheck tool and the SPDisposeCheckRules for Visual Studio 2010.
SPDisposeCheck tool: http://download.microsoft.com/download/B/4/D/B4D279A0-E159-40BF-A5E8-F49ABDBE95C7/SPDisposeCheck.msi
SPDisposeCheckRules for Visual Studio 2010: http://spdisposecheckstatic.codeplex.com/releases 

After downloading install the SPDisposeCheck tool on your machine and extract the zip file for the rules. The zip file contains a readme-vs2010.txt with instructions. Follow these instructions. 

The SPDisposeCheck tool is installed on the following location:
C:\Program Files (x86)\Microsoft\SharePoint Dispose Check 

At this location a zip file called SPDisposeExamplesSource.zip can be found. In this zip file several classes are stored, one of them is the class SPDisposeCheckIgnoreAttribute.cs.

To suppress a false positive you have to include this class in your project.

After including the attribute to suppress a false positive can be used:

[SPDisposeCheckIgnoreAttribute(SPDisposeCheckID.SPDisposeCheckID_110, "Don't want to do it")]
public void CreatingSPSiteLeak()
{
    SPSite siteCollection = new SPSite("http://moss");
    // siteCollection leaked
}

The above example is an example from another class in the zip file: SPSiteLeak.cs