Search code examples
groovyunreachable-code

Why does this code warned unreachable in Groovy?


I was thinking to return a map with several directories list. But the very first caused a warning for me:

def enlistFiles() {

    return
        [downloadFolder: downloadFolder.listFiles( new FileFilter() {
            @Override
            boolean accept(File file) {
                return !file.isDirectory()
            }
        })]

}

"Code unreachable"

Why?


Solution

  • Anything below line 3 will not be executed. The return key word should not be followed by a line break. Your code should be:

    def enlistFiles() {
        return [downloadFolder: downloadFolder.listFiles( new FileFilter() {
                @Override
                boolean accept(File file) {
                    return !file.isDirectory()
                }
            })]
    
    }