SONARJAVA-6788: Implement S9361: Duplicate keys or elements should not be passed to immutable collection factory methods - #5988
SONARJAVA-6788: Implement S9361: Duplicate keys or elements should not be passed to immutable collection factory methods#5988nathsou wants to merge 4 commits into
Conversation
…mmutable collection factory methods
…nd expand constant comparison
This comment has been minimized.
This comment has been minimized.
…duplicated java.util.Map literal
Code Review ✅ Approved 3 resolved / 3 findingsImplements rule S9361 to flag duplicate keys or elements in immutable collection factory methods, resolving infinite recursion on cyclic field initializers and fixing HTML formatting in the metadata. ✅ 3 resolved✅ Quality: HTML 'How to fix it' uses markdown bullets that won't render
✅ Edge Case: resolveExpression can recurse infinitely on cyclic field initializers
✅ Bug: withoutSemantic test expects issues that require semantics
Implementation Status ✅ 1 / 1 issues implemented✅ SONARJAVA-6788 — 1 / 1 objectivesThe PR successfully implements rule S9361, detecting duplicate keys or elements passed to Map.of(), Map.ofEntries(), and Set.of() with comprehensive tests and documentation. ✅ 1 complete
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 |
|
romainbrenguier
left a comment
There was a problem hiding this comment.
Some changes to make on the rspec side, but the java implementation looks good.
| <p>Java 9 introduced immutable collection factory methods: <code>Map.of()</code>, <code>Map.ofEntries()</code>, and <code>Set.of()</code>. Unlike | ||
| mutable collections such as <code>HashMap</code> (where duplicate keys overwrite previous entries) or <code>HashSet</code> (where duplicate elements | ||
| are ignored), these static factory methods disallow duplicate keys and elements.</p> | ||
| <p>When duplicate keys are passed to <code>Map.of()</code> or <code>Map.ofEntries()</code>, or duplicate elements are passed to <code>Set.of()</code>, | ||
| an <code>IllegalArgumentException</code> is thrown at runtime during collection creation.</p> |
There was a problem hiding this comment.
The introduction of the rule should start by describing when/under which conditions the issue is raised.
| href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Map.html#ofEntries(java.util.Map.Entry…)">Method | ||
| Map.ofEntries</a></li> | ||
| <li>Java Documentation - <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Set.html#of(E…)">Method Set.of</a></li> | ||
| <li>{rule:java:S4143} - Map values should not be replaced unconditionally</li> |
There was a problem hiding this comment.
This should go to a === Related rules section




Summary
Map.of(...)andMap.ofEntries(...).Set.of(...)..withoutSemantic()check verifier tests.Links
AI disclosure