Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 922
Adam Avatar asked Aug 19 '12 12:08

Adam


People also ask

How do I fix NullReferenceException object reference not set to an instance of an object?

The best way to avoid the "NullReferenceException: Object reference not set to an instance of an object” error is to check the values of all variables while coding. You can also use a simple if-else statement to check for null values, such as if (numbers!= null) to avoid this exception.

How to remove nullable warning in c#?

In the example above, the warning is because the Container , c , may have a null value for the States property. Assigning new states to a collection that might be null causes the warning. To remove these warnings, you need to add code to change that variable's null-state to not-null before dereferencing it.

What is a null check?

A null indicates that a variable doesn't point to any object and holds no value. You can use a basic 'if' statement to check a null in a piece of code. Null is commonly used to denote or verify the non-existence of something.

IS null check C#?

In C#, IsNullOrEmpty() is a string method. It is used to check whether the specified string is null or an Empty string. A string will be null if it has not been assigned a value. A string will be empty if it is assigned “” or String.


1 Answers

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>
like image 122
wal Avatar answered Nov 15 '22 19:11

wal