Java

Java

By DeepSource

Iterable<Path> is errorprone and should be replaced with Collection<Path> JAVA-E1096

Bug risk

Avoid using Iterable<Path> to represent collections of Paths.

java.nio.file.Path implements Iterable in and of itself, which allows paths to be treated as lists of path components. To avoid confusing a single Path object for a List (or whatever other data structure) of Paths, use Collection or a similar generic type instead.

Bad Practice

In the example below, the for loop may iterate over paths as if it were a list of paths, but it is actually a single path split into its components.

Iterable<Path> paths = Path.of("some/path");

// !!! this works!
for (Path toFile : paths) {
    // ...
}

Recommended

To avoid accidentally allowing the usage of a single Path value where many are expected, you can use Collection classes, such as List, Set, or even Collection itself.

Collection<Path> = List.of(Path.of("path/a"), Path.of("path/b"));

for (Path path : paths) {
    // ...
}

References