Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with Code Contracts warning CC1036 when using string.IsNullOrWhiteSpace?

I have the following code contract:

public void F(string x) {     Contract.Requires(!string.IsNullOrWhiteSpace(x));      throw new NotImplementedException(); } 

When compiling, I get the following warning:

warning CC1036: Detected call to method 'System.String.IsNullOrWhiteSpace(System.String)' without [Pure] in contracts of method [...]

How to deal with it?

What's odd, is that I'm also using string.IsNullOrEmpty, which isn't marked as [Pure] as well, in other contracts and the rewriter does not have a problem with that.

My Contract Rewriter's Version is 1.9.10714.2.

This is the relevant part from the implementation of String class I'm using (retrieved from metadata):

#region Assembly mscorlib.dll, v4.0.0.0 // C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\mscorlib.dll #endregion  using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Reflection; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; using System.Security; using System.Text;  namespace System {     // Summary:     //     Represents text as a series of Unicode characters.To browse the .NET Framework     //     source code for this type, see the Reference Source.     [Serializable]     [ComVisible(true)]     public sealed class String : IComparable, ICloneable, IConvertible, IEnumerable, IComparable<string>, IEnumerable<char>, IEquatable<string>     {      // [...]          //         // Summary:         // [...]         public static bool IsNullOrEmpty(string value);         //         // Summary:         // [...]         public static bool IsNullOrWhiteSpace(string value); 

Why is the [Pure] attribute missing?

like image 881
BartoszKP Avatar asked Jan 05 '16 13:01

BartoszKP


1 Answers

Here we have two points:

1. Why is the [Pure] attribute missing in string class for IsNullorWhiteSpace function?

2. How to resolve the CC1030 warning issue?

I will try to discuss both.

1. Why is the [Pure] attribute missing? It's not missing, metadata does not seem to be showing this.

This may not be marked as Pure in previous version of .NET FX as, they were saying:

Yes, we need to make our checker sensitive to the disable pragma...

Sigh.

We currently don't have that implemented, but I've added it to our work list.

Refer to the 5 year old discussion here.

But this has been marked as Pure in latest FX (4.6.1), Refer to .NET Framework 4.6.1, the new string class code.

[Pure] public static bool IsNullOrWhiteSpace(String value) {     if (value == null) return true;      for(int i = 0; i < value.Length; i++) {         if(!Char.IsWhiteSpace(value[i])) return false;     }      return true; } 

Then Why CC1036?

This warning "CC1036" is from CodeContracts, developers have open this issue yesterday only (refer here).

Now why metadata is not spitting up Pure attributes, this is a different question, like for Equals method, Pure is added but only SecuritySafeCritical is displayed in metadata code.

[SecuritySafeCritical] public static bool Equals(String a, String b, StringComparison comparisonType); 

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

How to resolve?

As others are also suggesting, create another method, mark it as Pure and call this in your contract condition.

like image 198
Anil Avatar answered Sep 24 '22 08:09

Anil