diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/unneededThrows/RedundantThrowsDeclarationInspection.java b/java/java-analysis-impl/src/com/intellij/codeInspection/unneededThrows/RedundantThrowsDeclarationInspection.java index f3116d93b38e..1140a44b9531 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/unneededThrows/RedundantThrowsDeclarationInspection.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/unneededThrows/RedundantThrowsDeclarationInspection.java @@ -1,4 +1,4 @@ -// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.codeInspection.unneededThrows; import com.intellij.analysis.AnalysisScope; @@ -263,21 +263,23 @@ public final class RedundantThrowsDeclarationInspection extends GlobalJavaBatchI for (Map.Entry mapping : mappings.entrySet()) { final PsiElement from = mapping.getKey(); final PsiElement to = mapping.getValue(); + if (!from.isValid()) continue; if (to == null) { new CommentTracker().deleteAndRestoreComments(from); } - else { + else if (to.isValid()) { final PsiElement element = new CommentTracker().replaceAndRestoreComments(from, to); instance.shortenClassReferences(element); } } for (PsiTryStatement tryStatement : tryStatements) { - if (tryStatement.getCatchSections().length == 0 && - tryStatement.getFinallyBlock() == null && - tryStatement.getResourceList() == null) { - BlockUtils.unwrapTryBlock(tryStatement); - } + if (!tryStatement.isValid()) continue; + if (tryStatement.getCatchSections().length != 0) continue; + if (tryStatement.getFinallyBlock() != null) continue; + if (tryStatement.getResourceList() != null) continue; + + BlockUtils.unwrapTryBlock(tryStatement); } }); } diff --git a/java/java-psi-impl/src/com/intellij/codeInsight/BlockUtils.java b/java/java-psi-impl/src/com/intellij/codeInsight/BlockUtils.java index 3d16e449886a..9e3aea4db85d 100644 --- a/java/java-psi-impl/src/com/intellij/codeInsight/BlockUtils.java +++ b/java/java-psi-impl/src/com/intellij/codeInsight/BlockUtils.java @@ -1,4 +1,4 @@ -// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.codeInsight; import com.intellij.openapi.project.Project; @@ -112,6 +112,8 @@ public final class BlockUtils { } public static void unwrapTryBlock(PsiTryStatement tryStatement) { + assert tryStatement.isValid() : "The tryStatement PSI element must be valid"; + PsiCodeBlock tryBlock = tryStatement.getTryBlock(); if (tryBlock == null) { return; diff --git a/java/java-tests/testData/inspection/redundantThrowsGlobalFix/afterNestedInCatch.java b/java/java-tests/testData/inspection/redundantThrowsGlobalFix/afterNestedInCatch.java new file mode 100644 index 000000000000..f985e3a14fd2 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantThrowsGlobalFix/afterNestedInCatch.java @@ -0,0 +1,10 @@ +// "FileNotFoundException" "true" + +import java.io.FileNotFoundException; + +class Main { + public void f() {} + { + f(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantThrowsGlobalFix/beforeNestedInCatch.java b/java/java-tests/testData/inspection/redundantThrowsGlobalFix/beforeNestedInCatch.java new file mode 100644 index 000000000000..2fdda4cb7bf9 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantThrowsGlobalFix/beforeNestedInCatch.java @@ -0,0 +1,18 @@ +// "FileNotFoundException" "true" + +import java.io.FileNotFoundException; + +class Main { + public void f() throws FileNotFoundException {} + { + try { + f(); + } catch (FileNotFoundException e1) { + try { + f(); + } catch (FileNotFoundException e2) { + e2.printStackTrace(); + } + } + } +} \ No newline at end of file