Skip to content

SONARJAVA-7170 Implement rule S9439 - Array initializers should use the shorthand form instead of an explicit "new" array creation - #6352

Open
rombirli wants to merge 4 commits into
masterfrom
rombirli/implement-short-array-initializer
Open

rombirli wants to merge 4 commits into
masterfrom
rombirli/implement-short-array-initializer

Conversation

@rombirli

Copy link
Copy Markdown
Contributor

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 are var declarations and cases where the created type differs from the declared one.

A targeted quick fix removes the redundant new <type>[] prefix.

@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

SONARJAVA-7170

@datadog-sonarsource

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

⚖️ Ruling update ready for review: #6354

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Ruling Diff Summary

Detected changes in 4 rule files: 0 issues removed, 156 issues added.

S9439 (java) on commons-beanutils - 0 issues removed, 62 issues added - new ruling file

Added src/main/java/org/apache/commons/beanutils2/BeanUtilsBean.java (line 1108)

      1103 |      * @return A <code>Method<code> for <code>Throwable.initCause</code>, or
      1104 |      * <code>null</code> if unavailable.
      1105 |      */
      1106 |     private static Method getInitCauseMethod() {
      1107 |         try {
>>>   1108 |             final Class<?>[] paramsClasses = new Class<?>[] { Throwable.class };
      1109 |             return Throwable.class.getMethod("initCause", paramsClasses);
      1110 |         } catch (final NoSuchMethodException e) {
      1111 |             final Log log = LogFactory.getLog(BeanUtils.class);
      1112 |             if (log.isWarnEnabled()) {
      1113 |                 log.warn("Throwable does not have initCause() method in JDK 1.3");

Added src/main/java/org/apache/commons/beanutils2/converters/ArrayConverter.java (line 135)

       130 | 
       131 |     private final Class<?> defaultType;
       132 |     private final Converter elementConverter;
       133 |     private int defaultSize;
       134 |     private char delimiter    = ',';
>>>    135 |     private char[] allowedChars = new char[] {'.', '-'};
       136 |     private boolean onlyFirstToString = true;
       137 | 
       138 |     // ----------------------------------------------------------- Constructors
       139 | 
       140 |     /**

Added src/test/java/org/apache/commons/beanutils2/BeanMapTestCase.java (line 181)

       176 |     //
       177 |     // Then, I manually added the "class" key, which is a property that exists for
       178 |     // all beans (and all objects for that matter.
       179 |     @Override
       180 |     public Object[] getSampleKeys() {
>>>    181 |         final Object[] keys = new Object[] {
       182 |             "someIntValue",
       183 |             "someLongValue",
       184 |             "someDoubleValue",
       185 |             "someFloatValue",
       186 |             "someShortValue",

Added src/test/java/org/apache/commons/beanutils2/BeanMapTestCase.java (line 208)

       203 |     private final Object objectInFullMap = new Object();
       204 | 
       205 |     // note to self: the sample values were created manually
       206 |     @Override
       207 |     public Object[] getSampleValues() {
>>>    208 |         final Object[] values = new Object[] {
       209 |             new Integer(1234),
       210 |             new Long(1298341928234L),
       211 |             new Double(123423.34),
       212 |             new Float(1213332.12f),
       213 |             new Short((short)134),

Added src/test/java/org/apache/commons/beanutils2/BeanMapTestCase.java (line 226)

       221 |         return values;
       222 |     }
       223 | 
       224 |     @Override
       225 |     public Object[] getNewSampleValues() {
>>>    226 |         final Object[] values = new Object[] {
       227 |             new Integer(223),
       228 |             new Long(23341928234L),
       229 |             new Double(23423.34),
       230 |             new Float(213332.12f),
       231 |             new Short((short)234),

Added src/test/java/org/apache/commons/beanutils2/BeanUtilsTestCase.java (line 679)

       674 |     public void testPopulateArrayProperties() {
       675 | 
       676 |         try {
       677 | 
       678 |             final HashMap<String, Object> map = new HashMap<>();
>>>    679 |             int intArray[] = new int[] { 123, 456, 789 };
       680 |             map.put("intArray", intArray);
       681 |             String stringArray[] = new String[]
       682 |                 { "New String 0", "New String 1" };
       683 |             map.put("stringArray", stringArray);
       684 | 

Added src/test/java/org/apache/commons/beanutils2/BeanUtilsTestCase.java (line 681)

       676 |         try {
       677 | 
       678 |             final HashMap<String, Object> map = new HashMap<>();
       679 |             int intArray[] = new int[] { 123, 456, 789 };
       680 |             map.put("intArray", intArray);
>>>    681 |             String stringArray[] = new String[]
       682 |                 { "New String 0", "New String 1" };
       683 |             map.put("stringArray", stringArray);
       684 | 
       685 |             BeanUtils.populate(bean, map);
       686 | 

Added src/test/java/org/apache/commons/beanutils2/ConvertUtilsTestCase.java (line 305)

       300 |      */
       301 |     public void testPositiveIntegerArray() {
       302 | 
       303 |         Object value = null;
       304 |         final int intArray[] = new int[0];
>>>    305 |         final int intArray1[] = new int[] { 0 };
       306 |         final int intArray2[] = new int[] { 0, 10 };
       307 | 
       308 |         value = ConvertUtils.convert("{  }", intArray.getClass());
       309 |         checkIntegerArray(value, intArray);
       310 | 

Added src/test/java/org/apache/commons/beanutils2/ConvertUtilsTestCase.java (line 306)

       301 |     public void testPositiveIntegerArray() {
       302 | 
       303 |         Object value = null;
       304 |         final int intArray[] = new int[0];
       305 |         final int intArray1[] = new int[] { 0 };
>>>    306 |         final int intArray2[] = new int[] { 0, 10 };
       307 | 
       308 |         value = ConvertUtils.convert("{  }", intArray.getClass());
       309 |         checkIntegerArray(value, intArray);
       310 | 
       311 |         value = ConvertUtils.convert("0", intArray.getClass());

Added src/test/java/org/apache/commons/beanutils2/ConvertUtilsTestCase.java (line 494)

       489 |      */
       490 |     public void testPositiveStringArray() {
       491 | 
       492 |         Object value = null;
       493 |         final String stringArray[] = new String[0];
>>>    494 |         final String stringArray1[] = new String[]
       495 |             { "abc" };
       496 |         final String stringArray2[] = new String[]
       497 |             { "abc", "de,f" };
       498 | 
       499 |         value = ConvertUtils.convert("", stringArray.getClass());

Added src/test/java/org/apache/commons/beanutils2/DynaBeanMapDecoratorTestCase.java (line 44)

        39 |     private static final DynaProperty stringProp = new DynaProperty("stringProp", String.class);
        40 |     private static final DynaProperty nullProp   = new DynaProperty("nullProp",   String.class);
        41 |     private static final DynaProperty intProp    = new DynaProperty("intProp",    Integer.class);
        42 |     private static final DynaProperty dateProp   = new DynaProperty("dateProp",   Date.class);
        43 |     private static final DynaProperty mapProp    = new DynaProperty("mapProp",    Map.class);
>>>     44 |     private static final DynaProperty[] properties = new DynaProperty[] {
        45 |                       stringProp, nullProp, intProp, dateProp, mapProp};
        46 |     private static final DynaClass dynaClass = new BasicDynaClass("testDynaClass", BasicDynaBean.class, properties);
        47 | 
        48 |     private static String  stringVal = "somevalue";
        49 |     private static Integer intVal    = new Integer(5);

Added src/test/java/org/apache/commons/beanutils2/DynaBeanMapDecoratorTestCase.java (line 53)

        48 |     private static String  stringVal = "somevalue";
        49 |     private static Integer intVal    = new Integer(5);
        50 |     private static Date    dateVal   = new Date();
        51 |     private final Map<Object, Object>     mapVal    = new HashMap<>();
        52 | 
>>>     53 |     private final Object[] values = new Object[] {stringVal, null, intVal, dateVal, mapVal};
        54 | 
        55 |     private BasicDynaBean dynaBean;
        56 |     private Map<String, Object> decoratedMap;
        57 |     private Map<String, Object> modifiableMap;
        58 |     private static final Map<String, Object> emptyMap = new DynaBeanPropertyMapDecorator(new BasicDynaBean(new BasicDynaClass()));

Added src/test/java/org/apache/commons/beanutils2/DynaBeanUtilsTestCase.java (line 530)

       525 | 
       526 |         try {
       527 | 
       528 |             final HashMap<String, Object> map = new HashMap<>();
       529 |             //            int intArray[] = new int[] { 123, 456, 789 };
>>>    530 |             final String intArrayIn[] = new String[] { "123", "456", "789" };
       531 |             map.put("intArray", intArrayIn);
       532 |             String stringArray[] = new String[]
       533 |                 { "New String 0", "New String 1" };
       534 |             map.put("stringArray", stringArray);
       535 | 

Added src/test/java/org/apache/commons/beanutils2/DynaBeanUtilsTestCase.java (line 532)

       527 | 
       528 |             final HashMap<String, Object> map = new HashMap<>();
       529 |             //            int intArray[] = new int[] { 123, 456, 789 };
       530 |             final String intArrayIn[] = new String[] { "123", "456", "789" };
       531 |             map.put("intArray", intArrayIn);
>>>    532 |             String stringArray[] = new String[]
       533 |                 { "New String 0", "New String 1" };
       534 |             map.put("stringArray", stringArray);
       535 | 
       536 |             BeanUtils.populate(bean, map);
       537 | 

Added src/test/java/org/apache/commons/beanutils2/LazyDynaListTestCase.java (line 42)

        37 | public class LazyDynaListTestCase extends TestCase {
        38 | 
        39 |     private static final String BASIC_PROP1 = "BasicDynaClass_Property1";
        40 |     private static final String BASIC_PROP2 = "BasicDynaClass_Property2";
        41 | 
>>>     42 |     protected DynaProperty[] properties = new DynaProperty[] {
        43 |                                                new DynaProperty(BASIC_PROP1, String.class),
        44 |                                                new DynaProperty(BASIC_PROP2, HashMap.class)};
        45 | 
        46 |     protected DynaClass treeMapDynaClass = new LazyDynaMap(new TreeMap<String, Object>());
        47 |     protected DynaClass hashMapDynaClass = new LazyDynaMap(new HashMap<String, Object>());
S9439 (java) on eclipse-jetty - 0 issues removed, 54 issues added - new ruling file

Added jetty-http/src/main/java/org/eclipse/jetty/http/HttpGenerator.java (line 50)

(source file not found at this revision: jetty-http/src/main/java/org/eclipse/jetty/http/HttpGenerator.java)

Added jetty-http/src/main/java/org/eclipse/jetty/http/HttpGenerator.java (line 821)

(source file not found at this revision: jetty-http/src/main/java/org/eclipse/jetty/http/HttpGenerator.java)

Added jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java (line 374)

(source file not found at this revision: jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java)

Added jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java (line 379)

(source file not found at this revision: jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java)

Added jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java (line 748)

(source file not found at this revision: jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java)

Added jetty-http/src/test/java/org/eclipse/jetty/http/pathmap/UriTemplatePathSpecBadSpecsTest.java (line 36)

(source file not found at this revision: jetty-http/src/test/java/org/eclipse/jetty/http/pathmap/UriTemplatePathSpecBadSpecsTest.java)

Added jetty-io/src/main/java/org/eclipse/jetty/io/WriteFlusher.java (line 50)

(source file not found at this revision: jetty-io/src/main/java/org/eclipse/jetty/io/WriteFlusher.java)

Added jetty-server/src/main/java/org/eclipse/jetty/server/ProxyConnectionFactory.java (line 374)

(source file not found at this revision: jetty-server/src/main/java/org/eclipse/jetty/server/ProxyConnectionFactory.java)

Added jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHttpOutputInterceptor.java (line 47)

(source file not found at this revision: jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHttpOutputInterceptor.java)

Added jetty-server/src/test/java/org/eclipse/jetty/server/HttpChannelEventTest.java (line 111)

(source file not found at this revision: jetty-server/src/test/java/org/eclipse/jetty/server/HttpChannelEventTest.java)

Added jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java (line 598)

(source file not found at this revision: jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java)

Added jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java (line 630)

(source file not found at this revision: jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java)

Added jetty-server/src/test/java/org/eclipse/jetty/server/MultiPartParserTest.java (line 477)

(source file not found at this revision: jetty-server/src/test/java/org/eclipse/jetty/server/MultiPartParserTest.java)

Added jetty-server/src/test/java/org/eclipse/jetty/server/ProxyProtocolTest.java (line 124)

(source file not found at this revision: jetty-server/src/test/java/org/eclipse/jetty/server/ProxyProtocolTest.java)

Added jetty-server/src/test/java/org/eclipse/jetty/server/ProxyProtocolTest.java (line 125)

(source file not found at this revision: jetty-server/src/test/java/org/eclipse/jetty/server/ProxyProtocolTest.java)
S9439 (java) on eclipse-jetty-similar-to-main - 0 issues removed, 13 issues added - new ruling file

Added jetty-http/src/main/java/org/eclipse/jetty/http/HttpGenerator.java (line 50)

(source file not found at this revision: jetty-http/src/main/java/org/eclipse/jetty/http/HttpGenerator.java)

Added jetty-http/src/main/java/org/eclipse/jetty/http/HttpGenerator.java (line 821)

(source file not found at this revision: jetty-http/src/main/java/org/eclipse/jetty/http/HttpGenerator.java)

Added jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java (line 374)

(source file not found at this revision: jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java)

Added jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java (line 379)

(source file not found at this revision: jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java)

Added jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java (line 748)

(source file not found at this revision: jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java)

Added jetty-http/src/test/java/org/eclipse/jetty/http/pathmap/UriTemplatePathSpecBadSpecsTest.java (line 36)

(source file not found at this revision: jetty-http/src/test/java/org/eclipse/jetty/http/pathmap/UriTemplatePathSpecBadSpecsTest.java)

Added jetty-io/src/main/java/org/eclipse/jetty/io/WriteFlusher.java (line 50)

(source file not found at this revision: jetty-io/src/main/java/org/eclipse/jetty/io/WriteFlusher.java)

Added jetty-server/src/main/java/org/eclipse/jetty/server/ProxyConnectionFactory.java (line 374)

(source file not found at this revision: jetty-server/src/main/java/org/eclipse/jetty/server/ProxyConnectionFactory.java)

Added jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHttpOutputInterceptor.java (line 47)

(source file not found at this revision: jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHttpOutputInterceptor.java)

Added jetty-server/src/test/java/org/eclipse/jetty/server/HttpChannelEventTest.java (line 111)

(source file not found at this revision: jetty-server/src/test/java/org/eclipse/jetty/server/HttpChannelEventTest.java)

Added jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java (line 598)

(source file not found at this revision: jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java)

Added jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java (line 630)

(source file not found at this revision: jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java)

Added jetty-server/src/test/java/org/eclipse/jetty/server/MultiPartParserTest.java (line 477)

(source file not found at this revision: jetty-server/src/test/java/org/eclipse/jetty/server/MultiPartParserTest.java)
S9439 (java) on sonarqube-6.5 - 0 issues removed, 27 issues added - new ruling file

Added src/main/java/org/sonar/server/computation/task/projectanalysis/api/measurecomputer/MeasureComputerDefinitionImpl.java (line 85)

(source file not found at this revision: src/main/java/org/sonar/server/computation/task/projectanalysis/api/measurecomputer/MeasureComputerDefinitionImpl.java)

Added src/main/java/org/sonar/server/computation/task/projectanalysis/step/LanguageDistributionMeasuresStep.java (line 58)

(source file not found at this revision: src/main/java/org/sonar/server/computation/task/projectanalysis/step/LanguageDistributionMeasuresStep.java)

Added src/main/java/org/sonar/server/computation/task/projectanalysis/step/NewSizeMeasuresStep.java (line 169)

(source file not found at this revision: src/main/java/org/sonar/server/computation/task/projectanalysis/step/NewSizeMeasuresStep.java)

Added src/main/java/org/sonar/server/computation/task/projectanalysis/step/UnitTestMeasuresStep.java (line 53)

(source file not found at this revision: src/main/java/org/sonar/server/computation/task/projectanalysis/step/UnitTestMeasuresStep.java)

Added src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java (line 112)

(source file not found at this revision: src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java)

Added src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java (line 113)

(source file not found at this revision: src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java)

Added src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java (line 114)

(source file not found at this revision: src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java)

Added src/main/java/org/sonar/server/plugins/ServerPluginRepository.java (line 78)

(source file not found at this revision: src/main/java/org/sonar/server/plugins/ServerPluginRepository.java)

Added src/main/java/org/sonar/server/rule/ws/SearchAction.java (line 106)

(source file not found at this revision: src/main/java/org/sonar/server/rule/ws/SearchAction.java)

Added src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/MaintainabilityMeasuresVisitorTest.java (line 69)

(source file not found at this revision: src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/MaintainabilityMeasuresVisitorTest.java)

Added src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/NewMaintainabilityMeasuresVisitorTest.java (line 68)

(source file not found at this revision: src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/NewMaintainabilityMeasuresVisitorTest.java)

Added src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/RatingGridTest.java (line 44)

(source file not found at this revision: src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/RatingGridTest.java)

Added src/test/java/org/sonar/server/computation/task/projectanalysis/source/HighlightingLineReaderTest.java (line 126)

(source file not found at this revision: src/test/java/org/sonar/server/computation/task/projectanalysis/source/HighlightingLineReaderTest.java)

Added src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadMeasureComputersStepTest.java (line 61)

(source file not found at this revision: src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadMeasureComputersStepTest.java)

Added src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadMeasureComputersStepTest.java (line 70)

(source file not found at this revision: src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadMeasureComputersStepTest.java)

@github-actions

Copy link
Copy Markdown
Contributor

⚖️ Ruling update ready for review: #6354

@rombirli
rombirli force-pushed the rombirli/implement-short-array-initializer branch from 7de88b0 to ef43d1a Compare October 14, 2026 14:36
@github-actions

Copy link
Copy Markdown
Contributor

⚖️ Ruling update ready for review: #6354

@rombirli
rombirli force-pushed the rombirli/implement-short-array-initializer branch from ef43d1a to 09f67ae Compare October 14, 2026 14:59
@rombirli

Copy link
Copy Markdown
Contributor Author

/pvf

@hashicorp-vault-sonar-prod

Copy link
Copy Markdown
Contributor

📊 Performance Validation complete
🆕 2791 new · 🗑️ 0 lost issues
View dashboard

Comment on lines +61 to +62
<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>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe put it like that:

Suggested change
<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>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Applied your suggestion, the link now reads that way.

@@ -0,0 +1,64 @@
<h2>Why is this an issue?</h2>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should have a first sentence to say when the rule raises an issue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.”

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Message is now your wording, with the actual prefix from the source.

Comment on lines +65 to +67
.withQuickFix(() -> JavaQuickFix.newQuickFix(QUICK_FIX_MESSAGE)
.addTextEdit(JavaTextEdit.replaceBetweenTree(newKeyword, true, openBrace, false, ""))
.build())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this would delete annotations in cases like String[] names = new @Marker String[] {"GET"};. Could we have a quickfix test for that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, it did. Annotated types are no longer reported, with tests.

@hashicorp-vault-sonar-prod

Copy link
Copy Markdown
Contributor

📊 Performance Validation complete
🆕 2791 new · 🗑️ 0 lost issues
View dashboard

@rombirli
rombirli force-pushed the rombirli/implement-short-array-initializer branch from 09f67ae to 2a1a0c8 Compare October 16, 2026 12:54
@gitar-bot

gitar-bot Bot commented Oct 16, 2026

Copy link
Copy Markdown
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 new <type>[] alongside an array initializer, with a quick fix that removes the redundant prefix. Assignments, arguments, returns, varargs, annotations, var declarations, and mismatched types are excluded. No issues found.

✅ 1 closed
Closed: Bug: test_without_semantic expects issues the check can't raise

📄 java-checks/src/test/java/org/sonar/java/checks/ShortArrayInitializerCheckTest.java:37-44 📄 java-checks/src/main/java/org/sonar/java/checks/ShortArrayInitializerCheck.java:71-75
test_without_semantic runs the same sample (9 // Noncompliant markers) with .withoutSemantic().verifyIssues(). With semantic off there are no type bindings, so AbstractTypedTree.symbolType() returns Type.UNKNOWN for both variable.symbol().type() and newArray.symbolType(). isSameType then returns false on every declaration and the check reports nothing. JavaCheckVerifier.verifyIssues() still checks the sample's expectations (assertOneOrMoreIssues), so this test should fail on all expected issues. The check is conservative without semantic on purpose, so the test should assert no issues instead, ideally on a small dedicated sample.

Verification Rules 1/1 met

Functional validation No results

Rules 1 passed 2 not applicable

✅ Repository license compliance

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

@sonarqube-next

Copy link
Copy Markdown
Contributor

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants