Skip to content

Commit d67e0f4

Browse files
Andreasleonardo-pilastri-sonarsource
authored andcommitted
SONARJAVA java:S6204 should not raise an issue when addFirst/addLast/removeFirst/removeLast is called on the list
1 parent bf899bf commit d67e0f4

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

java-checks-test-sources/default/src/main/java/checks/CollectorsToListCheckSample.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,26 @@ void compliant() {
119119
.collect(Collectors.toList()); // Compliant, list5 needs to be mutable
120120

121121
list5.removeIf(s -> true);
122+
123+
List<String> list6 = Stream.of("B", "C", "D")
124+
.collect(Collectors.toList()); // Compliant, list6 needs to be mutable
125+
126+
list6.addFirst("A");
127+
128+
List<String> list7 = Stream.of("A", "B", "C")
129+
.collect(Collectors.toList()); // Compliant, list7 needs to be mutable
130+
131+
list7.addLast("D");
132+
133+
List<String> list8 = Stream.of("A", "B", "C")
134+
.collect(Collectors.toList()); // Compliant, list8 needs to be mutable
135+
136+
list8.removeFirst();
137+
138+
List<String> list9 = Stream.of("A", "B", "C")
139+
.collect(Collectors.toList()); // Compliant, list9 needs to be mutable
140+
141+
list9.removeLast();
122142
}
123143

124144
void addX() {

java-checks/src/main/java/org/sonar/java/checks/CollectorsToListCheck.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public class CollectorsToListCheck extends AbstractMethodDetection implements Ja
5858

5959
private static final MethodMatchers LIST_MODIFICATION_METHODS = MethodMatchers.create()
6060
.ofSubTypes("java.util.List")
61-
.names("add", "addAll", "remove", "removeAll", "retainAll", "replaceAll", "set", "sort", "clear", "removeIf")
61+
.names("add", "addAll", "remove", "removeAll", "retainAll", "replaceAll", "set", "sort", "clear", "removeIf",
62+
"addFirst", "addLast", "removeFirst", "removeLast")
6263
.withAnyParameters()
6364
.build();
6465

0 commit comments

Comments
 (0)