How can I set my null-checking method to satisfy the warning "possible 'null' assignment to entity marked with 'NotNull' attribute"

I have an overload function for string to make it slightly easier to check if a string has been populated.

 
    public static bool IsNullOrWhitespace(this string source)
    {
        return string.IsNullOrWhiteSpace(source);
    }


When I use this on variable, then use that variable as a [NotNull] paramater in a method call, I get the warning "possible 'null' assignment to entity marked with 'NotNull' attribute." Directly calling string.IsNullOrWhiteSpace(source) keeps the warning from showing up. Is there a way I can tell ReSharper that my method is a valid check for null?

2 comments
Comment actions Permalink

You need to add the ContractAnnotationAttribute to your method:

 
[ContractAnnotation("source:null => true")]
public static bool IsNullOrWhitespace(this string source)
{
    return string.IsNullOrWhiteSpace(source);
}
0
Comment actions Permalink

That did the trick, Richard. Thanks!

0

Please sign in to leave a comment.