diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ExpectedTypeUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ExpectedTypeUtils.java index c9d04db31647..6c620472d2e7 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ExpectedTypeUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ExpectedTypeUtils.java @@ -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 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; } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/abstraction/overly_strong_type_cast/OverlyStrongTypeCast.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/abstraction/overly_strong_type_cast/OverlyStrongTypeCast.java index fbfeb59b8ccf..f1698ce5f7b5 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/abstraction/overly_strong_type_cast/OverlyStrongTypeCast.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/abstraction/overly_strong_type_cast/OverlyStrongTypeCast.java @@ -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; } \ No newline at end of file