FindBugs indicates "Dead store to local variable"

Mar 25, 2009

FindBugs, a nice tool for java developer. If you are interested in it, go to its home page to learn more.

'Dead Store to local variable', when i see this bug, it really confused me. From the description of FindBug:


DLS: Dead store to local variable (DLS_DEAD_LOCAL_STORE)

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.


Next, let's see the code:

List<Post> list = new List<Post>();
list = getPostList();

From the first line, I get a new list, however, this 'new' method is useless, list is got by the return of getPostList().

To avoid this bug, we can just delete the new method, like this.

List<Post> list = getPostList();

Java java eclipse

Two useful tools

Feb 12, 2009