When working with the Team System Static Code Analyzer there are times when the tool will throw out an issue that isn’t something you are going to resolve. Of course when you first run the code analyzer it’s not uncommon to have a list of literally hundreds of potential issues. As I’ve discussed in the past, the best solution when this list is long is to use the configuration settings to limit the areas of focus to things like performance and security and then move on to other areas.
However, as you are processing your potential issues, the fact is that some of those items won’t be issues. It’s very possible that the tool might warn about code which in fact in the limited context where it is used is implemented correctly. As such it’s important to be able to make those issues disappear from the overall list. In fact let me provide some sample code:
private void stackButton_Click(object sender, System.EventArgs e)
{
foreach (ToolStripItem item in this.stackStrip.Items)
{
if (item != sender && item is ToolStripButton)
{
((ToolStripButton)item).Checked = false;
}
}
}
So if you take the method above you’ll see that it is referencing an untyped collection of items, and by reviewing the ‘if’ statement we can tell that it is looking for one of multiple possible types in that collection and then casting and using objects of that type. Pretty straightforward and in the context of what it does I don’t see a better way of handling what it’s doing (you are welcome to educate me in the comments section…)
When I run this code through static analysis what I get is the following performance related warning:
CA1800 : Microsoft.Performance : 'item', a local, is cast to type 'System.Windows.Forms.ToolStripButton' multiple times in method StackView.stackButton_Click(Object, EventArgs):Void. Cache the result of the 'as' operator or direct cast in order to eliminate the redundant castclass instruction.
However if you look at the code the fact the casting from a functional standpoint is done only one. There’s no real redundancy of the casting instructions and no reason to cache the results. So what should we do, well we should indicate in the code that this condition has been reviewed and isn’t something we want to be warned about. Right clicking the error in the error list I select the “Suppress Message(s)” option from the context menu and the current warning is shown with a strike-through font. On subsequent builds/checks the error is not displayed.
Which begs the question of how does Team System implement this rule exception? The answer is that it adds an attribute to my source code. This attribute is actually a method call which tells the code analysis engine to ignore one or more rules in the scope of the current method call. Below is the instance of the SuppressMessage call attribute for this example message.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
This call allows the code analysis to recognize which message has been suppressed in future runs, and because it’s part of the source code the suppression is checked into source safe and easily recognized by others looking at the code. In addition to the SuppressMessage method there is a SuppressMessageAttribute method which has the following signature:
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
This example of that call is for a different message generated by migrating code from the Beta versions of VS2005 to the release version… in this case I want more time to investigate the implications of making the change suggested by the message to ensure that I eventually do this I can add a task to my TFS project, but that’s a story for another post.