Code analysis error: dead storage for local variable

I have a code analysis tool that puts a line LinkedHashSet<String> widgetsToCreate = new LinkedHashSet<String>();in the method below, any ideas on how to fix the logic that will satisfy the analysis tool?

Dead store for local variable:

This command assigns a value to a local variable, but the value is not read or used in any subsequent instruction. Often this indicates an error, since the calculated value is never used. Note that the Sun javac compiler often generates dead repositories for final local variables. Since FindBugs is a bytecode tool, there is no easy way to eliminate these false positives.

public void add(Map<String, String> input) {    
    TreeSet<String> widgetsToAdd = new TreeSet<String>();
    TreeSet<String> widgetsToUpdate = new TreeSet<String>();
    LinkedHashSet<String> widgetsToCreate = new LinkedHashSet<String>();

    for (Map.Entry<String, String> entry : input.entrySet()) {
      //logic to add to widgetsToAdd based on content of the input Map
    }

     widgetsToCreate = processInput(widgetsToAdd);
     for (Iterator<String> wIterator = widgetsToCreate.iterator(); wIterator.hasNext();) {
         //process each widgetsToCreate  
     }
}
+4
source share
1

, , , new LinkedHashSet<String>();

// LinkedHashSet assigned to widgetsToCreate 
LinkedHashSet<String> widgetsToCreate = new LinkedHashSet<String>();

// widgetsToCreate is not used
for (Map.Entry<String, String> entry : input.entrySet()) {
  //logic to add to widgetsToAdd based on content of the input Map
}

// new value assigned to widgetsToCreate, the LinkedHashSet assigned before wasn't used
widgetsToCreate = processInput(widgetsToAdd);

, :

...
for (Map.Entry<String, String> entry : input.entrySet()) {
  //logic to add to widgetsToAdd based on content of the input Map
}
LinkedHashSet<String> widgetsToCreate = processInput(widgetsToAdd);
+12

All Articles