From 02fdcefaa3f0283599d4f476da7ce177b0e6d484 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Thu, 30 Sep 2021 12:34:33 +0700 Subject: [PATCH] [java-inspections] CastCanBeRemoved: process captured wildcards (IDEA-279096) GitOrigin-RevId: 163695bf4d40bad340fea28fc9dbaa29c985acbd --- .../afterCapture.java | 12 ++++++++++++ .../beforeCapture.java | 12 ++++++++++++ ...tCanBeRemovedNarrowingVariableTypeInspection.java | 4 ++-- 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 java/java-tests/testData/inspection/castCanBeRemovedNarrowingVariableType/afterCapture.java create mode 100644 java/java-tests/testData/inspection/castCanBeRemovedNarrowingVariableType/beforeCapture.java 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; } }