- SonarLint for IntelliJ 8.2.0.68615
- IntelliJ IDEA 2023.1.2 (Ultimate Edition), Build #IU-231.9011.34, built on May 16, 2023
- OpenJDK 17.0.6+10
When collection a Stream
with Collectors.toList()
, SonarLint java:S6204
issue suggests to use the Stream.toList()
instead when the collected list is not going to be modified. Collections.reverse(List)
seems not to be detected as a function implying mutation, even if annotated with @Contract(mutates = "param1")
.
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class JavaS6204ListModification {
void undetectedMutatedList() {
List<Integer> toBeModifiedList = Stream.of(1, 2, 3)
.collect(Collectors.toList());
Collections.reverse(toBeModifiedList);
}
}