IntelliJ Build #IU-223.8617.56, built on January 26, 2023
openjdk version OpenJDK 64-Bit Server VM Zulu17.40+19-CA (build 17.0.6+10-LTS, mixed mode, sharing)
When collecting a Stream with Collectors.toList() in Java 16+, S6204 suggests using the new method Stream.toList() to collect to an unmodifiable List. Code in constructors seems to be not considered a modification and gives a false-positive:
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class MyClass {
private MyClass(List<String> list) {
list.sort(Comparator.naturalOrder()); //mutation here
}
public static void factory() {
// Replace this usage of 'Stream.collect(Collectors.toList())' with 'Stream.toList()'
new MyClass(Stream.of("whatever").collect(Collectors.toList()));
}
}