Repository navigation
SONARJAVA-7043: Implemented rule S9412 "Collections.sort()" should not be used - #6221
Conversation
…t be used Detect calls to Collections.sort() and recommend using List.sort() instead (available since Java 8). Provides a quick fix to transform the code automatically. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This comment has been minimized.
This comment has been minimized.
|
❌ Ruling needs updating. A fix PR has been created: #6222 Please review and merge it into your branch. |
…n, add quickfix tests - Implement JavaVersionAwareVisitor to disable rule on Java < 8 projects - Wrap complex expressions (ternary, cast, etc.) in parentheses in quickfix - Add quickfix test annotations for 1-arg, 2-arg, and complex expression cases - Add test for Java 7 producing no issues Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
🤖 Generated with GitHub Actions
|
❌ Ruling needs updating. A fix PR has been created: #6222 Please review and merge it into your branch. |
Fixes SonarQube S1641 quality gate violation by using EnumSet.of() for enum-typed collections. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Ruling Diff SummaryDetected changes in 3 rule files: 0 issues removed, 15 issues added. S9412 (
|
nathsou
left a comment
There was a problem hiding this comment.
LGTM, two non-blocking remarks on the quick fix.
| } else { | ||
| replacement = listText + ".sort(null)"; | ||
| } | ||
| return JavaQuickFix.newQuickFix("Use \"%s\" instead", replacement) |
There was a problem hiding this comment.
The quick fix title embeds the full replacement text. With an inline multi-line lambda or anonymous Comparator (Collections.sort(list, (a, b) -> { ... })), the whole block ends up in the IDE quick fix menu entry. A fixed title such as Replace with "List.sort()" would avoid that.
There was a problem hiding this comment.
Addressed in 9ae90fc: the quick fix title is now the fixed Replace with "List.sort()".
| String comparatorText = QuickFixHelper.contentForTree(mit.arguments().get(1), context); | ||
| replacement = listText + ".sort(" + comparatorText + ")"; | ||
| } else { | ||
| replacement = listText + ".sort(null)"; |
There was a problem hiding this comment.
Collections.sort(List<T>) enforces T extends Comparable<? super T> at compile time, while list.sort(null) does not: if the element type later stops being Comparable, a compile error becomes a runtime ClassCastException. list.sort(Comparator.naturalOrder()) keeps the type check (at the cost of an import in the quick fix). This follows the RSPEC's "How to fix it" section, so it may be better settled on SonarSource/rspec#8233.
There was a problem hiding this comment.
Kept list.sort(null) for now: it matches the RSPEC's "How to fix it" section and avoids adding an import in the quick fix. I agree the compile-time check is worth keeping, so I'll raise it on SonarSource/rspec#8233. If the RSPEC moves to Comparator.naturalOrder(), we'll update the quick fix to match.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Code Review ✅ Approved 3 closed / 3 findings🟡 Medium risk · Adds a Java rule and automated quick fix for collection sorting calls. Implements rule S9412 to detect ✅ 3 closed✅ Bug: Rule isn't limited to Java 8+, though its description says it is
✅ Bug: Quick fix produces wrong code when the list argument needs parentheses
✅ Quality: Sample file never checks the quick fix
Review coverage🧪 Functional validation 2 of 4 objectives covered 📋 Rules No rules evaluated 🤖 Auto-approval Not enabled · Set up Implementation Status ◻️ 2 of 4 objectives covered◻️ SONARJAVA-7043 - 2 of 4 objectives coveredThis PR implements rule S9412 to detect "Collections.sort()" calls and provide a quick fix, while excluding support for import removal and custom Other objectives on this issue, possibly covered elsewhere:
✅ 2 covered here
OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
|




Summary
Collections.sort()and recommend usingList.sort()instead (available since Java 8)AbstractMethodDetectionwithMethodMatchersfor both 1-arg and 2-arg overloadsCollections.sort(list)tolist.sort(null)andCollections.sort(list, comparator)tolist.sort(comparator)Test plan
verifyIssues())verifyIssues())🤖 Generated with Claude Code
Agent workflow
PR created using
uv run new_rule_implementation.py S9412 -j SONARJAVA-7043 -a claude -g AGENTS.md .claude/skills/new-rule/SKILL.mdTool link: https://github.com/SonarSource/languages-experimental-tooling/tree/romain/my-tickets/personal/romain-brenguier
Iterated on the PR with
uv run ci_loop.pyfor 2 iterations.✔️ The PR is now ready for review.
Addressed review comments in pr_report_6221.md using
uv run address_reviews.py pr_report_6221.md