Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I teach ReSharper a custom null check?

Tags:

c#

resharper

ReSharper is clever enough to know that a string.Format requires a not-null format argument so it warns me about it when I simply write

_message = string.Format(messageFormat, args);

where messageFormat can indeed be null. As soon as I add a condition for this variable:

if (!string.IsNullOrEmpty(messageFormat))
{
    _message = string.Format(messageFormat, args);
}

the warning disappears. Unfortunately it doesn't when I use an extension method:

if (messageFormat.IsNotNullOrEmpty())
{
    _message = string.Format(messageFormat, args); // possible 'null' assignment warning
}

My question is: is there a way to teach ReSharper that my extension method has the same meaning as !string.IsNullOrEmpty(messageFormat)?

The extension is defined as:

public static bool IsNotNullOrEmpty([CanBeNull] this string value) => !IsNullOrEmpty(value);
like image 733
t3chb0t Avatar asked Oct 06 '17 09:10

t3chb0t


People also ask

What is a null check?

A null indicates that a variable doesn't point to any object and holds no value. You can use a basic 'if' statement to check a null in a piece of code. Null is commonly used to denote or verify the non-existence of something.

IS NULL check C#?

The String class in the System namespace provides the IsNullOrEmpty() method to check if a string is null or an empty string(""). This is a handy method to validate user input.

How to write NULL checks in ReSharper?

This kind of null checks can be written in different ways, so they are configurable on the Code Editing | C# | Null checking page of ReSharper options ( Alt+R, O) , which is also accessible from the Alt+Enter menu on the corresponding actions:

How do I Turn Off [notnull] annotations in ReSharper?

This will let ReSharper notify you when null objects are passed to the decorated parameters. You can disable adding [NotNull] by clearing the Automatically propagate annotations checkbox on the Code Inspection | Code Annotations page of ReSharper options ( Alt+R, O) .

How do I check if a parameter is null or not?

If you use code annotations in your project, ReSharper will decorate parameters, which you check for null, with the [NotNull] attribute. This will let ReSharper notify you when null objects are passed to the decorated parameters.

How to change the Order of checks in ReSharper?

The order can be changed using Move up (Alt+U) and Move down (Alt+D) buttons. When ReSharper generates a null check, it will take the highest-priority pattern which semantically suits the context, taking into account the current C# version.


1 Answers

Yes there is. You need to use the ReSharper annotations to guide ReSharper's analysis. You're already using [CanBeNull] so they're already defined in your project.

The one you'll be interested in is ContractAnnotationAttribute:

Contract annotations let you define expected outputs for given inputs, or put in other words, define dependencies between reference type and boolean arguments of a function and its return value. The mechanism of contract annotations allows creating APIs that could be consumed in easier and safer way.

This is how you use it:

[ContractAnnotation("null => false")]
public static bool IsNotNullOrEmpty(this string value)
    => !string.IsNullOrEmpty(value);

The argument is a map of possible inputs (null, notnull, true, false) to outputs (null, notnull, canbenull, true, false, halt):

Here's another example:

[ContractAnnotation("foo: null => halt; bar: notnull => notnull")]
public string Frob(string foo, string bar)

Means that the decorated function will never return (or throw an exception) if you pass it null to the foo parameter, and guarantees that it won't return null if you pass a non-null value to bar.

The documentation describes the syntax in more detail.


Here's what happens without the attribute:

before

The warning disappears after you add it:

after

like image 168
Lucas Trzesniewski Avatar answered Oct 25 '22 02:10

Lucas Trzesniewski