Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code Analysis Failure: Dead store to local variable

Tags:

java

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

Dead store to local variable:

This instruction assigns a value to a local variable, but the value is not read or used in any subsequent instruction. Often, this indicates an error, because the value computed is never used. Note that Sun's javac compiler often generates dead stores for final local variables. Because FindBugs is a bytecode-based 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  
     }
}
like image 305
c12 Avatar asked Dec 10 '13 07:12

c12


1 Answers

Iam not sure but I think you get the error message because you never use the assigned 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);

So you could write:

...
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);
like image 194
micha Avatar answered Sep 22 '22 07:09

micha