3

Does anyone know how to add Code Contracts Ensures in ReSharper ExternalAnnotations? It's not there in the last v7.1.3 nor in the latest v8 EAP, and neither in any of the custom xmls floating around.

Specifically it should detect if a method does not return a null: Contract.Ensures(Contract.Result<T>() != null);

Nikola Bogdanović
  • 3,206
  • 1
  • 20
  • 28
  • What you are describing is not something that can be described via an external annotation. To have R# detect this, you would need to write your own plugin with an analyzer used specifically to detect such statements. – Dmitri Nesteruk Jun 23 '13 at 16:45
  • @DmitriNesteruk: ReSharper v7 came with its own Contracts (http://blogs.jetbrains.com/dotnet/2012/08/contract-annotations-in-resharper-7/) that extend the Annotations - if your opinion is based on that as well, than add an answer and I'll accept it... – Nikola Bogdanović Jun 23 '13 at 17:49

1 Answers1

4

If you're attempting to simply appease the analysis engine, the simplest thing to use is [NotNull] in front of the method declaration. The Contract Annotations to which you posted a link above is a more powerful mechanism for defining relationships between input parameters and the return value, e.g., [ContactAnnotation("null => null")].

However, explicitly analyzing for a Contract.Ensures statement is an entirely different proposition, as no automatic analysis can be defined for this statement via [ContractAnnotation] or any other ReSharper annotation attribute.

Dmitri Nesteruk
  • 23,067
  • 22
  • 97
  • 166
  • yes, I am aware of all that, and yes, it's not doable after all - simply as the attribute would somehow need to be implied for the method containing the code contract, and not what's inside the code contract itself... oh well, thanks anyway - please correct your answer (`Ensures` instead of `Requires`) so as not to confuse people, and I'll accept it. – Nikola Bogdanović Jun 25 '13 at 00:14