java:S6204 Code in Constructor is not considered a modification

  • SonarLint 7.4.0.60471
  • 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()));
   }
}

Hi,

Welcome to the community!

The latest version is 8.0.0.63273. Could you upgrade and see if this is still replicable?

 
Thx,
Ann

Retested with 8.0.0.63273. Still the same issue.

1 Like

Hi,

Thanks for checking. I’ve flagged this for team attention.

 
Ann

Hi again,

Actually, it seems that we’ve already created a ticket for this:

Hopefully it’ll be fixed soon.

 
:smiley:
Ann