Analyzing Visual Basic and C# code quality in Windows Store apps with Visual Studio code analysis
The code analysis tool in Microsoft Visual Studio Express 2012 for Windows 8 examines your code for a set of common defects and violations of good programming practice. Code analysis warnings differ from compiler errors and warnings because the code analysis tool searches for specific code patterns that are valid but could still create issues for you or other people who use your code. Code analysis can also find defects in your code that are difficult to discover through testing. Running the code analysis tool at regular intervals during your development process can enhance the quality of your completed app.
Note
In Visual Studio Ultimate 2012, Visual Studio Premium 2012, and Visual Studio Professional 2012, you can use the full functionality of code analysis. See Analyzing Application Quality by Using Code Analysis Tools in the MSDN Library.
In this topic
You can learn about:
Analyzing and resolving code analysis warnings
Suppressing code analysis warnings
Searching and filtering code analysis results
Visual Basic and C# code analysis warnings
Running code analysis
To run code analysis on your Visual Studio solution:
- On the Build menu, choose Run Code Analysis on Solution.
To automatically run code analysis each time you build a project:
Right-click the project name in Solution Explorer and then choose Properties.
In the project property page, choose Code Analysis and then choose Enable Code Analysis on Build (defines CODEANALYSIS constant).
The solution is compiled and code analysis runs. Results appear in the Code Analysis window.
Analyzing and resolving code analysis warnings
To analyze a specific warning, click the title of the warning in the Code Analysis window. The warning expands to display detailed information about the issue.
When you expand a warning, the line of code that caused the warning is highlighted in the Visual Studio code editor.
After you understand the issue, you can resolve it in your code. Then rerun code analysis to make sure that the warning no longer appears in the Code Analysis window, and that your fix has not raised new warnings.
Tip
You can rerun code analysis from the Code Analysis window. Click the Analyze button and choose the scope of the analysis. You can rerun analysis on the entire solution or on a selected project.
Suppressing code analysis warnings
There are times when you might decide not to fix a code analysis warning. You might decide that resolving the warning requires too much recoding in relation to the probability that the issue will arise in any real-world implementation of your code. Or you might believe that the analysis that is used in the warning is inappropriate for the particular context. You can suppress individual warnings so that they no longer appear in the Code Analysis window.
To suppress a warning:
If the detailed information is not displayed, click the title of the warning to expand it.
Choose the Actions link at the bottom of the warning.
Point to Suppress Message and then choose either In Source or In Suppression File.
In Source inserts a SuppressMessage attribute in the source file above the method that generated the warning. This makes the suppression more discoverable.
In Suppression File adds a SuppressMessage attribute to the GlobalSuppressions.cs file of the project. This can make the management of suppressions easier. Note that the SuppressMessage attribute added to GlobalSuppression.cs also targets the method that generated the warning. It does not suppress the warning globally.
Your decision whether to suppress the warning in the source file or in the suppression file depends on your coding style and needs.
Searching and filtering code analysis results
You can search long lists of warning messages and you can filter warnings in multi-project solutions.
In Visual Studio Express 2012 for Windows 8, all code analysis warnings have the severity level of Warning.
Visual Basic and C# code analysis warnings
Code analysis raises the following warnings:
CA1001: Types that own disposable fields should be disposable
CA1821: Remove empty finalizers
CA2213: Disposable fields should be disposed
CA2229: Implement serialization constructors
CA2231: Overload operator equals on overriding ValueType.Equals