Skip to content
This repository was archived by the owner on Jul 15, 2023. It is now read-only.
This repository was archived by the owner on Jul 15, 2023. It is now read-only.

string.IsNullOrWhiteSpace() not considered Pure by Requires() or Invariant() when running on .NET 4.6.1 #339

@Desarc

Description

@Desarc

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));
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions