larry
larry

Reputation: 71

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?

Upvotes: 7

Views: 551

Answers (2)

John Nicholas
John Nicholas

Reputation: 4836

The problem is that the code contracts use a rewriter. they show as warnings because they are only calculated after the build completes.

Well i don't really know how it works, but unless you built code contracts into the compiler i do not see how they could be anything but warnings / messages.

Upvotes: 1

Curtis P
Curtis P

Reputation: 351

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

Upvotes: 2

Related Questions