Skip to content

fix: Replace Stream.collect(Collectors.toList()) with Stream.toList()#2288

Open
sonarqube-agent[bot] wants to merge 1 commit into
masterfrom
remediate-master-20260520-050223-ef396cab
Open

fix: Replace Stream.collect(Collectors.toList()) with Stream.toList()#2288
sonarqube-agent[bot] wants to merge 1 commit into
masterfrom
remediate-master-20260520-050223-ef396cab

Conversation

@sonarqube-agent
Copy link
Copy Markdown

Updated TestRulesTest.java to use the modern Stream.toList() method instead of collect(Collectors.toList()), which is more efficient and returns an unmodified list. This resolves a SonarQube MAJOR issue and removes the now-unused Collectors import.

View Project in SonarCloud


Fixed Issues

java:S6204 - Replace this usage of 'Stream.collect(Collectors.toList())' with 'Stream.toList()' and ensure that the list is unmodified. • MAJORView issue

Location: its/plugin/it-python-plugin-test/src/test/java/com/sonar/python/it/plugin/TestRulesTest.java:163

Why is this an issue?

In Java 8 Streams were introduced to support chaining of operations over collections in a functional style. The most common way to save a result of such chains is to save them to some collection (usually List). To do so there is a terminal method collect that can be used with a library of Collectors. The key problem is that .collect(Collectors.toList()) actually returns a mutable kind of List while in the majority of cases unmodifiable lists are preferred. In Java 10 a new collector appeared to return an unmodifiable list: toUnmodifiableList(). This does the trick but results in verbose code. Since Java 16 there is now a better variant to produce an unmodifiable list directly from a stream: Stream.toList().

What changed

Removes the now-unused import of java.util.stream.Collectors, which is no longer needed after replacing .collect(Collectors.toList()) with .toList(). This is a cleanup change that supports the main fix.

--- a/its/plugin/it-python-plugin-test/src/test/java/com/sonar/python/it/plugin/TestRulesTest.java
+++ b/its/plugin/it-python-plugin-test/src/test/java/com/sonar/python/it/plugin/TestRulesTest.java
@@ -25,1 +24,0 @@ import java.util.List;
-import java.util.stream.Collectors;

Have a suggestion or found an issue? Share your feedback here.


SonarQube Remediation Agent uses AI. Check for mistakes.

Fixed issues:
- AZ5AH3Zx14LASZpDSnJH for java:S6204 rule

Generated by SonarQube Agent (task: 58d238e1-36d5-4c15-8eb5-4561ce93720a)
@sonar-review-alpha
Copy link
Copy Markdown

sonar-review-alpha Bot commented May 20, 2026

Summary

Modernizes a single stream collection to use Stream.toList() instead of collect(Collectors.toList()). The change is at line 163 in TestRulesTest.java, where a sorted stream of issues is collected into a list. The deprecated pattern is replaced with the cleaner Java 16+ API that returns an unmodifiable list, and the now-unused Collectors import is removed.

What reviewers should know

What to check:

  • Single file change (TestRulesTest.java) with a straightforward substitution at line 163
  • The return type remains compatible: Stream.toList() returns an unmodifiable List, same as the original intent
  • Import cleanup is necessary and complete (Collectors is no longer used)
  • This requires Java 16+; verify the project targets that or later

Non-obvious decisions:

  • None — this is a standard modernization with no behavioral changes to the test logic

  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

Copy link
Copy Markdown

@sonar-review-alpha sonar-review-alpha Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! ✅

Clean, safe change. The project targets Java 17 (jdk.source.version=17), so Stream.toList() (Java 16+) is fully compatible. All callers of issues() only read the returned list (.get(0), hasSize(), assertThat()), so the switch to an unmodifiable list introduces no risk of UnsupportedOperationException. The Collectors import removal is correct — no other references to Collectors remain in the file.

There are ~35 other collect(Collectors.toList()) occurrences across python-frontend and python-checks production code that this PR does not touch, but that is out of scope for a targeted SonarQube remediation fix.

🗣️ Give feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants