[java-inspections] CastCanBeRemoved: process captured wildcards (IDEA-279096)

GitOrigin-RevId: 163695bf4d40bad340fea28fc9dbaa29c985acbd
This commit is contained in:
Tagir Valeev
2021-09-30 10:57:34 +00:00
committed by intellij-monorepo-bot
parent 849769445d
commit 02fdcefaa3
3 changed files with 26 additions and 2 deletions
@@ -0,0 +1,12 @@
// "Change type of 'obj' to 'Reference<?>' and remove cast" "true"
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
class CastToRef {
private static final ReferenceQueue<Object> queue = new ReferenceQueue<>();
public static void main(String[] args) throws InterruptedException {
Reference<?> obj = queue.remove();
obj.clear();
}
}
@@ -0,0 +1,12 @@
// "Change type of 'obj' to 'Reference<?>' and remove cast" "true"
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
class CastToRef {
private static final ReferenceQueue<Object> queue = new ReferenceQueue<>();
public static void main(String[] args) throws InterruptedException {
Object obj = queue.remove();
((<caret>Reference<?>)obj).clear();
}
}
@@ -28,7 +28,7 @@ public class CastCanBeRemovedNarrowingVariableTypeInspection extends AbstractBas
public void visitTypeCastExpression(PsiTypeCastExpression cast) {
PsiTypeElement castTypeElement = cast.getCastType();
if (castTypeElement == null || castTypeElement.getAnnotations().length > 0) return;
PsiType castType = cast.getType();
PsiType castType = GenericsUtil.getVariableTypeByExpressionType(cast.getType());
if (!(castType instanceof PsiClassType) || ((PsiClassType)castType).isRaw()) return;
PsiReferenceExpression ref = tryCast(PsiUtil.skipParenthesizedExprDown(cast.getOperand()), PsiReferenceExpression.class);
if (ref == null) return;
@@ -56,7 +56,7 @@ public class CastCanBeRemovedNarrowingVariableTypeInspection extends AbstractBas
else {
PsiExpression variableInitializer = variable.getInitializer();
if (variableInitializer != null) {
PsiType initializerType = variableInitializer.getType();
PsiType initializerType = GenericsUtil.getVariableTypeByExpressionType(variableInitializer.getType());
if (initializerType == null || !castType.isAssignableFrom(initializerType)) return;
}
}