Menu

#1230 UseCollectionIsEmpty gets false positives

PMD-5.1.3
closed
PMD
3-Major
Bug
5.1.2
2015-01-14
2014-07-30
No

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");
        }
    }
}

Related

Issues: #1214
Issues: #1304

Discussion

  • Andreas Dangel

    Andreas Dangel - 2014-08-09

    Thanks for the bug report. This will be fixed with the next release.

     
  • Andreas Dangel

    Andreas Dangel - 2014-08-09
    • status: open --> closed
    • assigned_to: Andreas Dangel
    • Milestone: New Tickets --> PMD-next
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.