facebook / infer

A static analyzer for Java, C, C++, and Objective-C
http://fbinfer.com/
MIT License
14.83k stars 2k forks source link

A false negative about the rule INEFFICIENT_KEYSET_ITERATOR #1726

Open ghost opened 1 year ago

ghost commented 1 year ago

Infer version: v1.1.0 OS: Ubuntu 20

Command:

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.