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())
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!

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?

Comment actions Permalink

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


Please sign in to leave a comment.