Custom Null Checking

Answered

When a custom null checker expression (or statement) is defined, am I wrong in thinking that warnings regarding a possible null dereference should be satisfied?

For example, if my code base defines a null check expression as

    MyAssertion.IsNotNull(string name, object obj)  { ... }

and my null check pattern is defined as

    MyAssertion.IsNotNull($NAME$, $EXPR$);

Should this not produce a warning?

   void SomeFunction(SomeClass sc) {
      MyAssertion.IsNotNull(nameof(sc), sc); 

// This still shows a warning - possible null reference
      sc.Property = someValue;

   }
1
2 comments

Hello,

Pattern matching for Null checking does not define that the method checks nullability, but defines pattern which Rider will use when it will be asked to check nullability(for example, via context action). For notifying Rider that the method checks nullability you can use annotations:

class MyAssertion
{
[JetBrains.Annotations.ContractAnnotation("obj:null => halt")]
static void IsNotNull(string name, object obj)
{
...
}
}
2

Thank you for explaining, this makes perfect sense.

1

Please sign in to leave a comment.