I believe I have found a bug in ReSharper. Suppose I have 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 assigned to null in its scope, then the statement someArray != null would always be true. But that isn’t the case when that variable is given as ref-parameter to another method, since it could get assigned to null in that method. Then ReSharper incorrectly assumes that someArray != null is still always true.
I thought I’d share this information, because I’m unsure what I should do with this. Firstly I’d like someone to verify this bug, and afterwards send it to JetBrains?
Hm, apparently the static analysis of ReSharper is smarter than me….
The code in which I correctly get the "expression is always true" warning is:
I get the warning that
someArray != nullis redundant, so I thought ReSharper misinterpreted the ref parameter, sincesomeArraycan in fact be assigned to null. But that is not the reason why the warning is correct. Then a subtle fact plays a role: thatsomeArrayis null would mean that the method invocation ofBarwould throw aNullReferenceException, and with that alter the control flow such that the start of the while loop isn’t reached. So even whensomeArrayis assigned to null in Foo, the warning is correct.My mistake, and thank you all for your effort.