7

I have the following software:

  • Visual Studio 2013 Update 2
  • Code Contracts (1.6.60617.15)
  • ReSharper C# edition (8.2.0.2160)
  • ReSharper Code Contracts (1.0.0.0)

When I open a method with code contracts, ReSharper is confused about the contract:

enter image description here

It is warning me that chapter can be null, even though the contract requires it not to be. Also:

enter image description here

The contract invariant method is flagged as never used. Technically correct, but it should not tell me that because the method is used by the code contract rewriter to get information about each invariant. How do I go about teaching ReSharper about code contracts to correct these two issues?

Deathspike
  • 8,582
  • 6
  • 44
  • 82
  • 1
    Paste your code as a text, not as an image. – Soner Gönül Jul 19 '14 at 10:08
  • Also feedback to Microsoft. So, Microsoft will correct that issue in next update. – Shell Jul 19 '14 at 10:10
  • 2
    To demonstrate the issue, I needed the appearance of the issue as someone familiar with ReSharper environment would see it. What good would pasting the code do, @SonerGönül? – Deathspike Jul 19 '14 at 10:16
  • 3
    This is not a Code Contracts issue. It is a ReSharper issue, yet this scenario has been claimed to be supported. If anything, it should be reported to JetBrains, if this is indeed a bug @Shell. I'm here with a question to figure that out. – Deathspike Jul 19 '14 at 10:16
  • Just to make sure: Is Code Contracts Runtime Checking enabled in the project's properties and have you added CONTRACTS_FULL to the compilation symbols (Build section of the project properties)? – Keith Jul 21 '14 at 20:28
  • Yes, Code Contracts Runtime Checking is enabled, but I did not have the compilation symbol set to CONTRACTS_FULL. Adding it, however, does not seem to make a difference @Keith – Deathspike Jul 22 '14 at 16:47

2 Answers2

3

To get this working for Portable Class Library, please do the following:

  1. Create a new folder ExternalAnnotations in C:\Program Files (x86)\JetBrains\ReSharper\v8.2\Bin\ directory;

  2. Put System.Diagnostics.Contracts.xml there with the following content:

    <assembly name="System.Diagnostics.Contracts">
    <member name="M:System.Diagnostics.Contracts.Contract.Assume(System.Boolean)">
        <attribute ctor="M:JetBrains.Annotations.ContractAnnotationAttribute.#ctor(System.String)">
            <argument>condition:false=&gt;halt</argument>
        </attribute>
    </member>
    <member name="M:System.Diagnostics.Contracts.Contract.Assume(System.Boolean,System.String)">
        <attribute ctor="M:JetBrains.Annotations.ContractAnnotationAttribute.#ctor(System.String)">
            <argument>condition:false=&gt;halt</argument>
        </attribute>
    </member>
    <member name="M:System.Diagnostics.Contracts.Contract.Assert(System.Boolean)">
        <attribute ctor="M:JetBrains.Annotations.AssertionMethodAttribute.#ctor"/>
        <attribute ctor="M:JetBrains.Annotations.ContractAnnotationAttribute.#ctor(System.String)">
            <argument>condition:false=&gt;halt</argument>
        </attribute>
    </member>
    <member name="M:System.Diagnostics.Contracts.Contract.Assert(System.Boolean,System.String)">
        <attribute ctor="M:JetBrains.Annotations.AssertionMethodAttribute.#ctor"/>
        <attribute ctor="M:JetBrains.Annotations.ContractAnnotationAttribute.#ctor(System.String)">
            <argument>condition:false=&gt;halt</argument>
        </attribute>
    </member>
    <member name="M:System.Diagnostics.Contracts.Contract.Requires(System.Boolean)">
        <attribute ctor="M:JetBrains.Annotations.ContractAnnotationAttribute.#ctor(System.String)">
            <argument>condition:false=&gt;halt</argument>
        </attribute>
    </member>
    <member name="M:System.Diagnostics.Contracts.Contract.Requires(System.Boolean,System.String)">
        <attribute ctor="M:JetBrains.Annotations.ContractAnnotationAttribute.#ctor(System.String)">
            <argument>condition:false=&gt;halt</argument>
        </attribute>
    </member>
    <member name="M:System.Diagnostics.Contracts.Contract.Requires``1(System.Boolean)">
        <attribute ctor="M:JetBrains.Annotations.ContractAnnotationAttribute.#ctor(System.String)">
            <argument>condition:false=&gt;halt</argument>
        </attribute>
    </member>
    <member name="M:System.Diagnostics.Contracts.Contract.Requires``1(System.Boolean,System.String)">
        <attribute ctor="M:JetBrains.Annotations.ContractAnnotationAttribute.#ctor(System.String)">
            <argument>condition:false=&gt;halt</argument>
        </attribute>
    </member>
    <member name="M:System.Diagnostics.Contracts.Contract.Invariant(System.Boolean)">
        <attribute ctor="M:JetBrains.Annotations.AssertionMethodAttribute.#ctor"/>
        <attribute ctor="M:JetBrains.Annotations.ContractAnnotationAttribute.#ctor(System.String)">
            <argument>condition:false=&gt;halt</argument>
        </attribute>
    </member>
    <member name="M:System.Diagnostics.Contracts.Contract.Invariant(System.Boolean,System.String)">
        <attribute ctor="M:JetBrains.Annotations.AssertionMethodAttribute.#ctor"/>
        <attribute ctor="M:JetBrains.Annotations.ContractAnnotationAttribute.#ctor(System.String)">
            <argument>condition:false=&gt;halt</argument>
        </attribute>
    </member>
    <member name="M:System.Diagnostics.Contracts.Contract.ReportFailure(System.Diagnostics.Contracts.ContractFailureKind,System.String,System.String,System.Exception)">
        <attribute ctor="M:JetBrains.Annotations.AssertionMethodAttribute.#ctor"/>
        <attribute ctor="M:JetBrains.Annotations.ContractAnnotationAttribute.#ctor(System.String)">
            <argument>=&gt;halt</argument>
        </attribute>
    </member>
    <member name="T:System.Diagnostics.Contracts.ContractInvariantMethodAttribute">
        <attribute ctor="M:JetBrains.Annotations.MeansImplicitUseAttribute.#ctor" />
    </member>
    <member name="T:System.Diagnostics.Contracts.ContractClassForAttribute">
        <attribute ctor="M:JetBrains.Annotations.MeansImplicitUseAttribute.#ctor" />
    </member>
    </assembly>
    
  3. Close all Visual Studio instances (just to give ReSharper a chance to reload annotations), reopen Visual Studio and load needed solution;

  4. There is a chance you will need to clean ReSharper caches (ReSharper | Options | Environment | General | Clear Caches);

Also I filed a new ticket about supporting such case in ReSharper ExternalAnnotations extension by default.

Alexander Kurakin
  • 13,373
  • 2
  • 33
  • 29
2

I have since contracted JetBrains via e-mail support, and came to a sudden realization. I was working in a Portable Class Library solution. I will attach the e-mail correspondence from my side to this answer and will update this answer with follow-up responses (If any) for those who might have a similar problem at this moment.

Thank you for your quick reply. I have now installed 8.2.1000.4556 and updated the ExternalAnnotations extension to 8.2.1001. The aforementioned "ReSharper Code Contracts 1.0.0" plugin is actually called "ReSharper.ContractExtensions 0.7.51" on the plugin page. I am using .NET 4.5, and I did not define compilation symbols nor did I reference any Code Contracts related library.

However, I seem to have neglected to mention that I was working in a Portable Class Library project. Since I did not think about this possibility at all, I created a test project to test my ReSharper installation with. I used a regular project and a Portable Class Library project. The result may not be surprising for you: everything works as intended in the regular project, but Code Contracts are not recognized in the Portable Class Library.

It is therefore easy to conclude that there is no support yet for Portable Class Libraries, or this is merely something that has not been tested. I have attached the test project to this e-mail. If you open the "Class1.cs" file in the "ClassLibrary1" project from VS2013 Update 2 with ReSharper 8.2.1, you should see the same warnings as I do.

I hope you will consider adding support for these project types. I can only imagine it is something little going wrong in a support detection, but alas, I do not know your code base nor should I try to guess. I can only hope you will consider this feature.

Deathspike
  • 8,582
  • 6
  • 44
  • 82