'Redundant local variable' inspection quirk

'Redundant local variable' inspection has a quirk with autoboxing:


IDEA says that 'j' is redundant, but inlining 'j' changes the semantics of the code as after inlining 'k' and 'l' do not reference to the same object!

1 comment

Mileta Cekovic wrote:

'Redundant local variable' inspection has a quirk with autoboxing:

 int i = 1;
> Integer j = i;
> Integer k = j;
> Integer l = j;
> boolean eq = k == l;
> ]]>


IDEA says that 'j' is redundant, but inlining 'j' changes the semantics of the code as after inlining 'k' and 'l' do not reference to the same object!


In fact, in this very special case IDEA is correct because
even after inlining 'j', the value of 'eq' is always 'true'.
This "feature" has been discussed in many places on the web,
e.g. in http://www.davidflanagan.com/blog/000022.html.

(Of course, when you replace the first line with something
like int i = 168, IDEA is most likely to err in saying
"Condition 'k == l' is always 'true' (but that may depend
on the compiler).)

Regards,
Jens

0

Please sign in to leave a comment.