Amit Raz
Amit Raz

Reputation: 5536

Code Contracts warning not showing

I have this simple code:

public ArrayStack(int capacity)
    {
        Contract.Requires(capacity >= 0);
        Contract.Ensures(_items != null);
        Contract.Ensures(_items.Length == capacity);
        _items = new T[capacity];
        _top = -1;
    }

I expected that once I type the followig I will get a compile time warning, but I only get a runtime exception from the contract.

static void Main(string[] args)
    {
        int i = -1;
        ArrayStack<string> stack = new ArrayStack<string>(i);

    }

any ideas?

EDITED: picture of my code contract settings enter image description here

Upvotes: 1

Views: 462

Answers (2)

Amit Raz
Amit Raz

Reputation: 5536

Figured it out.

It seems the compiler is too smart and sees no one is using the stack after the last line so he does not check it.

once I add stack.push(...) it gives me the error...

cant have the coputer to be too smart...

Upvotes: 2

Patryk Ćwiek
Patryk Ćwiek

Reputation: 14328

If you want to have the squiggly lines, then you have to check 'Show squigglies':

enter image description here

The re-build the project, wait for the static analysis to finish and you will have both warnings in 'Output' window (if you're not running the 'Ultimate' version of VS, these can be easy to miss) and the lines under suspicious code.

[Edit] By the way, I always run with the 'Standard Contract Requires' Assembly Mode.

Then after the example build: enter image description here

and in the IDE:

enter image description here

Upvotes: 1

Related Questions