Repository navigation
SONARJAVA-7170 Implement rule S9439 - Array initializers should use the shorthand form instead of an explicit "new" array creation - #6352
Conversation
This comment has been minimized.
This comment has been minimized.
|
⚖️ Ruling update ready for review: #6354 |
Ruling Diff SummaryDetected changes in 4 rule files: 0 issues removed, 156 issues added. S9439 (
|
|
⚖️ Ruling update ready for review: #6354 |
7de88b0 to
ef43d1a
Compare
|
⚖️ Ruling update ready for review: #6354 |
ef43d1a to
09f67ae
Compare
|
/pvf |
|
📊 Performance Validation complete |
| <li>Java Documentation - <a href="https://docs.oracle.com/javase/specs/jls/se21/html/jls-10.html">Java Language Specification - Chapter 10. | ||
| Arrays</a></li> |
There was a problem hiding this comment.
Maybe put it like that:
| <li>Java Documentation - <a href="https://docs.oracle.com/javase/specs/jls/se21/html/jls-10.html">Java Language Specification - Chapter 10. | |
| Arrays</a></li> | |
| <li>Java Language Specification - <a href="https://docs.oracle.com/javase/specs/jls/se21/html/jls-10.html">Chapter 10. | |
| Arrays</a></li> |
There was a problem hiding this comment.
Applied your suggestion, the link now reads that way.
| @@ -0,0 +1,64 @@ | |||
| <h2>Why is this an issue?</h2> | |||
There was a problem hiding this comment.
We should have a first sentence to say when the rule raises an issue.
There was a problem hiding this comment.
Moved the trigger sentence to the top of the description.
| @Rule(key = "S9439") | ||
| public class ShortArrayInitializerCheck extends IssuableSubscriptionVisitor { | ||
|
|
||
| private static final String MESSAGE = "Remove the redundant array creation and keep only the array initializer."; |
There was a problem hiding this comment.
I find the message a bit redundant so not totally clear ('and' makes it sound like there are 2 actions to perform). Maybe something like “Remove the new {Type}[] prefix; the declaration already specifies the array type.”
There was a problem hiding this comment.
Message is now your wording, with the actual prefix from the source.
| .withQuickFix(() -> JavaQuickFix.newQuickFix(QUICK_FIX_MESSAGE) | ||
| .addTextEdit(JavaTextEdit.replaceBetweenTree(newKeyword, true, openBrace, false, "")) | ||
| .build()) |
There was a problem hiding this comment.
I think this would delete annotations in cases like String[] names = new @Marker String[] {"GET"};. Could we have a quickfix test for that?
There was a problem hiding this comment.
Good catch, it did. Annotated types are no longer reported, with tests.
|
📊 Performance Validation complete |
09f67ae to
2a1a0c8
Compare
Code Review ✅ Approved 1 closed / 1 findings🟡 Medium risk · Adding S9439 diagnostics and a quick fix that removes array-creation syntax could produce incorrect edits if type or annotation checks miss an edge case. Implements rule S9439 to flag variable and field declarations that use an explicit ✅ 1 closedClosed: Bug: test_without_semantic expects issues the check can't raise
Verification Rules 1/1 metFunctional validation No results Rules 1 passed 2 not applicable
Auto-approval Not enabledFor more verification details, comment 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 |
|




Implements S9439 for Java.
RSPEC: https://github.com/SonarSource/rspec/pull/8341
Jira: https://sonarsource.atlassian.net/browse/SONARJAVA-7170
The rule reports a variable or field declaration whose initializer spells out an explicit array creation on top of an array initializer, for example
new String[] {"GET"}where{"GET"}alone is enough. The shorthand is only legal in a declaration, so assignments, arguments, returns, varargs call sites and annotation element values are left alone, as arevardeclarations and cases where the created type differs from the declared one.A targeted quick fix removes the redundant
new <type>[]prefix.