ReSharper doesn't recognize custom IsNullOrEmpty extension method to a generic list.

Here is the extension:

 

    <Extension>
Public Function IsNullOrEmpty(Of T)(list As List(Of T)) As Boolean
If list Is Nothing OrElse list.Count = 0 Then
Return True
End If

Return False
End Function

 

Here is the code that calls the extension:

if (myList.IsNullOrEmpty())
{
throw new InvalidDataException("Some meaningful info here");
}

if (myList.Count > 1)
{
// The line above gets "Possible 'System.NullReferenceException'".
}



4 comments
Comment actions Permalink

You need to annotate the method:
https://www.jetbrains.com/help/resharper/Reference__Code_Annotation_Attributes.html#ContractAnnotationAttribute

<Extension, ContractAnnotation("null=>true")>
Public Function IsNullOrEmpty(Of T)(list As List(Of T)) As Boolean
0
Comment actions Permalink

Thanks, Richard. This is a shared code base, and not everyone is on ReSharper. It would be a hard sell to add the NuGet package, put #using JetBrains.Annotations, and explain why this attribute is needed for everyone. I would expect ReSharper to provide a more viable solution.

0
Comment actions Permalink

The other option is to use "external annotations", where the annotations are defined in an XML file:
https://www.jetbrains.com/help/resharper/Code_Analysis__External_Annotations.html

That way, your code doesn't contain any JetBrains annotations, or any reference to the NuGet package. However, it will be somewhat more complicated to set up.

0
Comment actions Permalink

Of course, if you're using the latest version of R#, and if your project supports it, you could enable "nullable annotations", which would work for anyone with an up-to-date version of Visual Studio, whether or not they have R# installed.

Nullable reference types | Microsoft Docs
C# Reserved attributes: Nullable static analysis | Microsoft Docs
What's New for C# Nullable Reference Types in ReSharper and Rider 2021.2 EAP? | The .NET Tools Blog

However, I'm not sure what support VB.NET offers for nullable reference types.

0

Please sign in to leave a comment.