IDEA-136413 ("Overly strong type cast" warning should take into account checked exceptions)

This commit is contained in:
Bas Leijdekkers
2015-08-24 12:08:57 +02:00
parent 6dc797f0bf
commit 5b88c8c41d
2 changed files with 31 additions and 0 deletions
@@ -15,6 +15,7 @@
*/
package com.siyeh.ig.psiutils;
import com.intellij.codeInsight.ExceptionUtil;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
@@ -22,10 +23,12 @@ import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.ContainerUtil;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashSet;
import java.util.Set;
public class ExpectedTypeUtils {
@@ -526,9 +529,12 @@ public class ExpectedTypeUtils {
if (aClass == null) {
return null;
}
final PsiReferenceList throwsList = method.getThrowsList();
final HashSet<PsiClassType> thrownTypes = ContainerUtil.newHashSet(throwsList.getReferencedTypes());
final PsiMethod[] superMethods = aClass.findMethodsBySignature(method, true);
PsiMethod topSuper = null;
PsiClass topSuperContainingClass = null;
methodLoop:
for (PsiMethod superMethod : superMethods) {
final PsiClass superClass = superMethod.getContainingClass();
if (superClass == null) {
@@ -552,6 +558,13 @@ public class ExpectedTypeUtils {
if (topSuper != null && superClass.isInheritor(topSuperContainingClass, true)) {
continue;
}
final PsiReferenceList superThrowsList = superMethod.getThrowsList();
final PsiClassType[] superThrownTypes = superThrowsList.getReferencedTypes();
for (PsiClassType superThrownType : superThrownTypes) {
if (!ExceptionUtil.isUncheckedException(superThrownType) && !thrownTypes.contains(superThrownType)) {
continue methodLoop;
}
}
topSuper = superMethod;
topSuperContainingClass = superClass;
}
@@ -70,4 +70,22 @@ class SAM {
{
Object runnable = (Runnable) () -> {};
}
}
class CloseableWarningTest {
private Object x = new Object();
public void example() {
//if (x instanceof SafeCloseable) {
((SafeCloseable) x).close();
//}
}
}
interface SafeCloseable extends Closeable {
@Override
void close();
}
interface Closeable extends AutoCloseable {
public void close() throws java.io.IOException;
}