Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive of "Dead instanceof" hint #50

Closed
markiewb opened this issue Mar 9, 2015 · 7 comments
Closed

False positive of "Dead instanceof" hint #50

markiewb opened this issue Mar 9, 2015 · 7 comments
Labels
Milestone

Comments

@markiewb
Copy link
Owner

markiewb commented Mar 9, 2015

ACTUAL:

java.util.Set a = null; if (a instanceof java.util.HashSet) {} // ERROR
java.util.Set e = null; if (e instanceof java.util.ArrayList) {} //ERROR

EXPECTED:

java.util.Set a = null; if (a instanceof java.util.HashSet) {} // *NO* ERROR
java.util.Set e = null; if (e instanceof java.util.ArrayList) {} //ERROR
@markiewb markiewb added the bug label Mar 9, 2015
@markiewb markiewb added this to the 1.4.1 milestone Mar 9, 2015
@markiewb markiewb closed this as completed Mar 9, 2015
@markiewb markiewb modified the milestones: 1.4.1, 1.5.0 Mar 18, 2015
@FrantaM
Copy link

FrantaM commented Apr 16, 2015

Why is (e instanceof java.util.ArrayList) an error? While ArrayList itself cannot be cast to Set, you can have a subclass of ArrayList that can. See http://ideone.com/sqQbM0.

@markiewb
Copy link
Owner Author

The hint only checks the compile-type of "e". "e" is an Set, so internally
it checks for (java.util.Set instanceof java.util.ArrayList), which returns
false.

With kind regards, markiewb

2015-04-16 11:52 GMT+02:00 FrantaM [email protected]:

Why is (e instanceof java.util.ArrayList) an error? While ArrayList
itself cannot be cast to Set, you can have a subclass of ArrayList that
can. See http://ideone.com/sqQbM0.


Reply to this email directly or view it on GitHub
#50 (comment)
.

@FrantaM
Copy link

FrantaM commented Apr 16, 2015

I can see what it does I'm asking as to why. Instance of ArrayList can be an instanceof Set at the same time so this is still a false positive.

@markiewb
Copy link
Owner Author

My usecase is to catch the following construct

            java.util.Set set = xxx;
            if (set instanceof java.util.ArrayList) {
                java.util.ArrayList list = set; //<- compile error
            }

With kind regards, markiewb

2015-04-16 12:39 GMT+02:00 FrantaM [email protected]:

I can see what it does I'm asking as to why. Instance of ArrayList
can be an instanceof Set at the same time so this is still a false
positive.


Reply to this email directly or view it on GitHub
#50 (comment)
.

@btbouwens
Copy link

Indeed this bug is still active: I see this for f instanceof Inter for an interface Inter which isn't implemented by the type of f itself, but by possible subclasses.

@jrb0001
Copy link

jrb0001 commented Jul 5, 2016

Still not fixed in v1.6.0.3-SNAPSHOT:

InterfaceA something = new ClassBExtendsCImplementsA();
System.out.println(something instanceof ClassC); // Runtime: true, Netbeans: "Dead instanceof. InterfaceA cannot be assigned to ClassC"

@markiewb
Copy link
Owner Author

markiewb commented Jul 5, 2016

@btbouwens, @jrb0001 , @FrantaM : You are all correct. That hint makes no sense. I will remove it via #70

NB already warns you about invalid combinations.

// For example
if (new Double(42) instanceof java.util.Map) {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants