Files
openide/java/java-tests/testData/inspection/dataFlow/fixture/CompareEqualObjectWithNull.java
Tagir Valeev c79e23a713 Dataflow refactoring: DfaFactMap => DfType
GitOrigin-RevId: 3ef9e633dc92929191cc5724109b3575bf6d12a1
2019-12-11 09:07:22 +00:00

36 lines
1.3 KiB
Java

class Test {
void test(Object obj) {
Object base = obj;
while (base != null) {
base = getObject(obj);
}
// On one hand people write such kind of code (assuming that obj is never null) and the warning looks noise to them
// On the other hand if precondition loop is used then indeed obj was compared to null, which is useless if we assume that
// it's never null. This code is completely equivalent to test2 where the problem is more explicit.
// If obj is not expected to be null, we should not check the condition before loop, thus do-while should be used
// (or, alternatively, @NotNull annotation should be added)
System.out.println(obj.<warning descr="Method invocation 'hashCode' may produce 'NullPointerException'">hashCode</warning>());
}
void test2(Object obj) {
Object base = obj;
if (base != null) {
do {
base = getObject(obj);
}
while (base != null);
}
System.out.println(obj.<warning descr="Method invocation 'hashCode' may produce 'NullPointerException'">hashCode</warning>());
}
void test3(Object obj) {
Object base = obj;
do {
base = getObject(obj);
}
while (base != null);
System.out.println(obj.hashCode());
}
native Object getObject(Object obj);
}