UseCollectionIsEmpty gets false positives
A source code analyzer
Brought to you by:
adangel,
juansotuyo
So, just to prove that I'm dealing with weird and unusual code... since issue [#1214] was fixed in 5.1.2, now I get false positives for other comparisons with 1. I went ahead and tried all the comparisons with 1 and here are my findings. (Note there are still also a couple false negatives here, although I haven't seen them in the code I'm trying to clean up so far, and most of the issues at this point are false positives.)
import java.util.ArrayList;
public class IsEmptyTest {
public static void main(String args[]) {
ArrayList<String> testObject = new ArrayList<String>();
// these should be flagged (as they are equivalent to == 0) and are
//if (testObject.size() < 1) {
// System.out.println("List is empty");
//}
//if (1 > testObject.size()) {
// System.out.println("List is empty");
//}
// these should not be flagged, and are not
if (testObject.size() <= 1) {
System.out.println("List may or may not be empty");
}
if (1 >= testObject.size()) {
System.out.println("List may or may not be empty");
}
// these should be flagged (as they are equivalent to != 0) and are not
if (testObject.size() >= 1) {
System.out.println("List is not empty");
}
if (1 <= testObject.size()) {
System.out.println("List is not empty");
}
// these should not be flagged, yet are
if (testObject.size() > 1) {
System.out.println("List is not empty, but not all non-empty lists will trigger this");
}
if (1 < testObject.size()) {
System.out.println("List is not empty, but not all non-empty lists will trigger this");
}
if (testObject.size() != 1) {
System.out.println("List may or may not be empty");
}
if (1 != testObject.size()) {
System.out.println("List may or may not be empty");
}
if (testObject.size() == 1) {
System.out.println("List is not empty, but not all non-empty lists will trigger this");
}
if (1 == testObject.size()) {
System.out.println("List is not empty, but not all non-empty lists will trigger this");
}
}
}
Thanks for the bug report. This will be fixed with the next release.