Wrong mistake? The wrong expression is always true. "

I believe I found a bug in resharper. Suppose I have the code as follows:

int[] someArray = new int[10]; while (someArray != null) { //perhaps some other usage of someArray here, but not assigning it. SomeMethod(ref someArray ); } 

If the local variable someArray is not set to null in its scope, then the expression someArray != null will always be true. But this is not the case when this variable is set as a ref parameter for another method, since in this method it can be assigned to a zero value. Then resharper incorrectly assumes that someArray != null is still always true.

I thought I would share this information because I'm not sure what I should do with it. First, I would like someone to check this error, and then send it to JetBrains?

+4
source share
2 answers

Hm, apparently, the static analysis of Resharper is smarter than me .... The code in which I correctly get the "expression is always true" is a warning:

 int[] someArray = new int[10]; while (someArray != null) { Foo(ref someArray); someArray.Bar(); } 

I get a warning that someArray != null is redundant, which is why I, although Resharper misinterpreted the ref parameter, because someArray can actually be null. But this is not the reason that the warning is correct. Then the subtle fact plays: someArray is null, meaning that the invocation Bar method will NullReferenceException , and with this change the control thread so that the start of the while loop is not reached, so even when someArray to null in Foo, the warning is correct.

My mistake, and thank you all for your efforts.

+8
source

Try changing this loop to do this, and check that resharper marks it in this situation. Is this always the case? But in the first cycle he is really β€œalways right”, therefore resharper correctly evaluates it (this is a static analysis).

+1
source

Source: https://habr.com/ru/post/1346868/


All Articles