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 2005+

Treating Warnings as Errors in Visual Studio

The compiler used by Visual Studio can produce errors when code cannot be built and warnings when it completes compilation successfully but there are areas of concern. To avoid warnings being missed or ignored, there is an option to treat them as errors.

Why Treat Warnings as Errors?

The compiler can produce warnings that do not cause a build to fail but that may point to problems that require resolution to avoid your programs from behaving unexpectedly. A simple example of C# code that generates a warning is as follows:

while (someBoolean = true)
    someBoolean = false;

At first glance this code seems to provide a while loop that executes whilst the someBoolean variable is set to true. As the variable's value becomes false in the first iteration, you may expect that the loop will execute at most once. However, this is actually an infinite loop because the programmer has accidentally used the assignment operator (=) instead of the equality operator (==). In the first line the someBoolean value is set to true, not compared with it. As such, the condition will evaluate to true and the loop will continue forever.

The compiler is able to spot the potential problem and shows the warning, "Assignment in conditional expression is always constant; did you mean to use == instead of = ?". Unfortunately it can be easy to miss this warning and release the program with a bug. One option to ensure that this type of mistake cannot be made is to treat all warnings within a project as errors. With this option active, any warning is upgraded to an error and causes the build to fail.

Enabling Warnings as Errors

The compiler is not a true part of Visual Studio. It is a separate program that can be used from the command line and can be instructed to treat warnings as errors using a command line switch. However, when you are using Visual Studio, or one of the free versions such as Visual C# Express edition, the integrated development environment (IDE) includes a graphical interface to enable the option.

To find the option, view the properties for a project by choosing the Properties option from the Project menu, or by right-clicking your project in the Project Explorer and choosing Properties from the context-sensitive menu that appears. You must click a project, not the solution, as the option is configured for each project individually. Once you have selected the menu option the project's property page should appear. This is arranged in a tabbed layout. To find the "Treat warnings as errors" option, select the "Build" tab.

Visual Studio Treat Warnings as Errors Settings

Choose the "All" option to treat all warnings as errors. You can test the option by adding the following method to any class within your code and also adding a call to this method. All calls to obsolete methods cause a warning to be generated. With the warning acting as an error you will find that the code will not compile.

public void ObsoleteMethod() { }

Treating Specific Warnings as Errors

In addition to the "None" and "All" options, you can elect to treat only specific warnings as errors. This is useful when you wish to permit most warnings but where selected warnings are not acceptable. To use this option, select the "Specific warnings:" radio button and provide a comma-separated list of warning numbers in the textbox. You may have noticed in the Output window that the warning generated when compiling included the warning number. The message will be similar to the following:

warning CS0612: 'ConsoleApplication1.Program.ObsoleteMethod()' is obsolete

To specify that only this type of warning should be treated as an error, add "612" to the textbox in the project properties.

Allowing Specific Warnings

Some developers prefer to treat all warnings as errors except for one or two specific warning types. Using the IDE, this would require that almost the entire list of warning numbers would need to be included in the "Specific warnings:" textbox for each project. This would be awkward to configure.

It is possible to exclude specific warnings by editing the project file directly using a text editor such as notepad. C# project files can be found in the project's folder and have the extension, "csproj". Before editing the file, switch on the option to treat all warnings as errors and then save your project. Open the project file in Notepad or your preferred editor and you will find a series of "PropertyGroup" XML tags. To ignore specific errors, add a new PropertyGroup tag and include within it another tag named "WarningsNotAsErrors". You can then list the warning numbers that should remain as warnings.


NB: It is advisable to backup the file before editing it.

25 June 2009