Value Analysis with NonNullable annotations expects annotated parameters

I expected that value analysis (in R# 2.5) would warn me that from parameter passed to the method can be null.

public void CopyRangeTo(string from, string to)
{
CopyRangeTo(GetRange(from), GetRange(to));
}



public Range GetRange( string name)
{
return ranges[name];
}

But it just didn't. If I would annotate it with it will warn me. But not without annotation. Is this behaviour correct ?

4 comments
Comment actions Permalink

Hello ian,

Initial state of unannotated entity is considered "unknown", so we don't
know if it can or cannot be null. If we were warning about unknown entities,
we would have issued too many false positives. So the answer is "yes", it
is by design. However, if you compare entity with unknown state to null it
essentially become "nullable". That is, rules are pretty complex and we try
hard to warn about any possible case we can detect with good level of certainty.
On the hand we don't want false positives and this feature doesn't replace
testing, thinking and other good programming techniques :)

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


ih> I expected that value analysis (in R# 2.5) would warn me that from
ih> parameter passed to the method can be null.
ih>
ih> public void CopyRangeTo(string from, string
ih> to)
ih> {
ih> CopyRangeTo(GetRange(from), GetRange(to));
ih> }
ih>
ih> public Range GetRange( string name)
ih> {
ih> return ranges[name];
ih> }
ih> But it just didn't. If I would annotate it with it will
ih> warn me. But not without annotation. Is this behaviour correct ?
ih>


0
Comment actions Permalink

If I get it right, it's probably pretty useless to use this annotations. Am I really forced to annotate all possible parameters and methods (for output values) to behave as I expected ?

From my example:
Method GetRange() EXPECTS NonNullable.
In method CopyRangeTo() parameter "from" can be null. It's unknow, so it can be null. If passing this parameter to GetRange() without null checking doesn't rise any warning than it is really useless.

I expect warning or throwing exception for this case from any kind of Design By Contract environment or something similar.

I would expect that If I compare entity to null that afterwards it has to be handled as nonnullable.

//uknown here
if (something == null)
{
//nonnullable here

}
//nullable here

If the number of warning really matters than it can be some kind of switch in there. At least we are able to delete it from resharper options (code analysis) right now. But I guessed, it was it's purpose to warn if it can be mismatch (it CAN throw exception in runtime).

0
Comment actions Permalink

Hello ian,

ih> In method CopyRangeTo() parameter "from" can be null. It's unknow, so
it can be null.
It's unknown, so it is not known if it can be null or can't be null. ReSharper
can't read your mind and, we think, displaying invalid warnings is no better
then missing potential null-reference problem. In general, we annotate public
API methods and assume tests will cover implementations.

However, your point is also valid, so we will think about specifying default
behavior, i.e. if un-annotated entity is nullable, non-nullable or unknown.

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


0
Comment actions Permalink

According to the current state of computer science, there are two strategies
of "Nullness analysis" (we call it "value analysis"): optimistic and
pessimistic. Optimistic approach thinks that unknown code is valid,
pessimistic approach counts any unknown value as possible error.

To avoid tons of false positives, we've selected optimistic approach.
If you would like ReSharper to work according to pessimistic strategy, we
could add corresponding option - it is rather trivial to implement both
strategies

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"ian horowity" <no_reply@jetbrains.com> wrote in message
news:4303875.1168777176794.JavaMail.itn@is.intellij.net...
>I expected that value analysis (in R# 2.5) would warn me that from
>parameter passed to the method can be null.
>

public void CopyRangeTo(string from, string to)
{
CopyRangeTo(GetRange(from), GetRange(to));
}

>
>


public Range GetRange( string name)
{
return ranges[name];
}

>

But it just didn't. If I would annotate it with it will warn
me. But not without annotation. Is this behaviour correct ?



0

Please sign in to leave a comment.