Static Code Analysis - Part 3 - Suppress Messages
[NOTE: Please log into Team System Rocks! to be able to see the images. ]
For background, please see Part 1 and Part 2 in this series of posts.
It could be that you want a particular static code analysis rule to continue to be used and run, but that it doesn't apply to a particular piece of code for some reason. In my example, I'm going to say that the 6th warning, CA1822, just doesn't apply in this case. I want to continue to use rule CA1822, but I don't want the Subtract method to continue to trip this warning.
To solve this problem, I want to suppress this warning message for this particular piece of code. To do that, I right-click on the warning, and select Suppress Message(s). This will take you to the offending code, and adds an attribute above the method:
This attribute makes sure that for the Microsoft.Performance category, and the CS1822:MarkMembersAsStatic rule, that this method, Subtract, will not trigger this warning when static code analysis is run. You can have multiple SuppressMessage attributes if need be.
If I save my code, and rebuild my solution again, this time, when static code analysis is run, the Subtract method no longer throws a warning, as shown: