This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Visual Studio
VS 2008+

Visual Studio Code Analysis

Visual Studio Team System 2008 includes code analysis tools in the Development and Team Suite editions. Using the code analysis feature, source code for managed assemblies can be scanned to ensure compliance with standard programming and design rules.

Code Analysis

Visual Studio 2008's code analysis feature provides functionality similar to the external tool, "FxCop". When executed, both tools examine the source code of your project and report where standard programming and design guidelines may have been broken. You can then review your code to determine whether the analysis is correct. Where a problem is encountered, the code analysis tool provides helpful information describing how it can be fixed.

Visual Studio Project Properties Dialog

Code analysis is not enabled by default for new projects. The setting that controls whether analysis is performed is configured using the properties of each Visual Studio project. To view the project properties dialog box, right-click the name of the project in the Solution Explorer and select "Properties" from the menu that appears. Alternatively, select the correct project and choose "Properties" from the "Project" menu.

The project properties dialog box uses a tabbed layout, as there are many options that can be configured. To view the code analysis settings, select the Code Analysis tab. The dialog box will look similar to the image below.

Visual Studio Code Analysis Settings

Enabling Code Analysis

To enable the code analysis tool, ensure the "Enable Code Analysis on Build..." checkbox is ticked. This setting does not immediately perform an analysis of the source code. Instead, it indicates that the code should be reviewed automatically following every compilation.

Generated Code

A portion of the code in a Visual Studio-based project is usually generated by the integrated development environment (IDE). The IDE adds standard items such as the methods required to start a program and the code that sets the properties on opening a Windows form. This code often includes items that do not meet the criteria that the code analysis checks.

If you would prefer not to manually adjust the generated code to achieve compliance with the programming and design rules, you can tick the "Suppress results from generated code" checkbox. This prevents problems in these areas from being displayed when the code is compiled.

Code Analysis Rules

The list in the code analysis tab holds the list of rules that are checked by the code analysis process, grouped in expandable sections. When one of the rules in this list is broken, a warning is displayed when the software is compiled. Each rule in the list also includes a checkbox. If this checkbox is ticked, the warning is upgraded to an error and prevents the compilation from completing successfully until the issue is resolved or explicitly ignored.

Code Analysis Warnings and Errors

The code analysis process is executed when the project is compiled. Depending upon the settings that you have selected in the code analysis section of the project's properties, each violation of a programming or design rule causes a warning or an error to be generated. These are displayed in the Error List window, which can be viewed by selecting the "Error List" option from the "View" menu.

The image below shows an example output containing several warnings. In each case a description of the warning and the name of the project with a potential rule violation is displayed. Where appropriate, the name of the file and the line number where the problem occurs are also visible.

If a warning is right-clicked, a context menu is displayed. This can be used to find more help relating to the problem and its potential resolution. Warning can also be ignored using this menu if it is determined that a problem was incorrectly detected.

Visual Studio Code Analysis Warnings

6 April 2008