Fenix
Fenix

Reputation: 2391

Sonar "Dead store to local variable" violation when create DateTime and add seconds

I would like to create org.joda.time.DateTime with current time + 10 sec. Now I use this code:

DateTime testTime = new DateTime();    
testTime = testTime.plusSeconds(10);

But Sonar generates Dodgy - Dead store to local variable violation. I suppose that the problem is the instantiating of DateTime in first step because in the second step it is overwritten with a new instance (because plusSeconds method creates copy).

How to solve this? Is there some best practice - e.g. avoid the creation of two instances ("create current time and add same time in one step")?

Upvotes: 0

Views: 663

Answers (1)

Mithfindel
Mithfindel

Reputation: 4708

How about:

DateTime testTime = new DateTime().plusSeconds(10);

Upvotes: 1

Related Questions