Search code examples
c#resharperextension-methodsnullreferenceexceptioncode-contracts

ReSharper Possible Null Exception when null is already checked


This is ReSharper 7 with Visual Studio 2012. With the sample below

// This code works fine and as expected and ReShrper is happy with it
if (!string.IsNullOrWhiteSpace(extension) && extension.Length == 3)
{
    // do something
}

// ReSharper highlights "extension" in extension.Length with "Possible 'System.NullReferenceException'"
if (!extension.IsNullOrWhiteSpace() && extension.Length == 3)
{
    // do something
}

And, I have created the following extension method:

public static class StringExtensions
{
    public static bool IsNullOrWhiteSpace(this string s)
    {
        return string.IsNullOrWhiteSpace(s);
    }
}

I looked at the reflected code of String.IsNullOrWhiteSpace and it doesn't have any related code or attribute that would highlight to R# that the check is verified. Is this hardcoded in R#?

I looked at Code Contracts, but I am not sure it would help in my case.

Do you have a workaround for proving to ReSharper that the check condition is already verified by my extension method?


Solution

  • Available in Resharper 7 and above

    [ContractAnnotation("null=>true")]
    public static bool IsNullOrWhiteSpace(this string s)
    

    Your project isn't going to know what ContractAnnotation is. You need to add it to your project. The preferred method is via nuget:

    PM> Install-Package JetBrains.Annotations

    Alternatively you can directly embed the source into your project:

    Resharper -> Options -> Code Annotations -> Copy default implementation to clipboard

    Then paste that into a new file, eg Annotations.cs. The ContractAnnotation definition lives in that file. For an official article on ContractAnnotation see here


    Previous answer (for non R#7 versions)

    Is this hardcoded in R#?

    No, Resharper uses External Annotations to provide this functionality. This article should answer all your questions, including a solution to provide your own external annotation for your IsNullOrWhiteSpace method.

    Example

    note: external annotations appear to only work on referenced libraries; if your reference is from a project the external annotations are not picked up; this is less than ideal

    Suppose you have your extension method in a class called TempExtensions which itself resides in an assembly named ClassLibrary1

    You need to add a new file at this location

    C:\Program Files (x86)\JetBrains\ReSharper\v7.0\Bin\ExternalAnnotations.NETFramework.ExternalAnnotations\ClassLibrary1\ClassLibrary1.xml

    The contents of the xml should contain:

    <assembly name="ClassLibrary1">
      <member name="M:ClassLibrary1.TempExtensions.IsNullOrWhiteSpace(System.String)">
        <attribute ctor="M:JetBrains.Annotations.ContractAnnotationAttribute.#ctor(System.String,System.Boolean)">
            <argument>null=&gt;true</argument>
            <argument>true</argument>
        </attribute>
      </member>
    </assembly>