Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ show "always true" hint but not "always false" for instanceof

So, I use IntelliJ IDEA to program in Java, and I was experimenting with the keyword instanceof and my code looked eventually like this:

public class Main {

    public static void main(String args[])
    {
        One one = new One();
        One two = new Two();

        if (one instanceof Two) 
        {
            System.out.println(one);
        }

        if (two instanceof Two) 
        {
            System.out.println(one);
        }

    }
}

class One { }

class Two extends One { }

IntelliJ gives me at the two instanceof Two line a hint "[...] is allways true", but for one instanceof Two IntelliJ doesn't give me a "[...] is always false" hint. Does anyone know why?

like image 628
Keheck Avatar asked Jul 07 '18 16:07

Keheck


People also ask

What is IntelliJ IDEA type hinting?

IntelliJ IDEA provides various means to assist inspecting and checking the types of the objects in your script. IntelliJ IDEA supports type hinting in function annotations and type comments using the typing module and the format defined by PEP 484.

How does IntelliJ IDEA check if a type is used correctly?

Any time you're applying type hints, IntelliJ IDEA checks if the type is used correctly according to the supported PEPs. If there is a usage error, the corresponding warning is shown and the recommended action is suggested. Below are the validation examples.

How to convert comment-based type hint to variable in IntelliJ IDEA?

Use a # type: comment to specify the types of local variables and attributes: For comment-based type hints, IntelliJ IDEA suggests an intention action that allows you to convert comment-based type hint to a variable annotation. This intention has the name Convert to variable annotation, and works as follows:

Does IntelliJ IDEA support Python stubs?

As IntelliJ IDEA supports Python stub files, you can specify the type hints using Python 3 syntax for both Python 2 and 3. If any type hints recorded in the stub files, they become available in your code that use these stubs. For example, the following type hint for some_func_2 becomes available in the Python code:


1 Answers

Updated: fixed in IDEA 2018.3.


(Disclaimer: IntelliJ IDEA developer is here, who is responsible for this feature).

Short answer: because it's not implemented.

When we track an actual type of variable within the data flow analysis, we use a model described by TypeConstraint class. It allows us to track the facts of two kinds: 1) if variable actual type is instanceof something and 2) if variable actual type is not instanceof something. Having these facts we can infer always true/always false instanceof in many cases E.g.:

void test(Object foo) {
  if (foo instanceof String) {
    if (foo instanceof Integer) { 
      // always false: "instanceof String" fact is not compatible 
      // with "instanceof Integer"
    }
  }
}

Or

void test(Object foo) {
  if (!(foo instanceof Number)) {
    if (foo instanceof Integer) { 
      // always false: "not instanceof Number" fact is not compatible 
      // with "instanceof Integer"
    }
  }
}

However for your case this model is not enough. We need to extend it to track the exact type of variable. In your code we track that one is instanceof One (which is compatible with instanceof Two fact), despite from the new expression we could know more precise type information that one is exactly One. This is not often usable, because in most of the cases (variable is method parameter, variable is returned from method, variable is assigned from field, array element, cast expression etc.) we cannot know whether the type is exact or subtype, so the current model is completely satisfactory. I can imagine only two cases where exactly One fact tracking is useful: the new expression (like in your case) and after comparison like obj.getClass() == Xyz.class.

I think, it's a reasonable feature to implement. I already thought about this, but as somebody else besides me also cares, I filed an issue, so you can track it.

like image 151
Tagir Valeev Avatar answered Nov 15 '22 00:11

Tagir Valeev