How do I customize inspections to recognize a custom method for checking for nulls?

We have several library functions and extension methods like .IsEmpty() or .IsEmptyOrNull(). When we use them, Rider/Resharper always flags the code underneath for possible null reference exceptions, like for the code below will flag myObject.DoSomething() as a possible null reference exception.

How do I configure the inspections to recognize my methods as null reference checks?

if (!myObject.IsEmptyOrNull())
{
myObject.DoSomething();
}
3 comments
Comment actions Permalink

Hello Chris Palmer, thank you for your questions. You need to mark your extension method with the following annotation (from JetBrains.Annotations package):

  [ContractAnnotation("null => true")]

Please let me know if you have any questions. Thank you!

1
Comment actions Permalink

That works well, but a few of the methods are in a library that we can't modify to annotate. Anything in the inspection configuration that could be set to do the same thing?

0
Comment actions Permalink

Please take a look at External Annotations, it should help in your case.

1

Please sign in to leave a comment.