1

I have a method that turns string.IsNullOrWhiteSpace(string value) into an extension method.

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

After I use this to check for null, Resharper still warns me that the variable I'm passing as a [NotNull] argument might be null.

"Possible 'null' assignment to entity marked with 'NotNull' attribute"

If I replace my use (str.IsNullOrWhiteSpace()) with the original (string.IsNullOrWhiteSpace(str)), the warning doesn't show up.

Is there a way with Resharper that I can train it to know that my extension method is a valid null checker so that this null assignment warning doesn't show up?

Note:

  1. I don't want to hide it with //resharper disable comments everwhere.
  2. I don't want to disable it entirely, as it should show up when I'm not null-checking.

Edit:

There's a property in the JetBrains.Annotations NuGet package called ContractAnnotation that does the trick.

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

This does the trick.

jack
  • 303
  • 3
  • 12

3 Answers3

3

Found it!

There's a property in the JetBrains.Annotations NuGet package called ContractAnnotation that does the trick.

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

This does the trick.

jack
  • 303
  • 3
  • 12
1

Except, if you want to remove the warning (ReSharper v2016.2), it should be false instead of true as shown in the answer.

[ContractAnnotation("parameterName:null => false")]

instead of

[ContractAnnotation("parameterName:null => true")]
dawide
  • 21
  • 2
  • 3
-1

it's because you can still call

((string)null).IsNullOrWhiteSpace()

and Resharper is concerned.

Rewrite the code as this:

(source == null) || string.IsNullOrWhiteSpace(source)

and maybe it'll stop complaining.

GreatAndPowerfulOz
  • 1,767
  • 13
  • 19
  • It still complains in the using code. Actually, when I added used your suggestion, Resharper recommended to "Merge sequential checks," which just removes the first one. – jack Oct 27 '15 at 14:58
  • calling ((string)null).IsNullOrWhitespace() would return false. – jack Oct 27 '15 at 14:59
  • @jack suggest you try it. – GreatAndPowerfulOz Oct 27 '15 at 15:36
  • I did try it, that's the results I got. What do you mean? – jack Oct 27 '15 at 20:33
  • 1
    @jack, if your Extension function is coded as shown, then there's no way that `((string)null).IsNullOrWhitespace()` can possibly return `false`. That or we live in differing universes. – GreatAndPowerfulOz Oct 27 '15 at 21:12
  • Sorry, I meant true. The point was that ((string)null).IsNullOrWhiteSpace() works as intended, so I'm not sure why Resharper would be concerned about it. – jack Oct 29 '15 at 16:21
  • @jack, Resharper is probably concerned about it because it has no idea of the implementation `IsNullorWhitespace` extension method. There may be a `#pragma` or `attribute` you can decorate the code with to tell Resharper to ignore null checks. – GreatAndPowerfulOz Oct 29 '15 at 16:24