4

I'm using Code Contracts to declare that a property returns a non-null, non-empty sequence of strings like so:

public IEnumerable<string> Filenames
{
    get
    {
        Contract.Ensures(Contract.Result<IEnumerable<string>>() != null);

        // Next line gives Resharper Warning
        // "Possible null assignment to entity marked with 'not null' attribute":

        Contract.Ensures(Contract.Result<IEnumerable<string>>().Any());

        return new []{"TEST"}; // Dummy data for demo purposes.
    }
}

I'm getting a warning from Resharper as described in the code comment above.

This is similar to the question here:, but I have tried applying the fix in the answer to that question and it doesn't fix this particular issue.

Does anyone know how to fix this (other than using Resharper comments to suppress the warning)?

I'm using Resharper 7.1.2 C# Edition, build 7.1.2000.1478

(I've checked on several machines and it happens on all of them. Vanilla install of R# - we haven't modified any of its XML files other than me trying to apply the fix from the answer that I linked above.)

Further information:

I'm trying this with Visual Studio 2012 with update 2, with .Net 4.0 and .Net 4.5.

Also, you need to add the conditional compilation symbol "CONTRACTS_FULL" to the project's Build settings (in the "conditional compilation symbol" textbox).

Community
  • 1
  • 1
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • What R# version are you using? I have no warning of such kind, using **ReSharper 7.1** and StyleCop plugin – Ilya Ivanov May 20 '13 at 15:19
  • Good point. It's 7.1. I'll update my question. I'll also check my co-workers machines. – Matthew Watson May 20 '13 at 15:21
  • Please post a piece of XML that you've tried to add to ReSharper's annotations. – Dmitry Osinovskiy May 20 '13 at 20:06
  • I too have this problem as well. Using .Net 4.5 and VS2012.2 Professional and ReSharper 7.1.3 full version - it seams that it doesn't detect the `Contracts.Ensures` at all (but `Contracts.Requires` is respected). Btw, I checked ExternalAnnotations in the latest ReSharper v8 EAP, and they are still the same... – Nikola Bogdanović Jun 23 '13 at 03:08
  • I updated my answer, and it was confirmed by a ReSharper expert in another question. – Nikola Bogdanović Jun 27 '13 at 18:54

1 Answers1

1

The problem is that although most of the Code Contracts are covered in ReSharper ExternalAnnotations, Ensures is not one of them (not even in any of the custom xmls floating around).

I just double checked ExternalAnnotations from the latest ReSharper v8 EAP, and they are still exactly the same as in the v7.1.3 - so absolutely nothing has changed so far.

I'll make a new question, asking if anyone knows how to implement it.

UPDATE: Code Contracts Ensures for ReSharper ExternalAnnotations

FINAL: it's not doable at 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...

Community
  • 1
  • 1
Nikola Bogdanović
  • 3,206
  • 1
  • 20
  • 28