Menu

#1395 UnusedPrivateMethod false positive for array element method call

PMD-5.3.4
closed
None
PMD
3-Major
Bug
PMD-5.3.3
UnusedPrivateMethod
2015-09-08
2015-08-26
No

A method is incorrectly flagged as unused when only called with an array element method return. The workaround is to get the element then call the method.

Example

    public void doSomething() {
        File[] files = getFiles();
        for (int i = 0; i < files.length; i++) {
            String name = getNameFromFilename(files[i].getName());
            System.out.println("name is " + name);
        }
    }

    private String getNameFromFilename(String fileName) {
        int index = fileName.lastIndexOf('.');
        return fileName.substring(0, index);
    }

Discussion

  • Andreas Dangel

    Andreas Dangel - 2015-09-08
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,6 +1,8 @@
     A method is incorrectly flagged as unused when only called with an array element method return.  The workaround is to get the element then call the method.
    
     Example
    +
    +~~~~~
         public void doSomething() {
             File[] files = getFiles();
             for (int i = 0; i < files.length; i++) {
    @@ -13,3 +15,5 @@
             int index = fileName.lastIndexOf('.');
             return fileName.substring(0, index);
         }
    +~~~~~
    +
    
    • status: open --> closed
    • assigned_to: Andreas Dangel
    • Milestone: New Tickets --> PMD-5.3.4
     

Log in to post a comment.