VS2013 with ReSharper 8.2 without 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)
  • Contracts with ReSharper code (1.0.0.0)

When I open a method with code contracts, ReSharper gets confused in the contract:

enter image description here

Warns me that chapter can be null, even if the contract requires it not to be. Also:

enter image description here

The contract invariant method is marked as never used . Technically correct, but he should not tell me about it, because the method is used by the code rewriter to obtain information about each invariant. How do I start ReSharper training on code contracts to resolve these two issues?

+7
c # visual-studio-2013 ide resharper code-contracts
source share
2 answers

To get this working for the Portable Class Library, follow these steps:

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

  • Put System.Diagnostics.Contracts.xml there with the following contents:

     <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> 
  • Close all instances of Visual Studio (just to give ReSharper the ability to reload the annotations), reopen Visual Studio and load the necessary solution;

  • There is a possibility that you will need to clear ReSharper caches (ReSharper | Options | Environment | General | Clear Caches);

Also, I still applied for a new ticket to support such a case in the ExternalSnnotations ReSharper extension by default.

+3
source share

Since then I signed a contract with JetBrains by email and suddenly realized. I worked in a Portable Class Library solution. I attach electronic correspondence on my part to this answer and update this answer with subsequent answers (if any) for those who may have a similar problem at the moment.

Thank you for your quick response. Now I 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, refer to any library related to Code Contracts.

However, I seem to have forgotten to mention that I worked in the Portable Class Library Project. Since I did not think about it 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 to you: everything works in a normal project, but code contracts are not recognized in a portable class library.

Therefore, it is easy to conclude that there is no support yet for portable class libraries, or is it just something that has not been tested. I have attached a test project to this email. if you open the Class1.cs file in the project "ClassLibrary1" from VS2013 Update 2 with ReSharper 8.2.1, you should see the same warnings as I do.

I hope you want to add support for these types of projects. I can only imagine that something is not so bad in support but, alas, I do not know your code base and Iโ€™m not trying to Guess. I can only hope that you consider this feature.

+2
source share

All Articles