Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code Contracts trying to get build errors instead of warnings

I'm trying to get VS2010 Ultimate with Code Contracts to generate Errors instead of Warnings.

I have this simple test program:

using System.Diagnostics.Contracts;

namespace MyError
{
  public class Program 
  {
     static void Main(string[] args)
     {
         Program prog = new Program();
         prog.Log(null);
     }

     public void Log(string msg)
     {
         Contract.Requires(msg != null);
     }
  }
}

It correctly determines there is a violation of the contract:

C:\...\Program.cs(10,13): warning : CodeContracts: requires is false: msg != null

In my csproj file there is this property field for Debug:

TreatWarningsAsErrors>true

Is there something else I have to set in the project settings to turn these into errors?

like image 262
larry Avatar asked Dec 20 '10 15:12

larry


People also ask

How do you compile without warnings being treated as errors?

You can make all warnings being treated as such using -Wno-error. You can make specific warnings being treated as such by using -Wno-error=<warning name> where <warning name> is the name of the warning you don't want treated as an error. If you want to entirely disable all warnings, use -w (not recommended).

Should you treat warnings as errors?

Yes, even once in a while you will encounter the occasional warning you'd be better off leaving as a warning or even disabling completely. Those should be the exception to the rule, though. Here's some practical advise: at the start of a new project, start treating all warnings as errors, by default.

How do I fix Visual Studio build errors?

To get help on a particular diagnostic message in Visual Studio, select it in the Output window and press the F1 key. Visual Studio opens the documentation page for that error, if one exists. You can also use the search tool at the top of the page to find articles about specific errors or warnings.


1 Answers

It looks like at this point Microsoft has elected not to make this possible, but they are considering it for the future: http://connect.microsoft.com/VisualStudio/feedback/details/646880/code-contracts-dont-listen-to-treat-warnings-as-errors-setting

like image 154
Curtis P Avatar answered Dec 11 '22 07:12

Curtis P