Static Code Analysis - Part 1

The static code analysis within Visual Studio is a great way of automating initial code review.  In order to turn it on do the following.

1.  Go to Properties within the particular project.

Code analysis can only be enabled at the project level, so the first step is to go to your properties section of each project you want enabled for code analysis.

2.  Go to the Code Analysis tab.

3.  At the top, check the box that "Enable Code Analysis on Build..."

4.  On the right-hand side of the screen, check the box for those rules that you want treated as an error.  (By default it is treated as a warning and will not stop the build process.)


Sometimes the rules may not make sense for projects where a lot of code is auto-generated.  In this case, you could leave that rule as a warning in the project settings. 

In other cases, you may want to suppress the errors for particular members of your types or for whole namespaces.  This can be done by right-clicking on the rule violation in the Error List that appears when compiling your project and selecting a Suppress Message option.


As seen above, this can either be set within your source or in a project suppression file.  You can learn more about this here.