BlackWaspTM
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.

[Obsolete]
public void ObsoleteMethod() { }
25 June 2009