infer run --inefficient-keyset-iterator -- javac Example.java
The following code example can be detected:
void inefficient_loop_itr_bad(HashMap<String, Integer> testMap) {
Iterator itr2 = testMap.keySet().iterator();
while (itr2.hasNext()) {
String key = (String) itr2.next();
testMap.get(key); // can report a warning here
}
}
However, it cannot detect the do-while loop:
void inefficient_loop_itr_bad(HashMap<String, Integer> testMap) {
Iterator itr2 = testMap.keySet().iterator();
do {
String key = (String) itr2.next();
testMap.get(key); // no warnings at this line
} while (itr2.hasNext());
}
These two code examples are equivalent, hence, I think this is a false negative.
Infer version: v1.1.0 OS: Ubuntu 20
Command:
The following code example can be detected:
However, it cannot detect the do-while loop:
These two code examples are equivalent, hence, I think this is a false negative.