From 8b03dc8d47d5483a06a1c54ac8c21a11acf03237 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Fri, 22 Feb 2019 17:06:28 +0700 Subject: [PATCH] IDEA-207296 Good code red: exception never thrown in the corresponding try block --- .../intellij/codeInsight/ExceptionUtil.java | 6 ++++++ .../ExceptionNeverThrownInTry.java | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/java/java-psi-impl/src/com/intellij/codeInsight/ExceptionUtil.java b/java/java-psi-impl/src/com/intellij/codeInsight/ExceptionUtil.java index 9a3a93a024ea..42c4b206c798 100644 --- a/java/java-psi-impl/src/com/intellij/codeInsight/ExceptionUtil.java +++ b/java/java-psi-impl/src/com/intellij/codeInsight/ExceptionUtil.java @@ -472,12 +472,18 @@ public class ExceptionUtil { MethodResolverProcessor processor = new MethodResolverProcessor((PsiMethodCallExpression)methodCall, containingFile); try { PsiScopesUtil.setupAndRunProcessor(processor, methodCall, false); + // Resolve other signatures in case of multiple interface inheritance + // e.g. consider + // interface X {void a() throws A;} interface Y{void a() throws B;} class Z extends X, Y {} + // here normal resolve returns X.a(), but we should check throws clauses for both a() methods. + // (see JLS 15.12.2.5) final List> candidates = ContainerUtil.mapNotNull( processor.getResults(), info -> { PsiElement element1 = info.getElement(); if (info instanceof MethodCandidateInfo && element1 != method && //don't check self MethodSignatureUtil.areSignaturesEqual(method, (PsiMethod)element1) && + !((PsiMethod)element1).hasModifierProperty(PsiModifier.PRIVATE) && !MethodSignatureUtil.isSuperMethod((PsiMethod)element1, method) && !(((MethodCandidateInfo)info).isToInferApplicability() && !((MethodCandidateInfo)info).isApplicable())) { return Pair.create((PsiMethod)element1, ((MethodCandidateInfo)info).getSubstitutor(false)); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting/ExceptionNeverThrownInTry.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting/ExceptionNeverThrownInTry.java index 9144be5a4acd..13dafe6948c7 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting/ExceptionNeverThrownInTry.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting/ExceptionNeverThrownInTry.java @@ -10,4 +10,22 @@ class x { catch (IOException e) { } } +} +////////////////// +class A { + private void a() { + } +} + +class B extends A { + void a() throws InterruptedException { + } + + void b() { + try { + a(); + } catch (InterruptedException e) { + // IDEA-207296 + } + } } \ No newline at end of file