Why do I get the following warning for this trivial code sample as soon as the Warning Level is on the 2nd level or higher?
public int Foo(int a)
{
if (a >= 0) throw new ArgumentException("a should be negative", "a");
Contract.EndContractBlock();
return a;
}
CodeContracts: Suggested requires: This precondition is redundant: Consider removing it. Are you comparing a struct value to null?
Clearly an integer can be negative so the precondition is hardly redundant, so why do I get this warning?
Edit: Here is what ILSpy shows for the created function when looking at the exe:
public int Foo(int a)
{
if (a >= 0)
{
ContractHelper.RaiseContractFailedEvent(ContractFailureKind.Precondition, null, "a < 0", null);
throw new ArgumentException("a should be negative", "a");
}
return a;
}
I know this doesn't directly answer your question, but it appears you're using a legacy mode for Code Contracts.
This document describes the recommended assembly mode based on usage requirements:
http://research.microsoft.com/en-us/projects/contracts/userdoc.pdf
From Pages 20, 21...
Another snippet from the document:
So using "Standard Contract Requires" assembly mode you could do either of the following:
Neither of these generate any warnings for me.
I hope this helps anyway.
Cheers peteski