Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2013 with ReSharper 8.2 not recognizing Code Contracts?

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?

like image 815
Deathspike Avatar asked Jul 19 '14 09:07

Deathspike


1 Answers

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.

like image 117
Alexander Kurakin Avatar answered Sep 29 '22 18:09

Alexander Kurakin