There seems to be an issue with string.IsNullOrWhiteSpace() when running Code Contracts on .NET 4.6.1. Given the following sample code:
public void StringTests()
{
RequiresNotNullOrWhiteSpace("test");
}
public void RequiresNotNullOrWhiteSpace(string s)
{
Contract.Requires(!string.IsNullOrWhiteSpace(s));
}
The static checker gives the following warning on the Requires() -call:
Warning CC1036 Detected call to method 'System.String.IsNullOrWhiteSpace(System.String)'
without [Pure] in contracts of method '....TestNullOrWhiteSpace(System.String)'.
... and additionally the following warning on the RequiresNotNullOrWhiteSpace("test") -call:
CodeContracts: requires unproven: !string.IsNullOrWhiteSpace(s). Are you making some assumption
on IsNullOrWhiteSpace that the static checker is unaware of?
I compiled exactly the same code on .NET 4.0, 4.5.1 and 4.6 without getting these warnings.
From the user documentation:
Code Contract tools currently assume the following things are pure:
• Any method whose fully qualified name begins with System.Diagnostics.Contracts.Contract,
System.String, System.IO.Path, or System.Type.
Additionally, the IsNullOrWhiteSpace() method is actually marked as [Pure] in .NET 4.6.1
The same problem applies to Invariant(). Given the following code, the same warnings are displayed:
private string testString = "test";
[ContractInvariantMethod]
private void TestInvariant()
{
Contract.Invariant(!string.IsNullOrWhiteSpace(testString));
}