ReSharper Possible Null Exception when null is already checked

This is ReSharper 7 with Visual Studio 2012. With an example 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 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 does not have any associated code or attribute that R # would String.IsNullOrWhiteSpace so that the validation is verified. Is it hardcoded in R #?

I looked at Code Contracts, but I'm not sure if this helps in my case.

Do you have a workaround for checking ReSharper that the validation condition has already been verified by my extension method?

+7
source share
3 answers

Available in Resharper 7 and later

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

Your project will not know what ContractAnnotation . You need to add it to your project. The preferred method is nuget:

PM> Install-Package JetBrains.Annotations

Alternatively, you can directly embed the source in your project:

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

Then paste this into a new file, for example Annotations.cs. The definition of ContractAnnotation is in this file. For an official article on ContractAnnotation, see here .


Previous answer (for versions without R # 7)

Is it hardcoded in R #?

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

Example

Note: external annotations seem to work only in reference libraries; if your link is related to the project, external annotations are not selected; it's less than ideal

Suppose you have an extension method in a class called TempExtensions , which itself is in an assembly called ClassLibrary1

You need to add a new file in this place.

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

The xml content 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> 
+14
source

As far as I know, this is a problem of a solution. He cannot learn methods and verify that a null check will be performed on the method. If you want to avoid this message, I think you need to do a null check. But I would rather ignore the resharper message in this situation.

0
source

The only workaround that I see is to add a functional parameter to the extension method.

 public static class StringExtensions { public static bool IsNotNullAndNotWhiteSpaceAnd(this string s, Func<string, bool> func) { if (string.IsNullOrWhiteSpace(s)) { return false; } return func(s); } } 

An example of using this extension method:

 string extension = null; //Test for null //string extension = "123"; //Test for Length==3 if (extension.IsNotNullAndNotWhiteSpaceAnd(_ => _.Length == 3)) { Console.Out.WriteLine("Length == 3"); } else { Console.Out.WriteLine("Null or WhiteSpace or Length != 3"); } 
0
source

All Articles