Incorrect Possible NullReferenceException warning

Suppose you have a static method IsNotNull which takes a parameter and if it's null it throws an ArgumentNullException:
public static void IsNotNull(Object actualValue, String paramName)
{
if(actualValue == null)
{
throw new ArgumentNullException(paramName);
}
}

Then somewhere in your code you do the following:
IsNotNull(something, "something is null")
otherthing = something.somestuff;

Resharper points out at the second line and suggests that "something" can possibly be null. It completely ignores the IsNotNull method which guarantees that "something" is not null when the method returns.

I also created a JIRA ticket]]>

2 comments
Comment actions Permalink

ReSharper does "local" analysis, i.e. it doesn't look into invoked method
(it is impossible to do so, for example it could be in compiled DLL, or in
unknown language, etc.). So, to help ReSharper, You have to mark method
"IsNotNull" with assertion attribute:

public static void
IsNotNull(Object
actualValue, String paramName) {...}


--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Roman Royter" <no_replay@jetbrains.com> wrote in message
news:3556935.1205269158658.JavaMail.itn@is.intellij.net...

Suppose you have a static method IsNotNull which takes a parameter and if
it's null it throws an ArgumentNullException:
public static void IsNotNull(Object actualValue, String paramName)
{
if(actualValue == null)
{
throw new ArgumentNullException(paramName);
}
}

>

Then somewhere in your code you do the following:
IsNotNull(something, "something is null")
otherthing = something.somestuff;

>

Resharper points out at the second line and suggests that "something" can
possibly be null. It completely ignores the IsNotNull method which
guarantees that "something" is not null when the method returns.



0
Comment actions Permalink

Adding AssertionMethod also adds a using statement JetBrains.Annotations;
My concern is how the other VS without R# would compile this code?

0

Please sign in to leave a comment.