diff --git a/java/java-psi-api/src/com/intellij/psi/util/RedundantCastUtil.java b/java/java-psi-api/src/com/intellij/psi/util/RedundantCastUtil.java index ac45aad9aa96..5730be7ac359 100644 --- a/java/java-psi-api/src/com/intellij/psi/util/RedundantCastUtil.java +++ b/java/java-psi-api/src/com/intellij/psi/util/RedundantCastUtil.java @@ -41,7 +41,7 @@ public class RedundantCastUtil { return new ArrayList<>(visitor.myFoundCasts); } - public static boolean isCastRedundant (PsiTypeCastExpression typeCast) { + public static boolean isCastRedundant(PsiTypeCastExpression typeCast) { PsiElement parent = typeCast.getParent(); PsiExpression operand = typeCast.getOperand(); if (operand != null && operand.getType() != null && operand.getType().equals(typeCast.getType())) return true; @@ -49,9 +49,9 @@ public class RedundantCastUtil { if (parent instanceof PsiExpressionList) parent = parent.getParent(); if (parent instanceof PsiReferenceExpression) parent = parent.getParent(); if (parent instanceof PsiAnonymousClass) parent = parent.getParent(); - MyIsRedundantVisitor visitor = new MyIsRedundantVisitor(true); + MyIsRedundantVisitor visitor = new MyIsRedundantVisitor(); parent.accept(visitor); - return visitor.isRedundant; + return visitor.foundRedundantCast == typeCast; } @Nullable @@ -63,10 +63,6 @@ public class RedundantCastUtil { private static class MyCollectingVisitor extends MyIsRedundantVisitor { private final Set myFoundCasts = new HashSet<>(); - private MyCollectingVisitor() { - super(true); - } - @Override public void visitClass(PsiClass aClass) { // avoid multiple visit @@ -90,24 +86,14 @@ public class RedundantCastUtil { } } + @SuppressWarnings("UnsafeReturnStatementVisitor") private static class MyIsRedundantVisitor extends JavaRecursiveElementWalkingVisitor { - private boolean isRedundant; - private final boolean myRecursive; - - private MyIsRedundantVisitor(final boolean recursive) { - myRecursive = recursive; - } - - @Override - public void visitElement(final PsiElement element) { - if (myRecursive) { - super.visitElement(element); - } - } + private PsiTypeCastExpression foundRedundantCast; protected void addToResults(@NotNull PsiTypeCastExpression typeCast){ if (!isTypeCastSemantic(typeCast)) { - isRedundant = true; + foundRedundantCast = typeCast; + stopWalking(); } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantConditional/afterTypeConversionOther.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantConditional/afterTypeConversionOther.java new file mode 100644 index 000000000000..7b7b7e2aacdc --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantConditional/afterTypeConversionOther.java @@ -0,0 +1,9 @@ +// "Simplify" "true" +class Test { + public static void main(String[] args) { + foo((String) null, (int) 0); + } + + static void foo(String s, int i) {} + static void foo(Number n, int i) {} +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantConditional/beforeTypeConversionOther.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantConditional/beforeTypeConversionOther.java new file mode 100644 index 000000000000..6905a4b0ac2a --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/constantConditional/beforeTypeConversionOther.java @@ -0,0 +1,9 @@ +// "Simplify" "true" +class Test { + public static void main(String[] args) { + foo(false ? "" : null, (int) 0); + } + + static void foo(String s, int i) {} + static void foo(Number n, int i) {} +} \ No newline at end of file