Search code examples
c#.netvb.netcode-contracts

C# - code contracts - Detected expression statement evaluated for potential side-effect


I have just started using CodeContracts with C#, and I might need some help. I have a class with a static list of keys, and I have a static method called Remove() that removes a key from that list.

Now what I understand from the contracts post conditions is that as the programmer of the class, I guarantee that the current public method does something specific, in this case : removing the element of the list.

Here's what I've written:

    private static List<Keys> m_usedKeys; // private list of keys

    public static void Remove(Keys _key)
    {
        m_usedKeys.Remove(_key);
        Contract.Ensures(!m_usedKeys.Any(x => x == _key));
    }

What I'm trying to "Ensure" here, is that the Remove() method removes the key for real from the list (Yes this might be a trivial example, but I get the error there.)

when I write the Contract.Ensures() line, VS2010 gives me the following error :

Error   3   Detected expression statement evaluated for potential side-effect in contracts of method 'LibJungleTimer.KeyBind.Remove(System.Windows.Forms.Keys)'. (Did you mean to put the expression into a Requires, Ensures, or Invariant call?)  C:\Users\Joel\Documents\Programmation\JT\JungleTimer\LibJungleTimer\KeyBind.cs  51

I think this error says that the expression m_usedKeys.remove(_key); has a potential side-effect. In fact, it has a side-effect, it removes a key from the list!

If I try to write something like this instead :

Contract.Assert(!m_usedKeys.Any(x => x == _key));

well it compiles fine. My question is what am I doing wrong? Shouldn't CodeContracts be used like that to ensure a post condition?

EDIT: Yes I indeed meant !m_usedKeys.Any(...);


Solution

  • The Code Contracts tool assumes that anything before the last Contract.Ensures / Contract.Requires is part of the contract... so it thinks your call to Remove is part of the contract, not part of the implementation. You have to reverse your code, so that all the contract code is before the implementation.

    public static void Remove(Keys _key)
    {
        Contract.Ensures(m_usedKeys.Any(x => x == _key));        
        m_usedKeys.Remove(_key);
    }
    

    I suspect you want !m_usedKeys.Any(...) by the way... and I'd also be wary of this code to start with, given that you've got no threading protection in place. It wouldn't bother me if this were an instance method, but static methods accessing static (i.e. global) state should generally be thread-safe.