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 551e6e9cb1ff..91af96dd8dd3 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 @@ -505,6 +505,24 @@ public class RedundantCastUtil { return; } } + if (parent instanceof PsiThrowStatement) { + final PsiClass thrownClass = PsiUtil.resolveClassInType(opType); + if (InheritanceUtil.isInheritor(thrownClass, false, CommonClassNames.JAVA_LANG_RUNTIME_EXCEPTION)) { + addToResults(typeCast); + return; + } + if (InheritanceUtil.isInheritor(thrownClass, false, CommonClassNames.JAVA_LANG_THROWABLE)) { + final PsiMethod method = PsiTreeUtil.getParentOfType(parent, PsiMethod.class); + if (method != null) { + for (PsiClassType thrownType : method.getThrowsList().getReferencedTypes()) { + if (TypeConversionUtil.isAssignable(thrownType, opType, false)) { + addToResults(typeCast); + return; + } + } + } + } + } if (parent instanceof PsiInstanceOfExpression || TypeConversionUtil.isAssignable(castTo, opType, false)) { addToResults(typeCast); } diff --git a/java/java-tests/testData/inspection/redundantCast/generics/CaseThrowable/expected.xml b/java/java-tests/testData/inspection/redundantCast/generics/CaseThrowable/expected.xml new file mode 100644 index 000000000000..cef9cae6cc39 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/CaseThrowable/expected.xml @@ -0,0 +1,13 @@ + + + + Test.java + 7 + Casting <code>e</code> to <code>Ex</code> is redundant + + + Test.java + 3 + Casting <code>e</code> to <code>FileNotFoundEx</code> is redundant + + diff --git a/java/java-tests/testData/inspection/redundantCast/generics/CaseThrowable/src/Test.java b/java/java-tests/testData/inspection/redundantCast/generics/CaseThrowable/src/Test.java new file mode 100644 index 000000000000..a260d3c2e99b --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/CaseThrowable/src/Test.java @@ -0,0 +1,13 @@ +public class RedundantCast{ + void m(IOEx e) throws IOEx { + throw (FileNotFoundEx)e; + } + + void foo(RuntimeException e) { + throw (Ex)e; + } + + static class Ex extends RuntimeException {} + static class IOEx extends Exception {} + static class FileNotFoundEx extends Exception {} +} diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCast15Test.java b/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCast15Test.java index 4581cf84f3f6..91afe58145b7 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCast15Test.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCast15Test.java @@ -49,6 +49,7 @@ public class RedundantCast15Test extends InspectionTestCase { public void testGetClassProcessing() throws Exception { doTest();} public void testInstanceOfChecks() throws Exception { doTest();} public void testForEachValue() throws Exception { doTest();} + public void testCaseThrowable() throws Exception { doTest();} public void testTypeParameterAccessChecksJava7() throws Exception { IdeaTestUtil.setTestVersion(JavaSdkVersion.JDK_1_7, getModule(), getTestRootDisposable());