Skip to content

Commit

Permalink
SONARJAVA-4912 S6204 Update issue message
Browse files Browse the repository at this point in the history
  • Loading branch information
kaufco committed Mar 18, 2024
1 parent 2c9f174 commit 0678be8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ static class ListWrapper {

void noncompliant() {
List<String> list1 = Stream.of("A", "B", "C")
.collect(Collectors.toList()); // Noncompliant [[sc=16;ec=35]] {{Replace this usage of 'Stream.collect(Collectors.toList())' with 'Stream.toList()'}}
.collect(Collectors.toList()); // Noncompliant [[sc=16;ec=35]] {{Replace this usage of 'Stream.collect(Collectors.toList())' with 'Stream.toList()' and ensure that the list is unmodified.}}

// Not modifying the list
list1.contains("B");

List<String> list2 = Stream.of("A", "B", "C")
.collect(Collectors.toUnmodifiableList()); // Noncompliant [[sc=16;ec=47]] {{Replace this usage of 'Stream.collect(Collectors.toUnmodifiableList())' with 'Stream.toList()'}}
.collect(Collectors.toUnmodifiableList()); // Noncompliant [[sc=16;ec=47]] {{Replace this usage of 'Stream.collect(Collectors.toUnmodifiableList())' with 'Stream.toList()'.}}

Stream.of("A", "B", "C")
.collect(Collectors.toList()); // Noncompliant [[sc=16;ec=35]] {{Replace this usage of 'Stream.collect(Collectors.toList())' with 'Stream.toList()'}}
.collect(Collectors.toList()); // Noncompliant [[sc=16;ec=35]] {{Replace this usage of 'Stream.collect(Collectors.toList())' with 'Stream.toList()' and ensure that the list is unmodified.}}

Stream.of("A", "B", "C")
.collect(Collectors.toUnmodifiableList()); // Noncompliant [[sc=16;ec=47]] {{Replace this usage of 'Stream.collect(Collectors.toUnmodifiableList())' with 'Stream.toList()'}}
.collect(Collectors.toUnmodifiableList()); // Noncompliant [[sc=16;ec=47]] {{Replace this usage of 'Stream.collect(Collectors.toUnmodifiableList())' with 'Stream.toList()'.}}

List<List<String>> listOfLists = new ArrayList<>();
// list1 appears in a call to List.add, but it is not the receiver, so it should not be interpreted as mutable:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

@Rule(key = "S6204")
public class CollectorsToListCheck extends AbstractMethodDetection implements JavaVersionAwareVisitor {
private static final String MESSAGE = "Replace this usage of 'Stream.collect(Collectors.%s())' with 'Stream.toList()'";
private static final String MESSAGE = "Replace this usage of 'Stream.collect(Collectors.%s())' with 'Stream.toList()'%s.";

private static final MethodMatchers COLLECT = MethodMatchers.create()
.ofSubTypes("java.util.stream.Stream")
Expand Down Expand Up @@ -113,7 +113,8 @@ private static boolean isInvariantTypeArgument(MethodInvocationTree collectMetho
}

private void reportIssue(MethodInvocationTree collector, boolean mutable) {
String message = String.format(MESSAGE, mutable ? "toList" : "toUnmodifiableList");
String message = mutable? String.format(MESSAGE, "toList", " and ensure that the list is unmodified")
: String.format(MESSAGE, "toUnmodifiableList", "");
reportIssue(collector, message);
}

Expand Down

0 comments on commit 0678be8

Please sign in to comment.