Hello David,
Compiler warnings are issued by the javac compiler. I don't think there's 
anything that core libraries should do here.

For this particular cast warning, I tried and it seems to only happen when the 
operand expression has the result type int exactly; the warning does not appear 
when the expression is another primitive type like byte cast to int.

I think given Java is a statically-typed language, it is necessary for 
programmers to understand that every Java expression has a compile-time type; 
in this case, the method resolution depends on the type being int or 
java.lang.Integer. Maybe to communicate intent, declaring local variable of 
int/Integer type before calling remove can be another workaround.

Regards,
Chen
________________________________
From: David Alayachew <[email protected]>
Sent: Tuesday, April 21, 2026 4:26 PM
To: core-libs-dev <[email protected]>
Subject: [External] : Can we special case remove the warning for redundant 
casts when referring to j.u.List.remove(int)?

Hello @core-libs-dev <[email protected]>,

In case you are not aware, one unfortunate wart in the Collections library
is that, if you have a List<Integer>, then it is very easy for Overload
selection to pick the wrong method when you do List.remove(Object) vs
List.remove(int). This is because, if you have pass in an Integer, overload
selection calls List.remove(Object), but if you pass in an int, overload
selection calls List.remove(int).

One surefire way to make sure you don't get hurt by this is to make an
explicit cast to (int) or (Integer), allowing you to communicate intent
about which method you want. However, you then get a warning about
redundant cast.

Sure, we could add @SuppressWarnings everywhere that we do this, but that's
not ideal. Would it make sense to special case the warning to skip over
this case?

Thank you for your time and consideration.
David Alayachew

Reply via email to