Mutable fields should not be "public static" (squid:S2386) using List.of()

(Janio Samuel) #1

We’re using SonarQube Community EditionVersion 7.5 (build 20543) and squid:S2386 fires a lot when using java.util.List of(). But using java.util.Collections unmodifiableList() the error disappears.
In the documentation both methods return an immutable list, so the error in the of() should not occur.
public static final List<String> WITH_ERROR = List.of("foo", "bar");

public static final List<String> WITHOUT_ERROR = Collections.unmodifiableList(List.of("foo", "bar"));

It would be great if S2386 could be updated to accept List.of().

1 Like
(Tibor Blenessy) #2

hello @JanioSamuel,

thanks for reporting this issue, I created the ticket to handle it