IntelliJ IDEA Contract Breach Warning

Here is the Java code:

public static boolean anyEqual(Object needle, Object... haystack) { if(needle == null || haystack == null) { return false; } if(haystack.length == 0) { return false; } for(Object match : haystack) { if(match != null && needle.getClass() == match.getClass() && needle.equals(match)) { return true; // warning from IntelliJ here, 'contract clause !null, null -> false is violated' } } return false; } 

Does anyone know why this is shown? contract clause !null, null -> false is violated ? Thanks!

IntelliJ 14.0.2 build: 139.659

Screenshot: enter image description here

+7
java intellij-idea
source share
3 answers

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.

+5
source share

This looks like an IntelliJ error for me, because by removing the static from the method, the warning disappears.

Something must be misleading to static analysis here. You can always post this to youtrack, so jetbrains developers can look at it.

Someone already reported this issue here.

(checked on v14.0.3)

+1
source share

This message is displayed because IntelliJ is checking for contract violations. This is a relatively new feature, read more at https://www.jetbrains.com/idea/features/annotation_java.html

-3
source share

All Articles