views:

486

answers:

3

According to this feature matrix, Visual Studio 2010 Premium (RC) includes "static code analysis".

However, adding calls to Contract.Requires at the start of my methods doesn't seem to have any effect. I don't get runtime exceptions if the contract is violated. I don't get compiler warnings if a contract cannot be proven. I also can't find a "Code Contracts" tab in the project settings.

Am I missing something obvious or is this feature only available in the Ultimate edition?

+3  A: 

There does indeed seem to be some confusion about whether or not Code Contracts is included in the framework that comes with beta 2 of VS2010, or even in VS2010 itself. The Code Contracts forum (and expecially this post) has several mentions of this with no answers as of yet.


I don't think that Code Contracts falls under the heading of static code analysis. I think static code analysis refers to other code analysis tools like FxCop.

I have tried using Contract.Requires in VS2010 beta 2 ultimate edition, but I get the same behaviour as you. If you look in the project properties under the Code Analysis tab, you can enable code analysis, but this doesn't relate to Code Contracts.

According to the comment by Melitta (from the Base Class Libraries Team) on the connect article Make data contract static checking available in Professional Edition:

...but the restriction on the static analysis tool stands for this upcoming release. It keeps the code analysis story consistent, in that the static analysis tools require VSTS.

This suggests that you (will) have to have the team system version to get the compile-time version of Code Contracts.

adrianbanks
Is there even a "team edition" anymore in vs2010? The feature matrix doesn't show it.
Wim Coenen
I think the Team System versions turn into the Ultimate version of 2010 (http://www.microsoft.com/visualstudio/en-gb/products/msdn/default.mspx#roadmap)
adrianbanks
+1  A: 

You need to turn on the code contracts runtime checking in the project properties. If that settings pane doesn't exist then you need to install the Code Contracts download from Microsoft (despite the fact that the Contract.Requires exists in the framework).

Only the runtime checking is available in the Professional editions.

Matt Breckon
+2  A: 

To get static analysis in Beta 2 you have to download and install the Code Contracts bits (1.2.21023.14 or higher) from MSDN DevLabs. Then under the project properties, there will now be a Code Contracts tab... from here you can turn on static analysis...

The behaviour you're seeing is because the Code Contracts API is in mscorlib assembly, which allows asserts to work, but nothing else.

I'm assuming this won't be necessary by RTM.

Paul Killick
Unfortunately, this looks like it's still not working in RTM. MS botched this one. You can put in Contracts.Requires, but the magic doesn't really happen until you download the CodeContracts tools.
Judah Himango