IntelliJ prints a formal contract for your method:
null, _ -> false; !null, null -> false
What does this actually mean:
The first contract indicates that while the first parameter is null , it will return false . This is observed in your first if :
if(needle == null || haystack == null) { return false; }
The second contract indicates that if the second parameter is null , it will return false . This is also indicated in the same if .
My gut tells me that IntelliJ has some trouble understanding that a formal loop contract is in addition to all of the above, although that would be as simple as another condition in the contract expression.
for(Object match : haystack) { if(match != null && needle.getClass() == match.getClass() && needle.equals(match)) { return true; } }
Let us briefly move on to this.
- The extended operator does not work if
haystack is 0, so something needs to be considered. - Elements inside the array may be
null , and I'm not quite sure that IntelliJ static analysis does not close this part yet. - We have already established that the
needle must be non-zero, so we do not violate the contract on this line. - If we have a scenario in which
match != null && needle.getClass() == match.getClass() && needle.equals(match) is true , we return true . Otherwise, we return false .
There is nothing that I can see in the official documentation , which gives us the expression that we need to say: "hey, we check the elements of the array!"; it may happen that the analysis stumbled on what we were returning true , despite what we stated above (since haystack not null).
Let me emphasize this point:
haystack must not be null so you can enter advanced. . Your code will not work otherwise.
In general, I would not worry about that. Better yet, report a bug so that it can be fixed or expanded.
Makoto
source share