Consider the following code:
public static void Foo()
{
Bar(null);
}
public static void Bar([NotNull] string s)
{
if (s == null)
throw new ArgumentNullException("s");
}
The [NotNull]
attribute is used on Bar
to tell callers that s
shouldn't be null. This works fine, and I get a warning when I pass null to Bar
(Possible 'null' assignment to entity marked with 'NotNull' attribute).
But it doesn't actually prevent me from passing null, so Bar
must still check if s
is null. So why am I also getting a warning on if (s == null)
(Expression is always false) ?
As far as I can tell, this attribute has an ambiguous meaning; depending on context, it can mean two different things:
- for the caller: don't pass a null argument
- for the callee: this argument is not null
Am I using this attribute correctly, or am I missing something?
BTW, I'm using Resharper 7 EAP, so it might be a bug; however I want to make sure my usage is correct before I report it...
EDIT: just tried the same thing with R# 5.1 at work; it shows the warning on the call site, but not in the method. I'll report it on Jetbrain's Youtrack.
EDIT2: bug reported here