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);
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.
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.
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:
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) .
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.
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.
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:
The warning disappears after you add it:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With