diff --git a/java/java-tests/testData/inspection/castCanBeRemovedNarrowingVariableType/afterCapture.java b/java/java-tests/testData/inspection/castCanBeRemovedNarrowingVariableType/afterCapture.java new file mode 100644 index 000000000000..d57ee03fd006 --- /dev/null +++ b/java/java-tests/testData/inspection/castCanBeRemovedNarrowingVariableType/afterCapture.java @@ -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 queue = new ReferenceQueue<>(); + + public static void main(String[] args) throws InterruptedException { + Reference obj = queue.remove(); + obj.clear(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/castCanBeRemovedNarrowingVariableType/beforeCapture.java b/java/java-tests/testData/inspection/castCanBeRemovedNarrowingVariableType/beforeCapture.java new file mode 100644 index 000000000000..abf9083573ab --- /dev/null +++ b/java/java-tests/testData/inspection/castCanBeRemovedNarrowingVariableType/beforeCapture.java @@ -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 queue = new ReferenceQueue<>(); + + public static void main(String[] args) throws InterruptedException { + Object obj = queue.remove(); + ((Reference)obj).clear(); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/src/com/intellij/codeInspection/redundantCast/CastCanBeRemovedNarrowingVariableTypeInspection.java b/plugins/InspectionGadgets/src/com/intellij/codeInspection/redundantCast/CastCanBeRemovedNarrowingVariableTypeInspection.java index d192583d3393..bb08817d1631 100644 --- a/plugins/InspectionGadgets/src/com/intellij/codeInspection/redundantCast/CastCanBeRemovedNarrowingVariableTypeInspection.java +++ b/plugins/InspectionGadgets/src/com/intellij/codeInspection/redundantCast/CastCanBeRemovedNarrowingVariableTypeInspection.java @@ -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; } }