Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to deal with bug "dead store to local variable" in java?

I wrote a simple test code. It is a circle. I suppose most people can image what is a circle class, so I will not paste it.

In the test code, I try to test the circle constructor with invalid point, and assume to throw an exception. But a bug occures. I check online, but still do not know how to solve the problem. Is there any one can help me? Thanks

code information, bug is in the last sentence of following code

/**
 * Tests that the Circle constructor throws an exception for center Point. 
 */
@Test (expected = IllegalArgumentException.class)
public void testIllegalCenter() {
  //Instantiates a circle with an incorrect center point.
    @SuppressWarnings("unused")
    final Circle testCircle = new Circle(VALID_RADIUS, INVALID_POINT, VALID_COLOR);
}

bug report

Bug: Dead store to testCircle in CircleTest.testIllegalCenter()

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.

like image 250
user3505808 Avatar asked Oct 20 '25 14:10

user3505808


1 Answers

Just remove the variable and call the constructor like this:

@Test (expected = IllegalArgumentException.class)
public void testIllegalCenter() {
    new Circle(VALID_RADIUS, INVALID_POINT, VALID_COLOR);
}
like image 95
Axel Avatar answered Oct 22 '25 02:10

Axel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!