0

The following is an example I have created in order to get the static analysis tool to fail:

using System.Diagnostics.Contracts;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            object x = null;
            Contract.Assert(x != null);
        }
    }
}

Runtime analysis throws the correct exception but static analysis builds without even a warning. I have every checkbox selected in the project settings, and the warning level on "hi". I am running version 1.4.51019.0.

What do I need to do to get compilation to fail?

Alex
  • 7,639
  • 3
  • 45
  • 58

1 Answers1

3

"Every checkbox selected" is the problem here. You need to uncheck "Infer Ensures".

What happens here is the following:
The code you created can never be correct, you initialize x with null and directly afterwards you assert that x should be not null.
Because of this contradiction, Code Contracts inferes Contract.Ensures(false), which will always fail.
I assume that the infered Contract.Ensures(false) at the beginning of the method will make the static analyser skip the rest of the method.

Infering Contract.Ensures(false) seems like a mistake of Code Contracts to me, but the only way to disable it is to disable the inference of ensures completely.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443