I tried your example and tried to weld it to the most elementary example.
It seems that there may be several problems, but here is an example that, I think, can illustrate one serious problem:
public static void TestMethod() { double d = MethodReturningDouble(); Contract.Assert(d >= 0.0); Contract.Assert(d <= 4.0); } public static double MethodReturningDouble() {
Without the specification of the code contract for the called methods, the static controller / analyzer does not seem to be able to work in any other way. MethodReturningDouble () returns a constant, and the static controller cannot decide that the statement will always pass.
In conclusion, it turns out that the static analyzer is ONLY for code contract specifications, and not for general analysis.
You can add assumptions about the methods you call (which do not have specific contracts):
For instance:
public static void TestMethodUsingRandom() { double d = new Random().NextDouble(); Contract.Assume(d <= 1.0); Contract.Assert(d >= 0.0); Contract.Assert(d <= 4.0); }
This is a legitimate thing if you know for sure that a particular method behaves in a certain way.
source share