Sonar dead store to local variable
12,151
Its a false positive of sonar. Sonar works on the compiled code, not your source. I guess the access to descriptions1
is optimized away or replaced by the compiler, thus sonar cant see it.
Author by
marc3l
“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” - Martin Fowler 0110110101100001011100100110001101100101011011000110100001101111011001010110110001101100001011100110010001100101
Updated on July 17, 2022Comments
-
marc3l almost 2 years
I have the following code and sonar is telling me the following error message:
Dead store to descriptions1 in new com.dscsag.dsct2c.test.TestStep(Integer, String, Model)
In the follwing code:
for (Integer count = 0; count < testStepCount; count++) { if (xmlReader.isTestExistingForOrderNumber(count, orderNumber, version)) { if (xmlReader.checkForDuplicateTest(orderNumber, count, version)) { GlobalVariables.LOGGING_logger.error("### " + this.getClass().getSimpleName() + ": There are one or more duplicated order numbers for test " + count + " under test step " + orderNumber + "."); model.setStatusText("STATUS_DUPLICATED_TEST", orderNumber.toString(), StatusCode.ERROR); throw new Exception(); } else { GlobalVariables.LOGGING_logger.info("### " + this.getClass().getSimpleName() + ": No duplicated found for test " + count + " under test step " + orderNumber + "."); boolean functionNeeded = xmlReader.isFunctionNeededForOrderNumber(orderNumber, count, version); String[][] descriptions1 = new String[2][descriptions[0].length]; for (int a = 0; a < this.descriptions[0].length; a++) { descriptions1[0][a] = descriptions[0][a]; descriptions1[1][a] = xmlReader.getTestDescription(descriptions[0][a], orderNumber, count, version); } ArrayList<String> filesString = xmlReader.getTestFilesForOrderNumber(orderNumber, count, version); ArrayList<File> filesFile = new ArrayList<File>(); Iterator<String> it = filesString.iterator(); while (it.hasNext()) { File file = new File(GlobalVariables.PATH_TestFiles + xmlReader.getValueDocumentConfiguration(XmlElements.CAD_APPL, XmlElements.TESTING_ENVIRONMENT, null) + "/" + it.next()); filesFile.add(file); GlobalVariables.LOGGING_logger.info("### " + this.getClass().getSimpleName() + ": Added file to test " + count + " under test step " + orderNumber + ": \"" + file.getName() + "\""); } testObjectsList.add(new TestObject(functionNeeded, xmlReader.getFunctionNameForOrderNumber(orderNumber, count, version), descriptions1, filesFile)); GlobalVariables.LOGGING_logger.info("### " + this.getClass().getSimpleName() + ": Finally created test object for test " + count + " under test step " + orderNumber + "."); } } else { model.setStatusText("STATUS_NO_TEST", count.toString(), StatusCode.WARNING); } } }
Can you see why I store to "dead"
descriptions1
? I think I have to use this variable. -
Chriss almost 11 yearsYou can add the file to the exclusion filter. Maybe you can also disable the "dead store to local variable" pattern for this file, but i don't know if its possible.